From 6f7c9ea855b1ae97871837abf33b85aaa6731eae Mon Sep 17 00:00:00 2001 From: Leonard Wang Date: Sun, 24 Apr 2022 12:14:13 +0800 Subject: [PATCH] runtime: set userForced while holding gcsema Fixes #52515 Change-Id: If10068a97733daf61aba72da5ed3935db8eed8b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/401776 Reviewed-by: hopehook Run-TryBot: hopehook Reviewed-by: Michael Knyszek Auto-Submit: Jenny Rakoczy Run-TryBot: Michael Knyszek Reviewed-by: Jenny Rakoczy TryBot-Result: Gopher Robot --- src/runtime/mgc.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/runtime/mgc.go b/src/runtime/mgc.go index 3243a15b4d..f7e54d3dd3 100644 --- a/src/runtime/mgc.go +++ b/src/runtime/mgc.go @@ -612,9 +612,6 @@ func gcStart(trigger gcTrigger) { return } - // For stats, check if this GC was forced by the user. - work.userForced = trigger.kind == gcTriggerCycle - // In gcstoptheworld debug mode, upgrade the mode accordingly. // We do this after re-checking the transition condition so // that multiple goroutines that detect the heap trigger don't @@ -630,6 +627,10 @@ func gcStart(trigger gcTrigger) { semacquire(&gcsema) semacquire(&worldsema) + // For stats, check if this GC was forced by the user. + // Update it under gcsema to avoid gctrace getting wrong values. + work.userForced = trigger.kind == gcTriggerCycle + if trace.enabled { traceGCStart() } -- 2.50.0