Skip to content

Commit d3a6756

Browse files
jbarnettebelak
authored andcommitted
Wait for connections to finish when shutting down
PR #74 introduced a WaitGroup for listeners, but it doesn't wait for open connections before closing the server. This patch waits until all conns are closed before returning from Shutdown.
1 parent 2a96aa1 commit d3a6756

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

server.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Server struct {
3939
mu sync.Mutex
4040
listeners map[net.Listener]struct{}
4141
conns map[*gossh.ServerConn]struct{}
42+
connWg sync.WaitGroup
4243
doneChan chan struct{}
4344
}
4445

@@ -122,16 +123,17 @@ func (srv *Server) Shutdown(ctx context.Context) error {
122123
srv.closeDoneChanLocked()
123124
srv.mu.Unlock()
124125

125-
listenerWgChan := make(chan struct{}, 1)
126+
finished := make(chan struct{}, 1)
126127
go func() {
127128
srv.listenerWg.Wait()
128-
listenerWgChan <- struct{}{}
129+
srv.connWg.Wait()
130+
finished <- struct{}{}
129131
}()
130132

131133
select {
132134
case <-ctx.Done():
133135
return ctx.Err()
134-
case <-listenerWgChan:
136+
case <-finished:
135137
return lnerr
136138
}
137139
}
@@ -319,7 +321,9 @@ func (srv *Server) trackConn(c *gossh.ServerConn, add bool) {
319321
}
320322
if add {
321323
srv.conns[c] = struct{}{}
324+
srv.connWg.Add(1)
322325
} else {
323326
delete(srv.conns, c)
327+
srv.connWg.Done()
324328
}
325329
}

0 commit comments

Comments
 (0)