http.ErrHandlerTimeout
GoERRORNotableHTTP
http: Handler timeout
Quick Answer
Wrap slow handlers with http.TimeoutHandler and check for this error to avoid writing after the deadline.
What this means
Returned by ResponseWriter.Write calls after the timeout set by http.TimeoutHandler has elapsed. The response has already been sent to the client.
Why it happens
- 1Handler took longer than the timeout duration configured in http.TimeoutHandler
- 2Slow database or external API call blocking the handler past its deadline
Fix
Check before writing
Check before writing
_, err := w.Write(data)
if errors.Is(err, http.ErrHandlerTimeout) {
return // response already sent
}Why this works
Returning immediately prevents superfluous write attempts after the timeout response.
Code examples
Wrap with TimeoutHandlergo
h := http.TimeoutHandler(myHandler,
2*time.Second, "timeout")
http.ListenAndServe(":8080", h)Detect in handlergo
_, err := fmt.Fprintln(w, "hello")
if errors.Is(err, http.ErrHandlerTimeout) {
return
}Context deadline checkgo
ctx := r.Context()
select {
case <-ctx.Done():
return
}Same error in other languages
Sources
Official documentation ↗
Go standard library
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev