Skip to content

Commit 99cb1c2

Browse files
committed
net/http: deflake TestCancelRequestWhenSharingConnection
The test sleeps for 1 millisecond to give the cancellation a moment to take effect. This is flaky because the request can finish before the cancellation of the context is seen. It's easy to verify by adding time.Sleep(2*time.Millisecond) after https://github.com/golang/go/blob/0a6c4c87404ecb018faf002919e5d5db04c69ee2/src/net/http/transport.go#L2619. With this modification, the test fails about 5 times out of 10 runs. The fix is easy. We just need to block the handler of the second request until this request is cancelled. I have verify that the updated test can uncover the issue fixed by CL 257818.
1 parent 6774ddf commit 99cb1c2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/net/http/transport_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6530,16 +6530,17 @@ func testCancelRequestWhenSharingConnection(t *testing.T, mode testMode) {
65306530
if !errors.Is(err, context.Canceled) {
65316531
t.Errorf("request 2: got err %v, want Canceled", err)
65326532
}
6533+
6534+
// Unblock the first request.
6535+
close(idlec)
65336536
}()
65346537

65356538
// Wait for the second request to arrive at the server, and then cancel
65366539
// the request context.
65376540
r2c := <-reqc
65386541
cancel()
65396542

6540-
// Give the cancellation a moment to take effect, and then unblock the first request.
6541-
time.Sleep(1 * time.Millisecond)
6542-
close(idlec)
6543+
<-idlec
65436544

65446545
close(r2c)
65456546
wg.Wait()

0 commit comments

Comments
 (0)