-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: dial should not be canceled if an idle connection is reused #66442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#59017 looks related and #59017 (comment) in particular. |
Change https://go.dev/cl/572696 mentions this issue: |
@neild this is the same issue as #59017 but with a proposed fix. The context cancelation is still kept as is but if the idle connection is reused, the context cancelation is disabled and if the new connection's dial succeeds the connection is put to idle queue. This limits the number of created and closed connections and in combination with |
@neild (though you are already pinged above) |
I'm going to mark this as a duplicate, so we have discussion in only one place. |
Duplicate of #59017 |
Go version
go1.22.1 linux/amd64
Output of
go env
in your module/workspace:What did you do?
When the HTTP transport processes a new request it first tries to get an idle connection (if keep alive is not disabled). If there are no idle connection it will dial a new connection. If an idle connection becomes ready while dialing the new connection, the idle connection will be reused and the new connection will be put to idle queue (if there is space). If the request's context is canceled, both the request's and the new connection's context will be canceled.
This causes problems when canceling a context after the response has already been read and closed but the new connection dial has not yet completed (e.g. TLS handshake not yet done). In this case the new connection will be closed. This may cause a lot of new connections to be created and closed (
MaxConnsPerHost
is ignored). If this repeats a lot, too many TCP connections are stuck inCLOSE_WAIT
state and new connections cannot be created.This only affects HTTP/1 (HTTP and HTTPS).
Example:
Example code:
What did you see happen?
With higher concurrency even setting
transport.MaxConnsPerHost
does not help. The setting fixes request errors but still creates too many TCP connections.What did you expect to see?
$ go run .
The text was updated successfully, but these errors were encountered: