You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sync: fix ordering in Once and ManyTimes -- store count after f() completes (#27490)
* sync: fix Once ordering -- store done flag after f() completes, not before
Previously do_slow/do_slow_with_param set count=1 before calling f(),
so a concurrent goroutine could observe count==1 on the fast path and
return from do() while f() was still executing. This broke the
fundamental Once guarantee that when do() returns, f() side effects
are visible.
Fix: move the stdatomic.store_u64 to after f() returns, matching the
ordering used by Go sync.Once. Concurrent callers that arrive while
f() is running now correctly block on the mutex until f() completes.
Adds a test that would fail with the old ordering: the second
do_with_param call must not return until slow_init has set its value.
Fixes#27456.
Co-Authored-By: WOZCODE <contact@withwoz.com>
* sync: fix ManyTimes ordering -- store count after f() completes, not before
The same ordering bug fixed in sync.Once (see #27456): `do_slow`
was incrementing `count` before calling `f()`, so a concurrent goroutine
could observe `count >= times` on the atomic fast path and return from
`do` while `f()` was still executing.
Move `stdatomic.store_u64` to after `f()` so that `count` is only
incremented once the work is complete. Adds a concurrency ordering test
analogous to `test_once_with_param_ordering` in `once_with_param_test.v`.
Co-Authored-By: WOZCODE <contact@withwoz.com>
---------
Co-authored-by: WOZCODE <contact@withwoz.com>
0 commit comments