Fix a listener leak when a server is stopped before it starts

If server.Stop() and server.Serve() race, Serve() can return without
closing the listener. This in turn can lead to clients timing out trying
to connect to a server that is neither accepting nor rejecting
connections.
This commit is contained in:
Ben Darnell
2016-05-16 19:11:17 -04:00
parent 4c4ed377c7
commit bab01e8e85
2 changed files with 64 additions and 1 deletions

View File

@ -257,12 +257,14 @@ func (s *Server) useTransportAuthenticator(rawConn net.Conn) (net.Conn, credenti
// Serve accepts incoming connections on the listener lis, creating a new
// ServerTransport and service goroutine for each. The service goroutines
// read gRPC requests and then call the registered handlers to reply to them.
// Service returns when lis.Accept fails.
// Service returns when lis.Accept fails. lis will be closed when
// this method returns.
func (s *Server) Serve(lis net.Listener) error {
s.mu.Lock()
s.printf("serving")
if s.lis == nil {
s.mu.Unlock()
lis.Close()
return ErrServerStopped
}
s.lis[lis] = true