Properly shutdown the server#244
Merged
Merged
Conversation
When starting the shutdown connection use a new context with specific timeout since the one used in the server has already been canceled.
nymd
requested changes
May 8, 2026
Comment on lines
335
to
350
| <-s.Context.Done() | ||
| err := serv.Shutdown(s.Context) | ||
|
|
||
| shutdownCtx, cancel := context.WithTimeout(context.Background(), config.Server.ShutdownTimeout) | ||
| defer cancel() | ||
|
|
||
| err := serv.Shutdown(shutdownCtx) | ||
| if err != nil { | ||
| log.Warnw("failed shutdown", "err", err) | ||
| log.Warnw("failed to shutdown", "err", err) | ||
| ec <- err | ||
| } | ||
|
|
||
| err = metricsServ.Shutdown(shutdownCtx) | ||
| if err != nil { | ||
| log.Warnw("failed to shutdown metrics server", "err", err) | ||
| ec <- err | ||
| } |
There was a problem hiding this comment.
If both shutdowns fail the second send blocks forever and defer close(ec) never runs.
Cleanest fix is collect both errors and send once, which keeps the deal with the consumer as "at most one value, then close":
Suggested change
| <-s.Context.Done() | |
| err := serv.Shutdown(s.Context) | |
| shutdownCtx, cancel := context.WithTimeout(context.Background(), config.Server.ShutdownTimeout) | |
| defer cancel() | |
| err := serv.Shutdown(shutdownCtx) | |
| if err != nil { | |
| log.Warnw("failed shutdown", "err", err) | |
| log.Warnw("failed to shutdown", "err", err) | |
| ec <- err | |
| } | |
| err = metricsServ.Shutdown(shutdownCtx) | |
| if err != nil { | |
| log.Warnw("failed to shutdown metrics server", "err", err) | |
| ec <- err | |
| } | |
| <-s.Context.Done() | |
| shutdownCtx, cancel := context.WithTimeout(context.Background(), config.Server.ShutdownTimeout) | |
| defer cancel() | |
| var errs []error | |
| if err := serv.Shutdown(shutdownCtx); err != nil { | |
| log.Warnw("failed to shutdown finder server", "err", err) | |
| errs = append(errs, fmt.Errorf("finder server: %w", err)) | |
| } | |
| if err := metricsServ.Shutdown(shutdownCtx); err != nil { | |
| log.Warnw("failed to shutdown metrics server", "err", err) | |
| errs = append(errs, fmt.Errorf("metrics server: %w", err)) | |
| } | |
| if err := errors.Join(errs...); err != nil { | |
| ec <- err | |
| } |
Needs errors added to the import block.
Contributor
Author
There was a problem hiding this comment.
Thanks, I analyzed the code further and found out that there could be more issues with the approach.
Updated the code to properly handle a case where there are multiple errors sent through the channel with extra unit tests to ensure out application / signal interface layer handles such case properly.
willscott
approved these changes
May 9, 2026
* Use non-blocking send when config time change is detected avoiding rare deadlock * Use same channel for signal detection for config file time change detection
This function is responsible for integration between the running server and external signals that could affect the server: termination and config reload.
5479660 to
e2951ba
Compare
nymd
approved these changes
May 20, 2026
nymd
left a comment
There was a problem hiding this comment.
Elegant fix to do it consumer-side. Thanks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When starting the shutdown connection use a new
context with specific timeout since the one used
in the server has already been canceled.