Skip to content

Commit 525ddce

Browse files
committed
server: fix Serve() vs. immediate Shutdown() race.
Fix a race where an asynchronous server.Serve() invoked in a a goroutine races with an almost immediate server.Shutdown(). If Shutdown() finishes its locked closing of listeners before Serve() gets around to add the new one, Serve will sit stuck forever in l.Accept(), unless the caller closes the listener in addition to Shutdown(). This is probably almost impossible to trigger in real life, but some of the unit tests, which run the server and client in the same process, occasionally do trigger this. Then, if the test tries to verify a final ErrServerClosed error from Serve() after Shutdown() it gets stuck forever. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent dd32dde commit 525ddce

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

server.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,18 @@ func (s *Server) RegisterService(name string, desc *ServiceDesc) {
7474
}
7575

7676
func (s *Server) Serve(ctx context.Context, l net.Listener) error {
77-
s.addListener(l)
77+
s.mu.Lock()
78+
s.addListenerLocked(l)
7879
defer s.closeListener(l)
7980

81+
select {
82+
case <-s.done:
83+
s.mu.Unlock()
84+
return ErrServerClosed
85+
default:
86+
}
87+
s.mu.Unlock()
88+
8089
var (
8190
backoff time.Duration
8291
handshaker = s.config.handshaker
@@ -188,9 +197,7 @@ func (s *Server) Close() error {
188197
return err
189198
}
190199

191-
func (s *Server) addListener(l net.Listener) {
192-
s.mu.Lock()
193-
defer s.mu.Unlock()
200+
func (s *Server) addListenerLocked(l net.Listener) {
194201
s.listeners[l] = struct{}{}
195202
}
196203

0 commit comments

Comments
 (0)