fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically #2012
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2012
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:fix/os-spin-always-once"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
spin_begin(host/nn_os_sync.c, x86-64: the spin before an event, queue or mutex wait sleeps) cached theHOS_SPIN_ALWAYSswitch in a function-static plainint, -1 until the first spin. Every thread that spun for the firsttime read it and wrote it with plain accesses, so two guest threads first spinning at once (on separate mutexes, say)
raced on it: a C data race, even though both stored the same value. This reads the cache with an atomic load; while it
is -1, the first spinner reads the switch under a lock (
spin_adaptive_lock) and stores it, and spinners that came atthe same time wait on the lock for that value. The spin and the switch work as before.
How to see it on
main:python -I -B tests/test_spin_always_once.pywith the test commit: its race case starts 8threads spinning for the first time at once (the fixture's
getenvholds each read until all have come), and the switchis read 8 times, each thread storing the flag.
Found by BeanBot's review of #1953 (Blocking 5,
inline 61951), which #1953 left on
mainfor a separate change.
Read first:
spin_begininhost/nn_os_sync.c;tests/test_spin_always_once.py.Overlap: #1953 (
perf/mutex-lock-wait-cold@8443bba1) calls this spin from its newmutex_lock_waitbut does not changespin_begin; the two merge without a conflict (checked withgit merge-tree), in either order.Testing
Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This has
NOT run in the game on this PC (Still to test). Each check ran in a hidden console of its own, in one build job of the
PC's job queue (fb3-sep-1), on this branch on
main@f1f2dc5b, heada59b3698:python -I -B tests/test_spin_always_once.py: "PASS: every case" (unset,0,off: 2 of 64 waits on an object thatkeeps missing spin;
1,yes: 64 of 64; the variable changed after the first spin, unset to1and1to0:no change; 8 threads spinning for the first time at once, unset and
1), the switch read once in each. At the testcommit
d20dcf59(main'sspin_begin) it fails in both race cases, "the switch read 8 times (expected once)"; theother seven pass there.
host/nn_os_sync.ccompiled (-fsyntax-only) with the host build's own flags, with and without Tracy: no error.python -I -B tests/run_all.py --strict --changed origin/main(the checks that read the changed files): 12 passed,0 failed, 1 skipped (
test_tracy_funcs.py: Linux only),test_cv_handoff_threads.py,test_spin_by_role.py,test_event_handoff.pyandtest_multiwait_wake.pyamong them.Still to test:
main,with
HOS_SPIN_ALWAYSunset and once with it=1, expecting no stall and frame times within the runs' spread (by hand)python3 -I -B tests/test_spin_always_once.py, expecting it to pass (by hand)this PR against
main, expecting no stall and frame times within the runs' spread (the Test Farm: play Castle Town'smarket for two minutes on Linux with an AMD CPU, a PC with 6 or fewer logical processors and the Steam Deck, this PR
against main, comparing frame times and looking for a stall)
Measurements
Nothing to measure: after the first spin the cost is the same one load and test as before (now an atomic load, the same instruction on x86-64); the lock is taken only while the switch has not been read.
Risks
The first spins of a run may wait on the lock while one thread reads the switch (a
getenv, once). If the lock werenever released the waits would stall, so a stall right after start would point here. No switch of its own:
HOS_SPIN_ALWAYSkeeps its meaning; a revert gives backmain's code. Players notice nothing after updating.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Recommendation: Request changes. The implementation removes the first-use race and confines lock contention to initialization. The regression test still passes if the unlocked cache load becomes a plain read, which would bring the race back.
Concurrent x86-64 waits now agree on one cached HOS_SPIN_ALWAYS value. The fix keeps the existing spin policy and adds one process-wide lock that can briefly serialize unrelated waits at first use.
@fixwill do (7)tests/test_spin_always_once.py:149: Replacing the outer atomic load in spin_begin with a plain load leaves the lock and second check intact. (more on the line)tests/test_spin_always_once.py:77: The start barrier only proves that workers reached the barrier. (more on the line)tests/test_spin_always_once.py:136: The race fixture ignores thread creation and wait errors. (more on the line)tests/test_spin_always_once.py:99: The repeat case ignores both environment setter return values. (more on the line)tests/test_spin_always_once.py:43: The standalone command in the PR misses a supported Windows setup where llvm-mingw is in the clone and neither LONGSHOT_MINGW nor PATH points to it. (more on the line)host/nn_os_sync.c:467: If launch timing is inconclusive, add a test-only timer around the cold lock acquisition so it reports time spent waiting before the first spin.For the author (2) (a run, a measurement or a decision)
host/nn_os_sync.c:467: The required author-PC game run was pending when the PR opened. (more on the line)Checks: Still running:
CI / Conventional Commit title (pull_request),CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request),CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request),CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request),CI / Workflow lint (actionlint) (pull_request),CI / Website build and checks (pull_request),Guide screenshots / Guide images and local UI source review (pull_request),Third-party parts / As their notes say (pull_request),Third-party parts / The same as upstream (pull_request),PR description / Description follows the template (pull_request_target).More: does it help, other systems, switches, what was read
Does it help: Yes for the reported duplicate initialization. On Windows 11 with llvm-mingw Clang 23.1.5, both eight-thread cases read the switch eight times at
d20dcf59, which uses main's spin_begin, and once ata59b3698. All seven other cases passed on both. In-game startup and frame pacing remain unmeasured.Other systems: Contended mutexes, blocking event and system-event waits, semaphore acquires, and blocking queue receives share the cold lock. That includes waits used by audio and video. The callers reach it before taking the object's host lock, and the initializer performs no virtual-core checkpoint. Ready and try paths, timed semaphore acquire, WaitAny, and SVC arbitration bypass the new lock. GPU vendors and drivers can see indirect effects through guest thread pacing. The diff touches neither renderer code nor save, input, audio-processing, or build logic.
Switches: HOS_SPIN_ALWAYS keeps its existing default. Unset or false uses adaptive back-off, spinning once in 32 waits on an object that keeps missing. True disables the back-off. The default and switch-table row still match the code. No switches added or removed.
Reviewed:
a59b369841againstf1f2dc5bd2: Read both changed files in full, both commits, AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, and the Forgejo template. Read the relevant caller, environment parser, Linux SRW-lock shim, Tracy wrapper, virtual-core scheduler, and test-runner sections. Skimmed CI definitions and the existing tests that read the changed code. The supplied description is the only run evidence. docs/review-checklist.md and the sibling Tooling checkout were absent. No recorded runs, translations, or generated files changed.; nothing run (a code review).Checked and fine:
Testing: Read-only review. The author reports nine fixture cases passing at
a59b3698, Windows syntax checks with and without Tracy, and 12 changed-file checks passing with one Linux-only skip. The supplied CI checks are still running. Linux build and fixture results, the full suite, and game runs are pending.Overlaps and merge order: #1953 adds a caller of spin_begin on its new mutex wait path. The author reports a clean merge in either order. Its cited commit is absent from this checkout, so the merge order was not independently verified.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -461,1 +465,3 @@if (adaptive < 0) adaptive = !env_on("HOS_SPIN_ALWAYS");int adaptive = __atomic_load_n(&spin_adaptive, __ATOMIC_RELAXED);if (adaptive < 0) {AcquireSRWLockExclusive(&spin_adaptive_lock);L7 (minor): The required author-PC game run was pending when the PR opened. The planned Market run can also miss the only lock contention if timing starts after the switch is cached. Compare from process launch on the author's Windows PC with the switch unset and set to 1, then report the matched Market frame times and the affected Tier 1 Windows and Linux coverage, including Steam Deck.
@ -0,0 +40,4 @@for c in (given, os.environ.get("CC")):if c:return shutil.which(c) or cmingw = os.environ.get("LONGSHOT_MINGW", "") if os.name == "nt" else ""L5 (minor): The standalone command in the PR misses a supported Windows setup where llvm-mingw is in the clone and neither LONGSHOT_MINGW nor PATH points to it. The game build and test runner discover that compiler, but direct invocation fails or picks a different Clang. Use the clone's bundled compiler as the Windows fallback before searching PATH, preserving --cc and CC precedence.
@ -0,0 +74,4 @@if (!strcmp(name, "HOS_SPIN_ALWAYS")) {const long k = __atomic_add_fetch(&switch_reads, 1, __ATOMIC_SEQ_CST);const uint64_t t0 = now_ms();while (k <= threads_n && __atomic_load_n(&switch_reads, __ATOMIC_SEQ_CST) < threads_n && now_ms() - t0 < 300) {}L2 (minor): The start barrier only proves that workers reached the barrier. If a heavily loaded runner delays all but one worker for 300 ms after that point, even the base can publish before a second worker reaches getenv and pass the race case. Have the fixture observe each worker reaching either the cold lock acquisition or getenv before releasing the first read. Report a missed rendezvous as a fixture failure.
@ -0,0 +96,4 @@#ifdef _WIN32char b[64];snprintf(b, sizeof b, "HOS_SPIN_ALWAYS=%s", v);_putenv(b);L4 (minor): The repeat case ignores both environment setter return values. If setenv fails to allocate, the old setting remains and the case can pass without testing a setting change. Check the setter result and fail the fixture if the update failed.
@ -0,0 +133,4 @@threads_n = N;#ifdef _WIN32HANDLE t[N];for (int i = 0; i < N; i++) t[i] = CreateThread(NULL, 0, racer, (void *)(intptr_t)i, 0, NULL);L3 (minor): The race fixture ignores thread creation and wait errors. If a Linux runner hits its thread limit, it can join an uninitialized pthread_t while the started workers wait for eight arrivals. On Windows, a null handle makes the wait fail and the main thread can read results while workers are alive. Check creation and join or wait results, fail with a clear diagnostic on setup failure, and close the Windows handles after joining.
@ -0,0 +146,4 @@fprintf(stderr, "unknown mode %s\n", mode);return 2;}if (switch_reads != 1) {L1: Replacing the outer atomic load in spin_begin with a plain load leaves the lock and second check intact. The fixture can still see one getenv call and the expected spin counts even though the plain read races with publication. Keep the cross-platform behavior cases and add a race-detection check, such as a Linux ThreadSanitizer run. Show that reverting the outer load to a plain read makes that check fail.
@fix
I changed this pull request where an edit could (the rest needs runs, measurements or decisions): the new head is
9034d73c42. I rebased it ontomain(2b3de5b27a).Changed
Changed in part (the rest is for the author)
python -I -B tests/test_spin_always_once.py --tsan; CI files cannot be changed under this request. No tests were run.Left for the author
f1f2dc5bwith test-only commitd20dcf59applied and aftera59b3698; mark startup and frame-time results as pending. The description is outside the checkout.f1f2dc5bwith test-only commitd20dcf59applied, and mark startup and frame-time measurements as pending. The description is not available to edit in the checkout.Still for the author (no edit does these)
tests/test_spin_always_once.py:272: The control is implemented. Record a Linux run where the atomic source passes and changing just the outer load to a plain read produces the expected race report on spin_adaptive.host/nn_os_sync.c:467: The new timer reports individual lock acquisitions. Run the matched Windows comparisons from process launch with HOS_SPIN_ALWAYS unset and set to 1, record Market frame times, and report affected Tier 1 coverage. Use normal builds for the comparison and the timer to investigate any startup delay.Nothing was built or run: check the change. It will be reviewed again.
Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
a59b3698419034d73c42Recommendation: Wait. The x86-64 fix makes every cache access atomic and serializes the first read of HOS_SPIN_ALWAYS. The updated race check still needs a Linux ThreadSanitizer result, and the startup, in-game, and full-suite results are pending.
Concurrent first waits now share one switch read. The usual spin path still uses one cache load and test, while concurrent cold callers can briefly wait on the new lock. The fixture now observes every worker reaching the cold path and handles setup failures on Windows and Linux.
@fixwill do (1)For the author (3) (a run, a measurement or a decision)
tests/test_spin_always_once.py:277: The sanitizer check and plain-load control are implemented, but the supplied Windows run predates both. Record a Linux x86-64 --tsan run showing a clean atomic case and a data race on spin_adaptive in the control.host/nn_os_sync.c:467: A Market capture that begins after startup misses the cold lock. Compare from process launch on the author's Windows PC with HOS_SPIN_ALWAYS unset and set to 1. Report matched Market frame times and affected Tier 1 Windows and Linux results, including Steam Deck. Use the new timer in a separate diagnostic build if the launch comparison cannot isolate the wait.Since the last review (on
a59b369841)Resolved: L2 (The start barrier only proves that workers reached the barrier. If a heavily loaded runner…); L3 (The race fixture ignores thread creation and wait errors. If a Linux runner hits its threa…); L4 (The repeat case ignores both environment setter return values. If setenv fails to allocate…); L5 (The standalone command in the PR misses a supported Windows setup where llvm-mingw is in t…)
Still open: L1 (The sanitizer check and plain-load control are implemented, but the supplied Windows run p…); L6 (Measurements says there is nothing to measure, but the standards call for before-and-after…); L7 (A Market capture that begins after startup misses the cold lock. Compare from process laun…); L8 (The reported changed-file run covers 12 checks and skips one. The full suite and Linux hos…)
Checks: Still running:
PR description / Description follows the template (pull_request_target),CI / Website build and checks (pull_request),Third-party parts / As their notes say (pull_request),CI / Conventional Commit title (pull_request),CI / Workflow lint (actionlint) (pull_request),Guide screenshots / Guide images and local UI source review (pull_request).More: does it help, other systems, switches, what was read
Does it help: Yes for the reproduced duplicate read. The earlier Windows fixture reported 8 reads with the test-only commit
d20dcf59and 1 with the fix ata59b3698in both race cases, with the switch unset and set to 1. The updated fixture and sanitizer control have no reported results at9034d73c. Startup and frame-time costs remain unmeasured.Other systems: The shared x86-64 path serves contended mutex, event, and queue waits on Windows and Linux, including Steam Deck. The new lock is taken before the callers acquire their wait locks. Non-x86 and Emscripten use the existing no-op spin helpers. GPU vendors share the same host path, so driver and hardware coverage depends on the pending Tier 1 runs.
Switches: HOS_SPIN_ALWAYS still defaults to adaptive spinning and keeps its existing switch-table entry. HOS_TEST_SPIN_LOCK_TIME is a compile-time diagnostic, undefined by default, which keeps clock reads and logging out of shipping builds. The fixture's --tsan option is off by default and requires Linux x86-64 and a working sanitizer runtime.
Reviewed:
9034d73c42against2b3de5b27a: Read the complete base-to-HEAD diff, the fix commit, tests/test_spin_always_once.py in full, and the changed section of host/nn_os_sync.c with its mutex, event, and queue callers. Read AGENTS.md, CONTRIBUTING.md, the Forgejo PR template, and docs/dev/standards.md. Checked the relevant POSIX lock shim, environment helper, source extraction, switch-table check, and test runner discovery. The rest of the host source and test runner were outside the re-check. No local review checklist or adjacent Tooling checkout was found. Test results were available as summaries in the PR description.; nothing run (a code review).Checked and fine:
Testing: Read-only review. The PR reports nine fixture cases passing, Windows syntax checks with and without Tracy, and 12 changed-file checks passing with one Linux-only skip on
a59b3698. Results are still needed for the updated fixture and sanitizer control, full suite, Linux host build, and matched startup and in-game runs. Six CI checks are pending with no head-only failures reported.Overlaps and merge order: #1953 adds a caller to the same spin path. The PR reports a conflict-free merge-tree check in both orders at the cited sibling revision. Neither change needs to merge first.
Automated code review by BeanBot (gpt-6.2-sol), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.
WIP: fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomicallyto fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically