Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Gowrapper around coroutines #1988

Merged
merged 13 commits into from
Dec 17, 2024
Merged

Conversation

cesarfda
Copy link
Contributor

@cesarfda cesarfda commented Dec 11, 2024

Resolves #1699:

This pull request introduces the use of gowrapper.Go() for better panic handling across various parts of the codebase. The changes ensure that goroutines are wrapped with proper context and logging for error handling. Below are the most significant changes grouped by theme:

Panic Handling with gowrapper.Go()

Additional Imports for gowrapper

Test Updates

Miscellaneous

@directionless
Copy link
Contributor

Might be in your TODO already, but can you add it to the linter?

@cesarfda cesarfda marked this pull request as ready for review December 13, 2024 17:33
Copy link
Contributor

@RebeccaMahany RebeccaMahany left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'm glad we've got this enforced via linting too.

Comment on lines +197 to +202
}, func(r any) {
slogger.Log(context.TODO(), slog.LevelError,
"exiting after runGroup panic",
"err", r,
)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to provide a second function to gowrapper.Go just to perform logging -- the gowrapper already handles logging on panic. This onPanic function is only needed if we need to perform an additional action when we notice a panic. (See e.g. https://github.com/kolide/launcher/blob/main/cmd/launcher/svc_windows.go#L205.)

Anywhere in this PR you have an onPanic function that just does logging, I would remove it and replace with func(r any) {}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just this one got left behind when you were updating 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I added that change to the refactor PR

ee/debug/shipper/shipper.go Outdated Show resolved Hide resolved
ee/desktop/runner/runner.go Outdated Show resolved Hide resolved
Copy link
Contributor

@James-Pickett James-Pickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@@ -79,7 +80,7 @@ func (r *RemoteRestartConsumer) Do(data io.Reader) error {

// Perform the restart by signaling actor shutdown, but delay slightly to give
// the actionqueue a chance to process all actions and store their statuses.
go func() {
gowrapper.Go(context.TODO(), r.slogger, func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love wrapping all our goroutines to help us be aware of panics! I think maybe we could improve the ergonomics of the wrapping so that we don't to pass an empty func if we just want the default onPanic behavior.

Some suggestions in no particular order:

  • Having another gowrapper func that doesn't require an OnPanic func.
  • Updating gowrapper.Go to do an nil check on the func so we could just pass nil as the 4th param.
  • Make the gowrapper into a struct and use an options pattern.

These are just suggestions, I'de also accept the PR as is.

Copy link
Contributor

@RebeccaMahany RebeccaMahany Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the first option especially, feels really easy to slot in with our existing usage. Maybe Go(ctx context.Context, slogger *slog.Logger, goroutine func()) (for usage that doesn't require an OnPanic func) and GoWithRecoveryAction(ctx context.Context, slogger *slog.Logger, goroutine func(), onPanic func(r any)) (for usage that does require an OnPanic func). I'd be fine with that in a follow-up PR as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll merge this one and start with the follow up on a seperate PR

@cesarfda cesarfda added this pull request to the merge queue Dec 17, 2024
Merged via the queue into kolide:main with commit dbb618a Dec 17, 2024
29 checks passed
@cesarfda cesarfda deleted the cesar/gowrapper branch December 17, 2024 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrap goroutines with gowrapper
4 participants