go network_error ai_generated true

http: response writer timeout: request timed out after 30s

ID: go/http-response-writer-timeout

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.21 active

Root Cause

The server's WriteTimeout expired while writing the response body, causing the connection to be closed abruptly.

generic

中文

服务器写入超时在写入响应体期间到期,导致连接突然关闭。

Workarounds

  1. 80% success
    Set a reasonable WriteTimeout and handle errors from Write: if _, err := w.Write(data); err != nil { return }
  2. 75% success
    Use http.TimeoutHandler to wrap handlers and return a timeout response: http.TimeoutHandler(handler, 30*time.Second, "timeout")

Dead Ends

Common approaches that don't work:

  1. 70% fail

    May cause slow clients to hold connections indefinitely, leading to resource exhaustion.

  2. 90% fail

    Writes will fail silently; data is corrupted and the connection is already closed.