go
network_error
ai_generated
true
http: request context canceled
ID: go/http-request-context-canceled
80%Fix Rate
85%Confidence
0Evidence
2024-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.17 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
The client canceled the request or the server's context was canceled due to timeout or client disconnect, causing the HTTP handler to abort.
generic中文
客户端取消请求或服务器上下文因超时或客户端断开而取消,导致 HTTP 处理器中止。
Workarounds
-
90% success
Check for context cancellation and return early: if r.Context().Err() != nil { return } -
70% success
Use http.Server with ReadTimeout and WriteTimeout properly: &http.Server{ReadTimeout: 10*time.Second, WriteTimeout: 10*time.Second}
Dead Ends
Common approaches that don't work:
-
80% fail
Processing continues but the client is gone; results are wasted and may cause resource leaks.
-
60% fail
Long timeouts can lead to resource exhaustion and don't address client-initiated cancellations.