go network_error ai_generated true

http: request context canceled

ID: go/http-request-context-canceled

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-01-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 90% success
    Check for context cancellation and return early: if r.Context().Err() != nil { return }
  2. 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:

  1. 80% fail

    Processing continues but the client is gone; results are wasted and may cause resource leaks.

  2. 60% fail

    Long timeouts can lead to resource exhaustion and don't address client-initiated cancellations.