go resource_error ai_generated true

错误:http:在已关闭的响应体上读取

error: http: read on closed response body

ID: go/net-http-response-body-not-closed

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

版本兼容性

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

根因分析

从已关闭的HTTP响应体中读取,通常由于过早关闭或重复关闭。

English

Reading from an HTTP response body that has already been closed, often due to premature close or double close.

generic

解决方案

  1. 95% 成功率 Always defer close after checking error
    resp, err := http.Get(url)
    if err != nil { return err }
    defer resp.Body.Close()
    // read resp.Body

无效尝试

常见但无效的做法:

  1. Not closing response body at all 80% 失败

    Leads to resource leak and eventually connection pool exhaustion.