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: Keepalive connections are not terminated when server returns. #14927
Comments
Looks like this is caused by connection pooling. I adjusted your sleep to 30s so I could run netstat. You can see that the listening socket is closed, but there are still connections open to the server which will get re-used to that server process. Calling
It would be nice to close all existing connections after |
Could it potentially be fixed by disabling keep-alive on the server? (Not a general solution, but as a temporary workaround) |
It seems to be related to keepalives, where conenctions that are kept alive are served by the "old" server, even if the server has exited. |
Putting this in place of the closer := http.DefaultTransport.(interface {
CloseIdleConnections()
})
closer.CloseIdleConnections() |
@ncw - but isn't that a client-side fix? I would expect some way of terminating keep-alive connections when the server exits. In this specific case we can just disable keepalive altogether, but it seems like a big gotcha. |
related to: #9478 |
@klauspost yes the I had a look through the net and net/http code and I can't see a way to do the equivalent on the server side. Putting If as @jbardin suggests #9478 was fixed, then you could put However I think that |
There's also the old issue of graceful shutdown at #4674, but there are enough hooks to implement that externally (and some packages that do). I'd like it to be supported directly in net/http myself; maybe that's where we should focus. |
Yeah, this is basically a dup of #4674 and/or #9478. The confusion in your repro is that you thought your |
ok, since this is now linked to the other cases, I will close this. It was quite unexpected behavior for me - and from #9478 I can see for you as well. We should maybe add some documentation, that this is the current behaviour - SetKeepAlivesEnabled isn't very clear, and the rest of the http.Server documetation doesn't mention it either. |
1. What version of Go are you using?
go version go1.6 windows/amd64
andgo version go1.5.3 windows/amd64
.2. What operating system and processor architecture are you using?
3. What did you do?
Playground Reproduction (must be run locally)
The application creates an HTTP server in a separate goroutine.
This server is then shut down. Everything should be unreferenced by this point. Another server is created in a new goroutine.
It seems that unless there is data written to the ResponseWriter, the old handler functions are used, even for the new server, which has a separate http.ServeMux and http.Server.
It does not matter even if there is minutes between the old server being stopped and the new one being created.
This is not a client specific issue, just so you do not spend time investigating that. This also happens with a browser, which is how the issue was discovered.
4. What did you expect to see?
After server "1" is stopped, there should be no logging lines prefixed with "1", and the handler should always print "yahoo.com".
5. What did you see instead?
Since everything is created "cleanly" in the goroutine AFAICT, there should not be any was for server "1" handler functions to be used.
The text was updated successfully, but these errors were encountered: