Skip to content

Commit 4c943ab

Browse files
committed
runtime: fix comments on the behavior of SetGCPercent
Fixes for #49680, #49695, #45867, and #49370 all assumed that SetGCPercent(-1) doesn't block until the GC's mark phase is done, but it actually does. The cause of 3 of those 4 failures comes from the fact that at the beginning of the sweep phase, the GC does try to preempt every P once, and this may run concurrently with test code. In the fourth case, the issue was likely that only *one* of the debug_test.go tests was missing a call to SetGCPercent(-1). Just to be safe, leave a TODO there for now to remove the extraneous runtime.GC calls, but leave the calls in. Updates #49680, #49695, #45867, and #49370. Change-Id: Ibf4e64addfba18312526968bcf40f1f5d54eb3f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/369815 Reviewed-by: Austin Clements <[email protected]> Trust: Michael Knyszek <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent dc65c48 commit 4c943ab

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

misc/cgo/test/testdata/issue9400_linux.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func test9400(t *testing.T) {
5050
// Disable GC for the duration of the test.
5151
// This avoids a potential GC deadlock when spinning in uninterruptable ASM below #49695.
5252
defer debug.SetGCPercent(debug.SetGCPercent(-1))
53-
// And finish any pending GC after we pause, if any.
53+
// SetGCPercent waits until the mark phase is over, but the runtime
54+
// also preempts at the start of the sweep phase, so make sure that's
55+
// done too. See #49695.
5456
runtime.GC()
5557

5658
// Temporarily rewind the stack and trigger SIGSETXID

src/runtime/debug_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ func startDebugCallWorker(t *testing.T) (g *runtime.G, after func()) {
3434
skipUnderDebugger(t)
3535

3636
// This can deadlock if there aren't enough threads or if a GC
37-
// tries to interrupt an atomic loop (see issue #10958). A GC
38-
// could also actively be in progress (see issue #49370), so we
39-
// need to call runtime.GC to block until it has complete. We
40-
// use 8 Ps so there's room for the debug call worker,
37+
// tries to interrupt an atomic loop (see issue #10958). Execute
38+
// an extra GC to ensure even the sweep phase is done (out of
39+
// caution to prevent #49370 from happening).
40+
// TODO(mknyszek): This extra GC cycle is likely unnecessary
41+
// because preemption (which may happen during the sweep phase)
42+
// isn't much of an issue anymore thanks to asynchronous preemption.
43+
// The biggest risk is having a write barrier in the debug call
44+
// injection test code fire, because it runs in a signal handler
45+
// and may not have a P.
46+
//
47+
// We use 8 Ps so there's room for the debug call worker,
4148
// something that's trying to preempt the call worker, and the
4249
// goroutine that's trying to stop the call worker.
4350
ogomaxprocs := runtime.GOMAXPROCS(8)
@@ -270,8 +277,14 @@ func TestDebugCallPanic(t *testing.T) {
270277
// progress. Wait until the current GC is done, and turn it off.
271278
//
272279
// See #10958 and #49370.
273-
runtime.GC()
274280
defer debug.SetGCPercent(debug.SetGCPercent(-1))
281+
// TODO(mknyszek): This extra GC cycle is likely unnecessary
282+
// because preemption (which may happen during the sweep phase)
283+
// isn't much of an issue anymore thanks to asynchronous preemption.
284+
// The biggest risk is having a write barrier in the debug call
285+
// injection test code fire, because it runs in a signal handler
286+
// and may not have a P.
287+
runtime.GC()
275288

276289
ready := make(chan *runtime.G)
277290
var stop uint32

src/runtime/proc_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ func TestGoroutineParallelism(t *testing.T) {
119119
// since the goroutines can't be stopped/preempted.
120120
// Disable GC for this test (see issue #10958).
121121
defer debug.SetGCPercent(debug.SetGCPercent(-1))
122-
// Now that GCs are disabled, block until any outstanding GCs
123-
// are also done.
122+
// SetGCPercent waits until the mark phase is over, but the runtime
123+
// also preempts at the start of the sweep phase, so make sure that's
124+
// done too. See #45867.
124125
runtime.GC()
125126
for try := 0; try < N; try++ {
126127
done := make(chan bool)
@@ -166,8 +167,9 @@ func testGoroutineParallelism2(t *testing.T, load, netpoll bool) {
166167
// since the goroutines can't be stopped/preempted.
167168
// Disable GC for this test (see issue #10958).
168169
defer debug.SetGCPercent(debug.SetGCPercent(-1))
169-
// Now that GCs are disabled, block until any outstanding GCs
170-
// are also done.
170+
// SetGCPercent waits until the mark phase is over, but the runtime
171+
// also preempts at the start of the sweep phase, so make sure that's
172+
// done too. See #45867.
171173
runtime.GC()
172174
for try := 0; try < N; try++ {
173175
if load {
@@ -629,8 +631,9 @@ func TestSchedLocalQueueEmpty(t *testing.T) {
629631
// If runtime triggers a forced GC during this test then it will deadlock,
630632
// since the goroutines can't be stopped/preempted during spin wait.
631633
defer debug.SetGCPercent(debug.SetGCPercent(-1))
632-
// Now that GCs are disabled, block until any outstanding GCs
633-
// are also done.
634+
// SetGCPercent waits until the mark phase is over, but the runtime
635+
// also preempts at the start of the sweep phase, so make sure that's
636+
// done too. See #45867.
634637
runtime.GC()
635638

636639
iters := int(1e5)

src/runtime/rwmutex_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func TestParallelRWMutexReaders(t *testing.T) {
5555
// since the goroutines can't be stopped/preempted.
5656
// Disable GC for this test (see issue #10958).
5757
defer debug.SetGCPercent(debug.SetGCPercent(-1))
58-
// Finish any in-progress GCs and get ourselves to a clean slate.
58+
// SetGCPercent waits until the mark phase is over, but the runtime
59+
// also preempts at the start of the sweep phase, so make sure that's
60+
// done too.
5961
GC()
6062

6163
doTestParallelReaders(1)

src/runtime/testdata/testprog/preempt.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func AsyncPreempt() {
2121
// Disable GC so we have complete control of what we're testing.
2222
debug.SetGCPercent(-1)
2323
// Out of an abundance of caution, also make sure that there are
24-
// no GCs actively in progress.
24+
// no GCs actively in progress. The sweep phase of a GC cycle
25+
// for instance tries to preempt Ps at the very beginning.
2526
runtime.GC()
2627

2728
// Start a goroutine with no sync safe-points.

0 commit comments

Comments
 (0)