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: Server should not be reusable #24047

nhooyr opened this issue Feb 22, 2018 · 4 comments

net/http: Server should not be reusable #24047

nhooyr opened this issue Feb 22, 2018 · 4 comments


Copy link

@nhooyr nhooyr commented Feb 22, 2018

Once a *http.Server is closed, it should not be reusable via *http.Server.Serve. If Serve is called on a closed *http.Server, it should return ErrServerClosed.

My reasoning behind this proposed change is that presently, there is subtle unexpected behaviour in what I think are pretty standard Go http servers. They have the following structure.

func main() {
	s := // define the http server
	go func() {
		err := s.ListenAndServe()
		// handle err
	sigs := // define and register sigs to receive os signals
	s.Close() // or s.Shutdown()

The problem with the above structure is the program may receive a os signal before the server has begun serving. It will call Close() or Shutdown() on the server. Then, the program could exit which means everything worked out fine but another possibility is that now the server begins serving. This is hypothetical and unlikely but if it does happen, the Server may receive a request and begin processing. This would be unexpected behaviour imo.

If the program originally called Close(), it would be unexpected for a new request to be accepted after the server is closed. If the program called Shutdown(), then the program may exit with a request in flight which would not be a graceful shutdown.

Now, I do not think we can change this behaviour because it is possible existing programs rely on it, perhaps Go 2?

Copy link

@tortuoise tortuoise commented Feb 23, 2018

I could very well be missing something. From the net/http docs:

Make sure the program doesn't exit and waits instead for Shutdown to return.

Copy link
Contributor Author

@nhooyr nhooyr commented Feb 23, 2018

Even if the program waits for Shutdown to return, Serve may still not have been called. If it does get called after Shutdown returns and it could be processing a request when the program exits, which would be very unexpected.

Copy link

@andybons andybons commented Mar 13, 2018

/cc @bradfitz

@andybons andybons added this to the Unplanned milestone Mar 26, 2018
Copy link
Contributor Author

@nhooyr nhooyr commented Feb 26, 2020

This seems to have been fixed.

@nhooyr nhooyr closed this Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.