From 3b5637ff2bd5c03479780995e7a35c48222157c1 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 11 May 2017 15:28:39 -0400 Subject: [PATCH] runtime: doubly fix "double wakeup" panic runtime.gchelper depends on the non-atomic load of work.ndone happening strictly before the atomic add of work.nwait. Until very recently (commit 978af9c2db, fixing #20334), the compiler reordered these operations. This created a race since work.ndone can change as soon as work.nwait is equal to work.ndone. If that happened, more than one gchelper could attempt to wake up the work.alldone note, causing a "double wakeup" panic. This was fixed in the compiler, but to make this code less subtle, make the load of work.ndone atomic. This clearly forces the order of these operations, ensuring the race doesn't happen. Fixes #19305 (though really 978af9c2db fixed it). Change-Id: Ieb1a84e1e5044c33ac612c8a5ab6297e7db4c57d Reviewed-on: https://go-review.googlesource.com/43311 Run-TryBot: Austin Clements TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/runtime/mgc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 5dc417038a..22e8c31317 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -2105,7 +2105,7 @@ func gchelper() { traceGCScanDone() } - nproc := work.nproc // work.nproc can change right after we increment work.ndone + nproc := atomic.Load(&work.nproc) // work.nproc can change right after we increment work.ndone if atomic.Xadd(&work.ndone, +1) == nproc-1 { notewakeup(&work.alldone) } -- 2.50.0