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

Preinitialize workCancel function in case StopAndCancel called before start #557

Merged
merged 1 commit into from
Aug 24, 2024

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Aug 23, 2024

This one's related to #549. Although the most proximate problem in that
repro code is that there was no error check when calling Start, it did
reveal a legitimate problem in that the River client will panic in case
StopAndCancel is called before Start because workCancel was never
set.

Here, initialize workCancel in the client's constructor. During normal
operation this will be overwritten almost immediately on Start as the
client starts up, but in case Start was never called or didn't run
successfully, it provides a function for StopAndCancel to call so that
it doesn't panic.

Fixes #549.

Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

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

Awesome, thank you!

@bgentry
Copy link
Contributor

bgentry commented Aug 23, 2024

@brandur oh, probably deserves a changelog entry too

@@ -491,6 +491,7 @@ func NewClient[TTx any](driver riverdriver.Driver[TTx], config *Config) (*Client
uniqueInserter: baseservice.Init(archetype, &dbunique.UniqueInserter{
AdvisoryLockPrefix: config.AdvisoryLockPrefix,
}),
workCancel: func(cause error) {}, // replaced on start, but here in case StopAndCancel is called before start up

Choose a reason for hiding this comment

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

for such usecase I would define a defautWorkCanceler := func(error) {}

Then use it here, it helps to document separately, and using

Suggested change
workCancel: func(cause error) {}, // replaced on start, but here in case StopAndCancel is called before start up
workCancel: defautWorkCanceler // replaced on start

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, not sure I agree on that one :) You're never going to reuse this function, and I think adding a named variable for it introduces a layer of indirection that in the end just serves to make it less clear what the code's doing.

…fore start

This one's related to #549. Although the most proximate problem in that
repro code is that there was no error check when calling `Start`, it did
reveal a legitimate problem in that the River client will panic in case
`StopAndCancel` is called before `Start` because `workCancel` was never
set.

Here, initialize `workCancel` in the client's constructor. During normal
operation this will be overwritten almost immediately on `Start` as the
client starts up, but in case `Start` was never called or didn't run
successfully, it provides a function for `StopAndCancel` to call so that
it doesn't panic.

Fixes #549.
@brandur brandur force-pushed the brandur-preinit-work-cancel branch from 4886b73 to 35623ec Compare August 24, 2024 21:42
@brandur
Copy link
Contributor Author

brandur commented Aug 24, 2024

@brandur oh, probably deserves a changelog entry too

Added.

@brandur brandur merged commit b4b778c into master Aug 24, 2024
14 checks passed
@brandur brandur deleted the brandur-preinit-work-cancel branch August 24, 2024 21:44
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.

Graceful shutdown - StopAndCancel panics
3 participants