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

Closed
nhooyr opened this issue Feb 22, 2018 · 4 comments
Closed

net/http: Server should not be reusable #24047

nhooyr opened this issue Feb 22, 2018 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@nhooyr
Copy link
Contributor

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
	<-sigs
	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?

@tortuoise
Copy link

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.

@nhooyr
Copy link
Contributor Author

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.

@andybons
Copy link
Member

/cc @bradfitz

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2018
@andybons andybons added this to the Unplanned milestone Mar 26, 2018
@nhooyr
Copy link
Contributor Author

nhooyr commented Feb 26, 2020

This seems to have been fixed.

@nhooyr nhooyr closed this as completed Feb 26, 2020
@golang golang locked and limited conversation to collaborators Feb 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants