/ go Public
net/http: Request context is not canceled when
Server.WriteTimeout is reached
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
What version of Go are you using (
Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
What did you do?
What did you expect to see?
Client side: all requests are cancelled latest after the write timeout, why wait until the handler is done to not receive the result?
r.Context()is cancelled, so it can stop handling because the result is not returned anyway
I have seen #21389 which is kinda related, but not the same issue. I do understand that
w.Write()does not immediately return an error because of buffering. I would expect the request context to be canceled immediately though.
What did you see instead?
Client: request took 1s, although the write timeout is 500ms
Handler: no way to tell whether the timeout was triggered, potentially expensive or destructive work continues
As is, I don't understand the use of
WriteTimeoutat all. It seems there is no effect, except for maybe big responses that might already be partially received by the client (see traefik/traefik#9191 (comment)).
I don't think this is a duplicate of #23262 because I see the behavior also when closing the body immediately.
It seems pretty simple to just write a middleware that wraps the request context with a timeout:
The text was updated successfully, but these errors were encountered: