Ensure connections are closed before WaitGroup marked as done

The previous ordering of defers meant the listener's connWG could fire and wake up other goroutines before the connection closed. Unsure if this caused any real bugs but this commit should make the code more correct.
This commit is contained in:
Chris S. Kim 2022-07-27 12:17:41 -04:00 committed by Chris S. Kim
parent 92c615c35f
commit 7f2732e12c
1 changed files with 5 additions and 3 deletions

View File

@ -166,9 +166,11 @@ func (l *Listener) Serve() error {
// handleConn is the internal connection handler goroutine.
func (l *Listener) handleConn(src net.Conn) {
defer src.Close()
defer func() {
// Make sure Listener.Close waits for this conn to be cleaned up.
defer l.connWG.Done()
src.Close()
l.connWG.Done()
}()
dst, err := l.dialFunc()
if err != nil {