go
runtime_error
ai_generated
true
http: 请求体已关闭
http: request body already closed
ID: go/http-request-body-already-closed
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.
解决方案
-
95% 成功率
if body, err := io.ReadAll(r.Body); err == nil { r.Body.Close() } -
90% 成功率
defer r.Body.Close() once at start of handler
无效尝试
常见但无效的做法:
-
80% 失败
Silently ignoring leads to incomplete reads and potential data corruption
-
90% 失败
Request body is a stream and cannot be reopened; must be buffered upfront if multiple reads needed