Skip to content
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

net/http: zero-value Transport is leak-prone #19620

bcmills opened this issue Mar 20, 2017 · 1 comment

net/http: zero-value Transport is leak-prone #19620

bcmills opened this issue Mar 20, 2017 · 1 comment


Copy link

@bcmills bcmills commented Mar 20, 2017

The documentation for http.Transport warns:

By default, Transport caches connections for future re-use. This may leave many open connections when accessing many hosts. This behavior can be managed using Transport's CloseIdleConnections method and the MaxIdleConnsPerHost and DisableKeepAlives fields.

For users of the API, it may not be obvious that the idle connections may remain open even after the Transport becomes unreachable and is garbage-collected.

It would be nice if the zero-value Transport represented a configuration that did not leak connections. Better still if we could find a way to automatically close the idle connections when the Transport becomes unreachable.

I don't think this is fixable in Go 1, but we should revisit this aspect of the API if/when we're thinking about Go 2.

(@bradfitz @dsnet @tokkee)

@bradfitz bradfitz changed the title http: zero-value Transport is leak-prone net/http: zero-value Transport is leak-prone Mar 20, 2017
@bradfitz bradfitz added the Go2 label Mar 20, 2017
Copy link

@bradfitz bradfitz commented Mar 20, 2017

  • earlier question: why are users creating their own Transports? It'd be nice to understand that first.

  • Finalizers, in addition to being terrible, don't really help here since the goroutines run by the Transport also reference the Transport.

  • we could introduce a default Transport.IdleTimeout value. It's hard to argue go1compat there since servers can hang up on you at any moment randomly. To say "really, really forever" we could respect a negative time.Duration. But any default we picked (say, 60 seconds or 5 minutes) is still too long for people creating Transports in a loop. So that doesn't really help.

I'm going to close this but I'll mark it as Go2.

@bradfitz bradfitz closed this Mar 20, 2017
@golang golang locked and limited conversation to collaborators Mar 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.