Properly shutdown the server#244
Open
byo wants to merge 1 commit into
Open
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.
willscott
approved these changes
May 9, 2026
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.