net: synchronize calls to Close in the withTCPConnPair test helper

withTCPConnPair is supposed to return only when both peer functions
have completed. However, due to the use of "defer" it was closing the
peers' connections after the synchronization point instead of before.

Fixes #62542.

Change-Id: I3e06c78984664172ff2d28b0fc582b8182f710f9
Reviewed-on: https://go-review.googlesource.com/c/go/+/526977
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Commit-Queue: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Bryan C. Mills 2023-09-08 16:39:17 -04:00 committed by Gopher Robot
parent a742ae493f
commit 2e7e1d2e8d
1 changed files with 7 additions and 5 deletions

View File

@ -440,8 +440,9 @@ func withTCPConnPair(t *testing.T, peer1, peer2 func(c *TCPConn) error) {
errc <- err
return
}
defer c1.Close()
errc <- peer1(c1.(*TCPConn))
err = peer1(c1.(*TCPConn))
c1.Close()
errc <- err
}()
go func() {
c2, err := Dial("tcp", ln.Addr().String())
@ -449,12 +450,13 @@ func withTCPConnPair(t *testing.T, peer1, peer2 func(c *TCPConn) error) {
errc <- err
return
}
defer c2.Close()
errc <- peer2(c2.(*TCPConn))
err = peer2(c2.(*TCPConn))
c2.Close()
errc <- err
}()
for i := 0; i < 2; i++ {
if err := <-errc; err != nil {
t.Fatal(err)
t.Error(err)
}
}
}