sync: fix ordering in Once and ManyTimes -- store count after f() completes#27490
Merged
Conversation
…efore 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 vlang#27456. Co-Authored-By: WOZCODE <contact@withwoz.com>
…before The same ordering bug fixed in sync.Once (see vlang#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>
Collaborator
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
Collaborator
|
Bouncing to pick up latest changes at master... |
Contributor
Author
|
Confirmed CIs failures not related to this PR |
This was referenced Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both
sync.Onceandsync.ManyTimeshad the same ordering bug:do_slowincrementedcount(or setdone) before callingf(), so a concurrent goroutine could observe the updated count on the atomic fast path indo()and return whilef()was still executing.Once: movedone = 1store to afterf()returnsManyTimes: movestdatomic.store_u64to afterf()returnsFixes: #27489
Test plan
test_once_ordering/test_once_with_param_orderinginvlib/sync/once_test.vandonce_with_param_test.vtest_many_times_orderingadded tovlib/sync/many_times_test.v— a concurrency ordering test: a slowf()signals on a channel then sleeps 50 ms before setting a value; a second concurrentdo()must not return untilf()has completedvlib/sync/passvfmtclean on all changed files🧙 Built with WOZCODE