From 7f2732e12c3b3d96b378765bfbb92c09f0c2f456 Mon Sep 17 00:00:00 2001 From: "Chris S. Kim" Date: Wed, 27 Jul 2022 12:17:41 -0400 Subject: [PATCH] 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. --- connect/proxy/listener.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/connect/proxy/listener.go b/connect/proxy/listener.go index 546658d692..7839df0428 100644 --- a/connect/proxy/listener.go +++ b/connect/proxy/listener.go @@ -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() - // Make sure Listener.Close waits for this conn to be cleaned up. - defer l.connWG.Done() + defer func() { + // Make sure Listener.Close waits for this conn to be cleaned up. + src.Close() + l.connWG.Done() + }() dst, err := l.dialFunc() if err != nil {