Skip to content

Commit a58afe4

Browse files
database64128gopherbot
authored andcommitted
net: fix testHookCanceledDial race
Loading and calling testHookCanceledDial in the function passed to context.AfterFunc is racey, because the function runs in a separate goroutine, and is not synchronized with the test code that restores the original testHookCanceledDial value. We could add a channel and wait for the "AfterFunc" to return in the deferred function, but that's a lot of synchronization overhead just for a bit of test code. Instead we simply load testHookCanceledDial into a local variable synchronously. This fixes the race without introducing any overhead. Fixes golang#75474 Change-Id: If8fbd0f5f65375577c2ded64a13a15b406c45ecc Reviewed-on: https://go-review.googlesource.com/c/go/+/704455 Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 3203a5d commit a58afe4

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/net/fd_unix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (fd *netFD) connect(ctx context.Context, la, ra syscall.Sockaddr) (rsa sysc
8282
defer fd.pfd.SetWriteDeadline(noDeadline)
8383
}
8484

85+
// Load the hook function synchronously to prevent a race
86+
// with test code that restores the old value.
87+
testHookCanceledDial := testHookCanceledDial
8588
stop := context.AfterFunc(ctx, func() {
8689
// Force the runtime's poller to immediately give up
8790
// waiting for writability, unblocking waitWrite

0 commit comments

Comments
 (0)