Skip to content

Commit 3b23d57

Browse files
neildgopherbot
authored andcommitted
http2: fix race condition when disabling goroutine debugging for one test
Fixes golang/go#66519 Change-Id: I7aecf20db44caaaf49754d62db193b8c42f3c63a Reviewed-on: https://go-review.googlesource.com/c/net/+/701836 Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 8741050 commit 3b23d57

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

http2/gotrack.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,32 @@ import (
1515
"runtime"
1616
"strconv"
1717
"sync"
18+
"sync/atomic"
1819
)
1920

2021
var DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1"
2122

23+
// Setting DebugGoroutines to false during a test to disable goroutine debugging
24+
// results in race detector complaints when a test leaves goroutines running before
25+
// returning. Tests shouldn't do this, of course, but when they do it generally shows
26+
// up as infrequent, hard-to-debug flakes. (See #66519.)
27+
//
28+
// Disable goroutine debugging during individual tests with an atomic bool.
29+
// (Note that it's safe to enable/disable debugging mid-test, so the actual race condition
30+
// here is harmless.)
31+
var disableDebugGoroutines atomic.Bool
32+
2233
type goroutineLock uint64
2334

2435
func newGoroutineLock() goroutineLock {
25-
if !DebugGoroutines {
36+
if !DebugGoroutines || disableDebugGoroutines.Load() {
2637
return 0
2738
}
2839
return goroutineLock(curGoroutineID())
2940
}
3041

3142
func (g goroutineLock) check() {
32-
if !DebugGoroutines {
43+
if !DebugGoroutines || disableDebugGoroutines.Load() {
3344
return
3445
}
3546
if curGoroutineID() != uint64(g) {
@@ -38,7 +49,7 @@ func (g goroutineLock) check() {
3849
}
3950

4051
func (g goroutineLock) checkNotOn() {
41-
if !DebugGoroutines {
52+
if !DebugGoroutines || disableDebugGoroutines.Load() {
4253
return
4354
}
4455
if curGoroutineID() == uint64(g) {

http2/server_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,9 +3520,10 @@ func testServerContentLengthCanBeDisabled(t testing.TB) {
35203520
}
35213521

35223522
func disableGoroutineTracking(t testing.TB) {
3523-
old := DebugGoroutines
3524-
DebugGoroutines = false
3525-
t.Cleanup(func() { DebugGoroutines = old })
3523+
disableDebugGoroutines.Store(true)
3524+
t.Cleanup(func() {
3525+
disableDebugGoroutines.Store(false)
3526+
})
35263527
}
35273528

35283529
func BenchmarkServer_GetRequest(b *testing.B) {

0 commit comments

Comments
 (0)