go runtime_error ai_generated true

http: superfluous response.WriteHeader call from main.handler (server.go:42)

ID: go/http-superfluous-writeheader

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-06-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

WriteHeader is called more than once, or Write is called before WriteHeader. Only the first status code is sent.

generic

中文

WriteHeader 被调用多次,或在 WriteHeader 之前调用了 Write。只有第一个状态码会被发送。

Workarounds

  1. 90% success
    if !wroteHeader { w.WriteHeader(http.StatusOK); wroteHeader = true }
    json.NewEncoder(w).Encode(v)
  2. 88% success
    Return early after writing an error status; never write body after WriteHeader without checking.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Hides a real bug; the wrong status code may be sent to clients.

  2. 70% fail

    Does not fix the underlying control flow.