From f40560ff89eac34709f5fee56ebf71bfb2c4065c Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Wed, 18 Feb 2026 11:33:56 -0500 Subject: runtime: tell the race detector about synchronization on mainInitDone CL 743940 puts an atomic bool initialization-done channel, which is a nice optimization. In race mode, however, the atomic bool does not have race instrumentation, so the race detector doesn't know the happens-before edge between the initialization and the cgo callback. To tell the race detector about this synchronization, just use the channel unconditionally in race mode. Channel operations have the proper race instrumentation. Alternatively, we could explicitly instrument the atomic boolean operations. TODO: write a test. Change-Id: I466b20a46cd39d2bbe2149a9009e1a01b15891e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/746581 Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Knyszek --- src/runtime/cgocall.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/runtime') diff --git a/src/runtime/cgocall.go b/src/runtime/cgocall.go index 7f05c13aee..626f7edf01 100644 --- a/src/runtime/cgocall.go +++ b/src/runtime/cgocall.go @@ -428,7 +428,10 @@ func cgocallbackg1(fn, frame unsafe.Pointer, ctxt uintptr) { // // We check a bool first for speed, and wait on a channel // if it's not ready. - if !mainInitDone.Load() { + // + // In race mode, skip the optimization and always use the + // channel, which has the race instrumentation. + if raceenabled || !mainInitDone.Load() { <-mainInitDoneChan } } -- cgit v1.3