go network_error ai_generated true

http:在已关闭的连接上写入

http: write on closed connection

ID: go/http-write-after-shutdown

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-08-02首次发现

版本兼容性

版本状态引入弃用备注
1.21 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

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

  2. 90% 失败

    The issue is not buffer size but connection state.