fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically #2012

Merged
HumanBean merged 3 commits from siantm/Longshot:fix/os-spin-always-once into main 2026-11-21 05:51:28 +00:00
Contributor

Summary

spin_begin (host/nn_os_sync.c, x86-64: the spin before an event, queue or mutex wait sleeps) cached the
HOS_SPIN_ALWAYS switch in a function-static plain int, -1 until the first spin. Every thread that spun for the first
time 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 at
the 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.py with the test commit: its race case starts 8
threads spinning for the first time at once (the fixture's getenv holds each read until all have come), and the switch
is read 8 times, each thread storing the flag.

Found by BeanBot's review of #1953 (Blocking 5,
inline 61951), which #1953 left on main
for a separate change.

Read first: spin_begin in host/nn_os_sync.c; tests/test_spin_always_once.py.

Overlap: #1953 (perf/mutex-lock-wait-cold @ 8443bba1) calls this spin from its new mutex_lock_wait but does not change
spin_begin; the two merge without a conflict (checked with git 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, head a59b3698:

  • python -I -B tests/test_spin_always_once.py: "PASS: every case" (unset, 0, off: 2 of 64 waits on an object that
    keeps missing spin; 1, yes: 64 of 64; the variable changed after the first spin, unset to 1 and 1 to 0:
    no change; 8 threads spinning for the first time at once, unset and 1), the switch read once in each. At the test
    commit d20dcf59 (main's spin_begin) it fails in both race cases, "the switch read 8 times (expected once)"; the
    other seven pass there.
  • host/nn_os_sync.c compiled (-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.py and test_multiwait_wake.py among them.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market, two minutes, this PR against main,
    with HOS_SPIN_ALWAYS unset and once with it =1, expecting no stall and frame times within the runs' spread (by hand)
  • Linux: build the host and run python3 -I -B tests/test_spin_always_once.py, expecting it to pass (by hand)
  • Linux with an AMD CPU, a PC with 6 or fewer logical processors and the Steam Deck: Castle Town's market, two minutes,
    this PR against main, expecting no stall and frame times within the runs' spread (the Test Farm: play Castle Town's
    market 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 were
never released the waits would stall, so a stall right after start would point here. No switch of its own:
HOS_SPIN_ALWAYS keeps its meaning; a revert gives back main's code. Players notice nothing after updating.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A fix: a game-free test that fails on main and passes here.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.
## Summary `spin_begin` (host/nn_os_sync.c, x86-64: the spin before an event, queue or mutex wait sleeps) cached the `HOS_SPIN_ALWAYS` switch in a function-static plain `int`, -1 until the first spin. Every thread that spun for the first time 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 at the 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.py` with the test commit: its race case starts 8 threads spinning for the first time at once (the fixture's `getenv` holds each read until all have come), and the switch is read 8 times, each thread storing the flag. Found by BeanBot's review of #1953 ([Blocking 5](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61957), [inline 61951](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61951)), which #1953 left on `main` for a separate change. Read first: `spin_begin` in `host/nn_os_sync.c`; `tests/test_spin_always_once.py`. Overlap: #1953 (`perf/mutex-lock-wait-cold` @ 8443bba1) calls this spin from its new `mutex_lock_wait` but does not change `spin_begin`; the two merge without a conflict (checked with `git 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, head a59b3698: - `python -I -B tests/test_spin_always_once.py`: "PASS: every case" (unset, `0`, `off`: 2 of 64 waits on an object that keeps missing spin; `1`, `yes`: 64 of 64; the variable changed after the first spin, unset to `1` and `1` to `0`: no change; 8 threads spinning for the first time at once, unset and `1`), the switch read once in each. At the test commit d20dcf59 (`main`'s `spin_begin`) it fails in both race cases, "the switch read 8 times (expected once)"; the other seven pass there. - `host/nn_os_sync.c` compiled (`-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.py` and `test_multiwait_wake.py` among them. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market, two minutes, this PR against `main`, with `HOS_SPIN_ALWAYS` unset and once with it `=1`, expecting no stall and frame times within the runs' spread (by hand) - Linux: build the host and run `python3 -I -B tests/test_spin_always_once.py`, expecting it to pass (by hand) - Linux with an AMD CPU, a PC with 6 or fewer logical processors and the Steam Deck: Castle Town's market, two minutes, this PR against `main`, expecting no stall and frame times within the runs' spread (the Test Farm: play Castle Town's market 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 were never released the waits would stall, so a stall right after start would point here. No switch of its own: `HOS_SPIN_ALWAYS` keeps its meaning; a revert gives back `main`'s code. Players notice nothing after updating. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `main` is named). - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A fix: a game-free test that fails on `main` and passes here. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold.
tests/test_spin_always_once.py runs host/nn_os_sync.c's spin_begin with longshot_env.h's env_on, one child process per
case with the switch set in its environment: unset, 0 and off adaptive (2 of 64 waits on an object that keeps missing
spin), 1 and yes always (64 of 64), the variable changed after the first spin, and 8 threads spinning for the first
time at once. The switch must be read once in every case. On main spin_begin caches it in a plain static int that each
first spinner reads and writes: the race case reads it 8 times.

Assisted-by: Claude Code
fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
a59b369841
spin_begin cached the switch in a function-static int, -1 until the first spin, which every thread spinning for the
first time read and wrote with plain accesses: two x86-64 workers first spinning on separate objects at once raced on
it (both storing the same value, but a C data race all the same). The cache is now read with an atomic load; while it
is -1 the first spinner reads the switch under spin_adaptive_lock and stores it, and spinners that came at the same
time wait on the lock for that value. The spin's behaviour and the switch's meaning are unchanged.

Assisted-by: Claude Code
BeanBot left a comment

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.

@fix will do (7)

  1. L1 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)
  2. L2 (minor) tests/test_spin_always_once.py:77: The start barrier only proves that workers reached the barrier. (more on the line)
  3. L3 (minor) tests/test_spin_always_once.py:136: The race fixture ignores thread creation and wait errors. (more on the line)
  4. L4 (minor) tests/test_spin_always_once.py:99: The repeat case ignores both environment setter return values. (more on the line)
  5. L5 (minor) 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)
  6. L6 (minor): Measurements says there is nothing to measure, but the standards call for before-and-after proof for a fix. Put the reported eight-to-one result in the table, identify the base with the test-only commit applied, and distinguish it from the pending startup and frame-time results.
  7. L7 (its part an edit can do) 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)

  • L7 (minor) host/nn_os_sync.c:467: The required author-PC game run was pending when the PR opened. (more on the line)
  • L8 (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready.

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 at a59b3698. 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: a59b369841 against f1f2dc5bd2: 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:

  • The cache holds a single scalar. Relaxed atomic accesses suffice, and the lock protects the second check and the only store.
  • The process-lifetime lock needs no per-thread allocation. The steady path takes no lock, and the existing no-spin guard remains ahead of initialization.
  • The test compiles the real source cut, controls HOS_SPIN_ALWAYS per child, and covers both switch settings. Its compiler and temporary child processes match the described test.
  • Both commits carry Assisted-by trailers and keep the test before the fix. The title follows the Conventional Commit format after stripping WIP. The diff contains no game-derived assets, generated output, third-party changes, or renderer API changes.

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.

**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. **`@fix` will do (7)** 1. **L1** `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) 2. **L2** (minor) `tests/test_spin_always_once.py:77`: The start barrier only proves that workers reached the barrier. (more on the line) 3. **L3** (minor) `tests/test_spin_always_once.py:136`: The race fixture ignores thread creation and wait errors. (more on the line) 4. **L4** (minor) `tests/test_spin_always_once.py:99`: The repeat case ignores both environment setter return values. (more on the line) 5. **L5** (minor) `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) 6. **L6** (minor): Measurements says there is nothing to measure, but the standards call for before-and-after proof for a fix. Put the reported eight-to-one result in the table, identify the base with the test-only commit applied, and distinguish it from the pending startup and frame-time results. 7. **L7** (its part an edit can do) `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) - **L7** (minor) `host/nn_os_sync.c:467`: The required author-PC game run was pending when the PR opened. (more on the line) - **L8** (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready. **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)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **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 at a59b3698. 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:** a59b369841e1 against f1f2dc5bd282: 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:** - The cache holds a single scalar. Relaxed atomic accesses suffice, and the lock protects the second check and the only store. - The process-lifetime lock needs no per-thread allocation. The steady path takes no lock, and the existing no-spin guard remains ahead of initialization. - The test compiles the real source cut, controls HOS_SPIN_ALWAYS per child, and covers both switch settings. Its compiler and temporary child processes match the described test. - Both commits carry Assisted-by trailers and keep the test before the fix. The title follows the Conventional Commit format after stripping WIP. The diff contains no game-derived assets, generated output, third-party changes, or renderer API changes. **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. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -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);
Member

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.

**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 c
mingw = os.environ.get("LONGSHOT_MINGW", "") if os.name == "nt" else ""
Member

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.

**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) {}
Member

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.

**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 _WIN32
char b[64];
snprintf(b, sizeof b, "HOS_SPIN_ALWAYS=%s", v);
_putenv(b);
Member

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.

**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 _WIN32
HANDLE t[N];
for (int i = 0; i < N; i++) t[i] = CreateThread(NULL, 0, racer, (void *)(intptr_t)i, 0, NULL);
Member

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.

**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) {
Member

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.

**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.
Owner
@fix
Member

I changed this pull request where an edit could (the rest needs runs, measurements or decisions): the new head is 9034d73c42. I rebased it onto main (2b3de5b27a).

Changed

  • Race detection: tests/test_spin_always_once.py:272: Added Linux ThreadSanitizer coverage and a plain-load control that must report a race on spin_adaptive. The check was not run, as instructed.
  • Cold-path rendezvous: tests/test_spin_always_once.py:73: Count each worker at the cold lock or getenv and fail if all workers do not arrive before the deadline.
  • Thread setup and cleanup: tests/test_spin_always_once.py:162: Check thread creation and waits, release started workers on setup failure, and close joined Windows handles.
  • Environment updates: tests/test_spin_always_once.py:119: Check the platform setter result and fail the repeat case if the update fails.
  • Windows compiler discovery: tests/test_spin_always_once.py:45: Check the clone's llvm-mingw after LONGSHOT_MINGW and before PATH, preserving --cc and CC precedence.

Changed in part (the rest is for the author)

  • Cold-lock timing: host/nn_os_sync.c:467: Added the opt-in HOS_TEST_SPIN_LOCK_TIME compile-time timer, which logs each cold lock wait in nanoseconds before the thread spins. The author must compare process-launch timing on the Windows PC with HOS_SPIN_ALWAYS unset and set to 1, use the timer if launch timing is inconclusive, and report matched Market frame times and Tier 1 Windows and Linux coverage including Steam Deck.
  • Portable spin fixture: tests/test_spin_always_once.py:18 keeps the default fixture portable and adds a strict --tsan invocation for Linux x86-64, including the plain-load control. The author must configure a validation job with a provisioned ThreadSanitizer toolchain to run 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

  • PR measurements description: Update the PR description with the reported eight-to-one getenv reads, identifying baseline f1f2dc5b with test-only commit d20dcf59 applied and after a59b3698; mark startup and frame-time results as pending. The description is outside the checkout.
  • Measurements description: Update the PR description: report eight switch reads before versus one after, identify the baseline as main at f1f2dc5b with test-only commit d20dcf59 applied, and mark startup and frame-time measurements as pending. The description is not available to edit in the checkout.
  • Testing and Risks description: Update the PR description to explain that --tsan runs the Linux x86-64 race and plain-load control and requires a working ThreadSanitizer compiler and runtime. State that HOS_TEST_SPIN_LOCK_TIME is undefined by default and, when defined, logs each cold lock acquisition before spinning. Identify sanitizer, startup and frame-time results that remain pending. The description is not available to edit in the checkout.

Still for the author (no edit does these)

  • L1 (minor) 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.
  • L7 (minor) 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.
  • L8 (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready.

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.

I changed this pull request where an edit could (the rest needs runs, measurements or decisions): the new head is 9034d73c42f6. I rebased it onto `main` (2b3de5b27a77). **Changed** - **Race detection: tests/test_spin_always_once.py:272**: Added Linux ThreadSanitizer coverage and a plain-load control that must report a race on spin_adaptive. The check was not run, as instructed. - **Cold-path rendezvous: tests/test_spin_always_once.py:73**: Count each worker at the cold lock or getenv and fail if all workers do not arrive before the deadline. - **Thread setup and cleanup: tests/test_spin_always_once.py:162**: Check thread creation and waits, release started workers on setup failure, and close joined Windows handles. - **Environment updates: tests/test_spin_always_once.py:119**: Check the platform setter result and fail the repeat case if the update fails. - **Windows compiler discovery: tests/test_spin_always_once.py:45**: Check the clone's llvm-mingw after LONGSHOT_MINGW and before PATH, preserving --cc and CC precedence. **Changed in part (the rest is for the author)** - **Cold-lock timing: host/nn_os_sync.c:467**: Added the opt-in HOS_TEST_SPIN_LOCK_TIME compile-time timer, which logs each cold lock wait in nanoseconds before the thread spins. The author must compare process-launch timing on the Windows PC with HOS_SPIN_ALWAYS unset and set to 1, use the timer if launch timing is inconclusive, and report matched Market frame times and Tier 1 Windows and Linux coverage including Steam Deck. - **Portable spin fixture**: tests/test_spin_always_once.py:18 keeps the default fixture portable and adds a strict --tsan invocation for Linux x86-64, including the plain-load control. The author must configure a validation job with a provisioned ThreadSanitizer toolchain to run `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** - **PR measurements description**: Update the PR description with the reported eight-to-one getenv reads, identifying baseline f1f2dc5b with test-only commit d20dcf59 applied and after a59b3698; mark startup and frame-time results as pending. The description is outside the checkout. - **Measurements description**: Update the PR description: report eight switch reads before versus one after, identify the baseline as main at f1f2dc5b with test-only commit d20dcf59 applied, and mark startup and frame-time measurements as pending. The description is not available to edit in the checkout. - **Testing and Risks description**: Update the PR description to explain that --tsan runs the Linux x86-64 race and plain-load control and requires a working ThreadSanitizer compiler and runtime. State that HOS_TEST_SPIN_LOCK_TIME is undefined by default and, when defined, logs each cold lock acquisition before spinning. Identify sanitizer, startup and frame-time results that remain pending. The description is not available to edit in the checkout. **Still for the author** (no edit does these) - **L1** (minor) `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. - **L7** (minor) `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. - **L8** (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready. Nothing was built or run: check the change. It will be reviewed again. --- <sub>Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.</sub>
BeanBot force-pushed fix/os-spin-always-once from a59b369841
Some checks failed
PR description / Description follows the template (pull_request_target) Successful in 4s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
to 9034d73c42
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 15s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 19s
CI / Workflow lint (actionlint) (pull_request) Successful in 21s
Third-party parts / As their notes say (pull_request) Successful in 24s
CI / Website build and checks (pull_request) Failing after 26s
PR description / Description follows the template (pull_request_target) Failing after 5s
2026-11-21 05:41:39 +00:00
Compare
BeanBot left a comment

Recommendation: 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.

@fix will do (1)

  1. L6 (minor): Measurements says there is nothing to measure, but the standards call for before-and-after proof for a fix. Put the reported eight-to-one result in the table, identify the base with the test-only commit applied, and distinguish it from the pending startup and frame-time results.

For the author (3) (a run, a measurement or a decision)

  • L1 (minor) 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.
  • L7 (minor) 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.
  • L8 (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready.

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 d20dcf59 and 1 with the fix at a59b3698 in both race cases, with the switch unset and set to 1. The updated fixture and sanitizer control have no reported results at 9034d73c. 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: 9034d73c42 against 2b3de5b27a: 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:

  • The relaxed atomics protect a standalone integer. The lock serializes initialization, and the flag publishes no other state.
  • The first-read lock is released before optional logging. The default build adds neither clock calls nor logging to the spin path.
  • The fixture retains both switch settings and both post-initialization environment changes. Its sanitizer run is opt-in, with no new package install or download.
  • The patch stays on one topic and contains no game files, build output, generated files, or third-party changes.

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.

**Recommendation: 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. **`@fix` will do (1)** 1. **L6** (minor): Measurements says there is nothing to measure, but the standards call for before-and-after proof for a fix. Put the reported eight-to-one result in the table, identify the base with the test-only commit applied, and distinguish it from the pending startup and frame-time results. **For the author (3)** (a run, a measurement or a decision) - **L1** (minor) `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. - **L7** (minor) `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. - **L8** (minor): The reported changed-file run covers 12 checks and skips one. The full suite and Linux host build and fixture results are still missing. Report the full suite, Linux build and fixture results, and any failures shared with the base before marking the PR ready. **Since the last review** (on a59b369841e1) 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)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Yes for the reproduced duplicate read. The earlier Windows fixture reported 8 reads with the test-only commit d20dcf59 and 1 with the fix at a59b3698 in both race cases, with the switch unset and set to 1. The updated fixture and sanitizer control have no reported results at 9034d73c. 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:** 9034d73c42f6 against 2b3de5b27a77: 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:** - The relaxed atomics protect a standalone integer. The lock serializes initialization, and the flag publishes no other state. - The first-read lock is released before optional logging. The default build adds neither clock calls nor logging to the spin path. - The fixture retains both switch settings and both post-initialization environment changes. Its sanitizer run is opt-in, with no new package install or download. - The patch stays on one topic and contains no game files, build output, generated files, or third-party changes. **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. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.</sub>
HumanBean changed title from WIP: fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically to fix(os): spin_begin reads HOS_SPIN_ALWAYS once, under a lock, and its cache atomically 2026-11-21 05:51:15 +00:00
HumanBean deleted branch fix/os-spin-always-once 2026-11-21 05:51:28 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2012
No description provided.