go runtime_error ai_generated true

http: 请求体已关闭

http: request body already closed

ID: go/http-request-body-already-closed

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

版本兼容性

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

根因分析

尝试读取或关闭已经关闭的http.Request.Body,通常是由于重复关闭或处理器返回后继续读取。

English

Attempting to read from or close an http.Request.Body that has already been closed, typically due to double-close or reading after the handler has returned.

generic

解决方案

  1. 95% 成功率
    if body, err := io.ReadAll(r.Body); err == nil { r.Body.Close() }
  2. 90% 成功率
    defer r.Body.Close() once at start of handler

无效尝试

常见但无效的做法:

  1. 80% 失败

    Silently ignoring leads to incomplete reads and potential data corruption

  2. 90% 失败

    Request body is a stream and cannot be reopened; must be buffered upfront if multiple reads needed