go network_error ai_generated true

http: write on closed connection

ID: go/http-write-after-shutdown

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Attempting to write a response to a client connection that has already been closed, often due to a timeout or client disconnect.

generic

中文

尝试向已关闭的客户端连接写入响应,通常是由于超时或客户端断开连接。

Workarounds

  1. 85% success
    if err := json.NewEncoder(w).Encode(data); err != nil {
        log.Printf("Error writing response: %v", err)
    }
  2. 80% success
    Use context cancellation to detect client disconnects early.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    The write will fail silently, and the client will never receive the response.

  2. 90% fail

    The issue is not buffer size but connection state.