http: on config reload, the grace period context is cancelled as soon as `App.Stop` returns, so graceful shutdown of the old servers always ends immediately with "context canceled"
Summary
On a config reload (not on process exit), modules/caddyhttp/app.go App.Stop() creates the grace-period context with context.WithTimeoutCause(...) and defer cancel(), but only waits for the server shutdowns when caddy.Exiting() is true. On a reload, Stop returns right after startedShutdown.Wait(), the deferred cancel() fires, and every server.server.Shutdown(ctx) returns context canceled at once.
Effects:
- the configured
grace_periodis effectively zero on every reload; - after
Shutdownreturns with that error, nothing closes the old server's remaining connections (there is noClose()after a failedShutdown), so any connection the graceful path did not close keeps being served by the old config for as long as the client keeps it open.
Evidence
Every reload logs, in the same millisecond:
INFO http servers shutting down; grace period initiated {"duration": 30}
ERROR http server shutdown {"error": "context canceled", "addresses": [":443"]}Reproduced with v2.11.2 and v2.11.3 (the same code is on master): any config with grace_period set, then POST /load of a new config. The context canceled error appears on each reload. With the fix below applied to v2.11.3, it no longer appears.
In production we observed one HTTP/2 connection from a CDN, opened at the exact moment of a reload, that kept being served by the replaced config for more than six minutes (its reverse_proxy upstream pointed to a port removed by the new config, so every request on that connection got 502, while all other connections used the new config). We have not yet reproduced how that connection escaped the GOAWAY, so we do not claim this issue explains it on its own. The context cancellation, however, removes the grace-period window during which such leftovers would otherwise be reaped.
Relevant code (v2.11.2, modules/caddyhttp/app.go, App.Stop)
if app.GracePeriod > 0 {
var cancel context.CancelFunc
timeout := time.Duration(app.GracePeriod)
ctx, cancel = context.WithTimeoutCause(ctx, timeout, ...)
defer cancel()
...
}
...
startedShutdown.Wait()
if caddy.Exiting() {
finishedShutdown.Wait()
}
// ... stop hooks ...
return nil // deferred cancel() fires here, while Shutdown(ctx) is still runningProposed fix
Release the grace context when the shutdowns finish, not when Stop returns:
- if app.GracePeriod > 0 {
- var cancel context.CancelFunc
+ cancel := context.CancelFunc(func() {})
+ if app.GracePeriod > 0 {
timeout := time.Duration(app.GracePeriod)
ctx, cancel = context.WithTimeoutCause(ctx, timeout, fmt.Errorf("server graceful shutdown %ds timeout", int(timeout.Seconds())))
- defer cancel()
...
if caddy.Exiting() {
finishedShutdown.Wait()
+ } else {
+ go func() {
+ finishedShutdown.Wait()
+ cancel()
+ }()
}
...
+ if caddy.Exiting() {
+ cancel()
+ }
app.stopped = trueWith this change, the context canceled error no longer appears on reload and the old servers get the configured grace period. It may also be worth calling server.server.Close() when Shutdown returns a deadline error, so that leftovers cannot outlive the grace period. That is a behaviour choice (long-lived streams), so we leave it to the maintainers.
Source: caddyserver/caddy