Skip to content

Properly shutdown the server#244

Open
byo wants to merge 1 commit into
mainfrom
proper-server-shutdown
Open

Properly shutdown the server#244
byo wants to merge 1 commit into
mainfrom
proper-server-shutdown

Conversation

@byo
Copy link
Copy Markdown
Contributor

@byo byo commented May 8, 2026

When starting the shutdown connection use a new
context with specific timeout since the one used
in the server has already been canceled.

When starting the shutdown connection use a new
context with specific timeout since the one used
in the server has already been canceled.
@byo byo requested review from gammazero, nymd and willscott May 8, 2026 16:14
Comment thread server.go
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
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@nymd nymd assigned byo May 8, 2026
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.

3 participants