go system_error ai_generated true

http: Server.Shutdown: context deadline exceeded

ID: go/http-server-shutdown-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-02-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.16 active
1.20 active

Root Cause

During graceful shutdown, the server did not finish processing active requests within the given context deadline.

generic

中文

在优雅关闭期间,服务器未能在给定的上下文截止时间内完成处理活动请求。

Workarounds

  1. 85% success
    Set a reasonable timeout and handle error: ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second); defer cancel(); if err := srv.Shutdown(ctx); err != nil { log.Fatal(err) }
  2. 90% success
    Track active connections and wait for them before calling Shutdown: use sync.WaitGroup to wait for handlers to finish.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Delays shutdown indefinitely and may not solve if requests hang forever.

  2. 80% fail

    Loses active connections and may corrupt data.