WIP: fix(os): HOS_CHAIN's flag is an atomic, and the recorder writes a record only once its note has filled it #2019

Draft
siantm wants to merge 2 commits from siantm/Longshot:fix/os-chain-flag-atomic into main
Contributor

Summary

HOS_CHAIN=<path> (host/hos_chain.c, a diagnostic for scripts/perf.py chain) records the guest threads' waits and
wake-ups between <path>.go appearing and going away. Its on/off flag, hos_chain_on, was a volatile int: the
recorder thread set and cleared it, a note at the buffer's capacity cleared it, and every wait and signal read it, with
no synchronisation, and nothing published the record buffer or the records to the thread that writes the file. At the
stop the recorder cleared the flag, slept 50 ms and wrote every record a note had taken, whether or not that note had
filled it in yet: a note whose thread the system had not run again for 50 ms left a record of zeros in the file.

This makes the flag an atomic: set with release once the buffer is allocated, read with acquire in hos_chain_note
before it uses the buffer, read relaxed by the HOS_CHAIN macro and the six waits in host/nn_os_sync.c that decide
whether to call it. A note stores its record's kind last, with release, and the recorder waits (acquire) for the kind of
every record taken before the stop, at most 5 s in all, instead of sleeping 50 ms; the thread table it names threads
from is stored and read atomically too. The result: the file holds only whole records. With HOS_CHAIN unset nothing
changes but the flag's load, which is the same instruction on x86-64.

How to see it on main: python -I -B tests/test_chain_recorder.py with the test commit: a note held after it has
taken its record while the recording stops leaves a record of zeros in the file (see Testing).

Found by BeanBot's review of #1953 (Blocking 4, volatile hos_chain_on),
which #1953 left on main for a separate change.

Read first: hos_chain_note, chain_write and chain_thread in host/hos_chain.c; tests/test_chain_recorder.py.

Overlap: #1953 (perf/mutex-lock-wait-cold @ 8443bba1) moves one of the six reads (const int chain = hos_chain_on;
in mutex_lock) into its new mutex_lock_wait; the two conflict on that one line (git merge-tree), and #1957, which
carries #1953's commit, does too. Proposed order: this first (#1953 waits for its in-game measurements); #1953 and #1957
are then rebased, the moved line reading the flag as this PR does. If #1953 lands first, this PR is rebased instead.

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 build jobs of the PC's
job queue (fb3-sep-1, then fb3-sep-2 after the fixture fix below), on this branch on main @ f1f2dc5b, head 2aed1932. Then rebased onto main @ 40bfceb0 without a conflict, its two
commits unchanged (git range-diff), now f74e2760 (test) and a6fffab8; the checks have not run again on that base (of the files this changes, main changed only spin_begin in host/nn_os_sync.c since (#2012), away from these lines):

  • python -I -B tests/test_chain_recorder.py: "PASS: 4001 records of 5 threads, each whole, the note held across the
    stop among them; nothing before the recording". At the test commit 47c61625 (main's recorder) it fails: "1 record(s)
    not whole, the first record 4000: time 0, key 0x0, thread 0, kind 0 (written while its note was still filling it);
    the held note's record is not in the file whole". (fb3-sep-1 ran the test's first form, whose stub did not declare
    hos_chain_start; added.)
  • host/hos_chain.c, host/nn_os_sync.c, host/hos_thread.c, host/nvn_native.c and host/oot_barrier.c (the
    HOS_CHAIN users) 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 (43 checks read the changed files): 36 passed, 7 failed,
    1 skipped (test_tracy_funcs.py: Linux only). The 7 failing (test_crash_record.py, test_dev_packs.py,
    test_host_guest_writes.py, test_libc_hooks.py, test_mods_list.py, test_slow_frames.py, test_vcore_escape.py): they fail on main @ f1f2dc5b too, run by name from a main worktree in the same job, with the same messages.

Still to test:

  • Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market with HOS_CHAIN=chain.bin, chain.bin.go
    made for about 20 s and removed, then python scripts/perf.py chain chain.bin, expecting the "chain: N records of M
    threads written" line, a report like main's and no record of kind 0; and two minutes with HOS_CHAIN unset, this
    PR against main, expecting frame times within the runs' spread (by hand)
  • Linux: build the host and run python3 -I -B tests/test_chain_recorder.py, expecting it to pass (by hand)
  • Linux with an AMD CPU and the Steam Deck: Castle Town's market, two minutes, this PR against main with HOS_CHAIN
    unset, 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 and the Steam Deck, this PR against main, comparing frame times)

Measurements

Nothing to measure: with HOS_CHAIN unset each wait still loads one flag and tests it (an atomic relaxed load, the same instruction on x86-64); the recorder's own cost only shows while a diagnostic recording runs.

Risks

Only HOS_CHAIN recordings change: the stop now waits for notes under way (normally microseconds; at most 5 s, then the
file ends before the first unfinished record and the log says so) instead of sleeping 50 ms, and a recording's file
can no longer hold a record of zeros. Off (no HOS_CHAIN), the waits read the flag as before. No switch of its own; 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 `HOS_CHAIN=<path>` (host/hos_chain.c, a diagnostic for `scripts/perf.py chain`) records the guest threads' waits and wake-ups between `<path>.go` appearing and going away. Its on/off flag, `hos_chain_on`, was a `volatile int`: the recorder thread set and cleared it, a note at the buffer's capacity cleared it, and every wait and signal read it, with no synchronisation, and nothing published the record buffer or the records to the thread that writes the file. At the stop the recorder cleared the flag, slept 50 ms and wrote every record a note had taken, whether or not that note had filled it in yet: a note whose thread the system had not run again for 50 ms left a record of zeros in the file. This makes the flag an atomic: set with release once the buffer is allocated, read with acquire in `hos_chain_note` before it uses the buffer, read relaxed by the `HOS_CHAIN` macro and the six waits in host/nn_os_sync.c that decide whether to call it. A note stores its record's kind last, with release, and the recorder waits (acquire) for the kind of every record taken before the stop, at most 5 s in all, instead of sleeping 50 ms; the thread table it names threads from is stored and read atomically too. The result: the file holds only whole records. With `HOS_CHAIN` unset nothing changes but the flag's load, which is the same instruction on x86-64. How to see it on `main`: `python -I -B tests/test_chain_recorder.py` with the test commit: a note held after it has taken its record while the recording stops leaves a record of zeros in the file (see Testing). Found by BeanBot's review of #1953 ([Blocking 4, `volatile hos_chain_on`](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1953#issuecomment-61957)), which #1953 left on `main` for a separate change. Read first: `hos_chain_note`, `chain_write` and `chain_thread` in `host/hos_chain.c`; `tests/test_chain_recorder.py`. Overlap: #1953 (`perf/mutex-lock-wait-cold` @ 8443bba1) moves one of the six reads (`const int chain = hos_chain_on;` in `mutex_lock`) into its new `mutex_lock_wait`; the two conflict on that one line (`git merge-tree`), and #1957, which carries #1953's commit, does too. Proposed order: this first (#1953 waits for its in-game measurements); #1953 and #1957 are then rebased, the moved line reading the flag as this PR does. If #1953 lands first, this PR is rebased instead. ## 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 build jobs of the PC's job queue (fb3-sep-1, then fb3-sep-2 after the fixture fix below), on this branch on `main` @ f1f2dc5b, head 2aed1932. Then rebased onto `main` @ 40bfceb0 without a conflict, its two commits unchanged (`git range-diff`), now f74e2760 (test) and a6fffab8; the checks have not run again on that base (of the files this changes, `main` changed only `spin_begin` in `host/nn_os_sync.c` since (#2012), away from these lines): - `python -I -B tests/test_chain_recorder.py`: "PASS: 4001 records of 5 threads, each whole, the note held across the stop among them; nothing before the recording". At the test commit 47c61625 (`main`'s recorder) it fails: "1 record(s) not whole, the first record 4000: time 0, key 0x0, thread 0, kind 0 (written while its note was still filling it); the held note's record is not in the file whole". (fb3-sep-1 ran the test's first form, whose stub did not declare `hos_chain_start`; added.) - `host/hos_chain.c`, `host/nn_os_sync.c`, `host/hos_thread.c`, `host/nvn_native.c` and `host/oot_barrier.c` (the `HOS_CHAIN` users) 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` (43 checks read the changed files): 36 passed, 7 failed, 1 skipped (`test_tracy_funcs.py`: Linux only). The 7 failing (`test_crash_record.py`, `test_dev_packs.py`, `test_host_guest_writes.py`, `test_libc_hooks.py`, `test_mods_list.py`, `test_slow_frames.py`, `test_vcore_escape.py`): they fail on `main` @ f1f2dc5b too, run by name from a `main` worktree in the same job, with the same messages. **Still to test:** - Windows 11, this PC (i7-9700, RTX 4060 (NVIDIA 610.88)): Castle Town's market with `HOS_CHAIN=chain.bin`, `chain.bin.go` made for about 20 s and removed, then `python scripts/perf.py chain chain.bin`, expecting the "chain: N records of M threads written" line, a report like `main`'s and no record of kind 0; and two minutes with `HOS_CHAIN` unset, this PR against `main`, expecting frame times within the runs' spread (by hand) - Linux: build the host and run `python3 -I -B tests/test_chain_recorder.py`, expecting it to pass (by hand) - Linux with an AMD CPU and the Steam Deck: Castle Town's market, two minutes, this PR against `main` with `HOS_CHAIN` unset, 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 and the Steam Deck, this PR against main, comparing frame times) ## Measurements Nothing to measure: with `HOS_CHAIN` unset each wait still loads one flag and tests it (an atomic relaxed load, the same instruction on x86-64); the recorder's own cost only shows while a diagnostic recording runs. ## Risks Only `HOS_CHAIN` recordings change: the stop now waits for notes under way (normally microseconds; at most 5 s, then the file ends before the first unfinished record and the log says so) instead of sleeping 50 ms, and a recording's file can no longer hold a record of zeros. Off (no `HOS_CHAIN`), the waits read the flag as before. No switch of its own; 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_chain_recorder.py compiles the real host/hos_chain.c over a stub hos.h (host/hos.h's HOS_CHAIN declarations
and macro cut into it) and drives it with HOS_CHAIN set and its .go file made and removed: four threads note 1000 times
each through the HOS_CHAIN macro, then one more thread's note is held after it has taken its record (in the clock it
reads) while the recording stops. The file must hold 4001 whole records, the held one among them. On main the recorder
clears its volatile flag, sleeps 50 ms and writes the file while that note has not filled its record: a record of
zeros is written.

Assisted-by: Claude Code
fix(os): HOS_CHAIN's flag is an atomic, and the recorder writes a record only once its note has filled it
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 16s
Website / Website on Cloudflare (pull_request_target) Has been skipped
a6fffab8d7
hos_chain_on was a volatile int: the recorder thread set and cleared it, a note at capacity cleared it, and every
wait and signal read it with no synchronisation, and nothing published the record buffer (recs) or the records to the
thread that writes the file. At the stop the recorder cleared the flag, slept 50 ms and wrote every record a note had
taken, whether or not the note had filled it in yet.

The flag is now a plain int accessed with atomics: set with release once recs is allocated, read with acquire in
hos_chain_note before it uses recs, read relaxed by HOS_CHAIN and the six waits in nn_os_sync.c that decide whether to
call it. A note stores its record's kind last, with release; chain_write waits (acquire) for the kind of each record
taken before the stop, at most 5 s in all, instead of the 50 ms sleep, so the file holds only whole records. The thread
table the file names its threads from is stored and read atomically too.

Assisted-by: Claude Code
BeanBot left a comment

Recommendation: Request changes. The readiness flag fixes the zero-filled record reproduced by the Windows fixture, but the recorder can still dereference a destroyed guest thread while writing its name. Stop and slot reservation also need a common cutoff so a late producer cannot delay or alter the capture after it closes.

This makes the diagnostic flag atomic and publishes each record after its fields are ready. That improves capture integrity without adding a lock to the waits or presents that check the flag. When recording is enabled, the new stop path scans the capture and can wait up to five seconds, then discard completed records after the first unfinished slot.

@fix will do (20)

  1. L1 host/hos_chain.c:85: The atomic load protects the pointer slot but leaves the pointed-to thread unowned. (more on the line)
  2. L2 host/hos_chain.c:86: A live thread name can still race with SetThreadName. (more on the line)
  3. L3 host/hos_chain.c:43: Checking the flag and reserving a slot are separate operations. (more on the line)
  4. L4 host/hos_chain.c:111: Removing .go from an active capture with zero reservations leaves the flag set. (more on the line)
  5. L5 (its part an edit can do) host/hos_chain.c:73: Keep the original cutoff and inspect the remaining slots after the deadline. Report the omitted count and preserve completed records if partial captures are supported, or report a failed capture.
  6. L6 (minor) host/hos_chain.c:47: The 1,025th distinct producer shares a thread ID with the 1,024th. (more on the line)
  7. L7 (minor) host/hos_chain.c:88: An empty guest thread name produces a two-field header that the real reader rejects. (more on the line)
  8. L8 host/hos_chain.c:92: The writer ignores header, payload and close errors and logs success even if a full disk leaves a truncated file. (more on the line)
  9. L9 (minor) host/hos_chain.c:100: The marker path silently truncates output paths longer than 1,020 bytes. A valid path in a deep Linux directory can therefore fail to start recording or poll a different marker. Size the buffer from the path and suffix, or reject an overlong path with a clear diagnostic before polling.
  10. L10 (minor) host/hos_chain.c:104: Any marker open failure is treated as removal. (more on the line)
  11. L11 (minor) host/hos_chain.c:115: The one-shot recorder keeps its buffer and continues waking every 100 ms to open the marker for the rest of the process, even after a failed write. (more on the line)
  12. L12 (minor) host/hos_chain.c:123: CreateThread's result is ignored. If thread creation fails, a requested capture silently does nothing. If it succeeds, the handle is leaked on Windows and retains the shim's handle reference on Linux. Log failure and close the successful handle if the thread is detached.
  13. L13 tests/test_chain_recorder.py:76: The fixture gives the recorder pointers into each worker's TLS, then joins the workers before the recorder reads their names. (more on the line)
  14. L14 tests/test_chain_recorder.py:111: The held note releases itself after two seconds even if the recorder has never observed the stop. (more on the line)
  15. L15 tests/test_chain_recorder.py:223: The Windows fixture omits the UTF-8 manifest that Longshot uses for narrow argv, getenv and fopen. (more on the line)
  16. L16 (minor) tests/test_chain_recorder.py:41: The fixture treats CC as a single executable. (more on the line)
  17. L17 (minor) tests/test_chain_recorder.py:145: On Linux a failed pthread_create leaves join with an uninitialized thread handle. (more on the line)
  18. L18 (minor) tests/test_chain_recorder.py:233: The fixture checks the held record's identity but accepts duplicate worker records, any valid kind for worker events and any caller address. (more on the line)
  19. L19 (minor) docs/dev/reference/profiling.md:120: The reference says the capture is written when the marker goes away or the buffer fills, without describing delayed or partial output. A developer who removes the marker and immediately quits can miss the file while it waits for a note. Document the final timeout and output policy and the completion log to wait for before exiting.
  20. L20 (minor): Measurements dismisses measurement even though the repository expects before-and-after proof for a fix. Put the zero-filled versus 4,001-whole-record result there with its actual commits and setup, then add the current comparison when it runs. Correct the suite tally as well. The reported results account for 43 executed checks plus one skip.

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

  • L5 (minor) host/hos_chain.c:73: The deadline throws away every later record, including whole records from unrelated threads. (more on the line)
  • L21 (minor): The PR opened without the project's required author-side in-game run. The earlier fixture and syntax checks cannot establish that an actual capture opens in scripts/perf.py or that the disabled diagnostic leaves frame pacing within the usual spread. Complete the Windows and Linux validation, including the planned Steam Deck and affected Tier 1 runs, and record the base and head results.
  • L22 (minor): The earlier Windows report covers selected checks only and has a Linux-only skip. Run the full required suite on the updated head, confirm any failures against the same base, and record Linux GCC or Clang and Windows llvm-mingw results for the recorder fixture.

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).

More: does it help, other systems, switches, what was read

Does it help: Yes for the reported Windows reproduction. On the earlier base, the test-only commit wrote one zero-filled record out of 4,001. The earlier fixed head wrote all 4,001 whole records from five threads. The current base and head have no reported rerun, and in-game behavior and frame pacing remain unverified.

Other systems: The flag is read by guest synchronization, virtual-core scheduling, sleep, the job barrier and NVN presents. Their lock and wake paths are unchanged in the diff. Windows and Linux share the recorder, with Linux providing the Win32 thread and sleep shims. The binary layout and consumer are unchanged, but the consumer still depends on valid thread headers and a complete file. The change has no direct GPU-vendor, renderer ABI, audio, input, save, cache or Launcher changes. Windows syntax checks with and without Tracy were reported on the earlier head. Linux, Steam Deck and in-game frame pacing are pending.

Switches: No switch was added or removed. HOS_CHAIN remains an opt-in developer diagnostic and defaults to off, which is appropriate. Its enabled stop behavior changes. The existing switch-table row still describes the default correctly, and the profiling reference needs the new stop behavior.

Reviewed: a6fffab8d7 against 40bfceb055: Read all four changed files and both commits, plus AGENTS.md, CONTRIBUTING.md, the Forgejo template and developer standards. No docs/review-checklist.md was present. Read the relevant startup, thread lifetime and naming, macro caller, POSIX shim, capture reader and test-runner code. Skimmed the reported Windows runs, CI status and the earlier chain-reader fix. No generated or translated files changed.; nothing run (a code review).

Checked and fine:

  • All production flag reads and writes use atomic operations. The release at startup and acquire in the note publish the allocated buffer, and the relaxed guards preserve the existing short-circuit conditions.
  • For each retained slot, the writer's acquire of kind orders the preceding record field writes before serialization. The record count is capped before indexing or multiplying by the record size.
  • The 32-byte record layout, kind values and HOSCHAIN1 version are unchanged. Existing source checks still match the declarations and layout.
  • The diff is one topic, both commits carry Assisted-by trailers, and there are no game-derived fixtures, build outputs, generated files, third-party edits or renderer API changes. The underlying title follows Conventional Commits.
  • The fixture explicitly sets HOS_CHAIN for its child and directs writes to its temporary directory. The described compiler and fixture execution match the code.

Testing: Read-only review. The author reported a Windows game-free failure on the earlier base and a pass on the earlier fixed head, plus Windows syntax checks with and without Tracy. Seven reported failures also occurred on that earlier base. The supplied current CI has checks still running and lists no head-only failures.

Overlaps and merge order: The PR reports one conflicting mutex-wait flag read in #1953 and in #1957, which carries that commit. Land #2019 first, then rebase both and keep the atomic read at its new location. If #1953 lands first, rebase #2019. The overlap and merge order come from the PR description.


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 readiness flag fixes the zero-filled record reproduced by the Windows fixture, but the recorder can still dereference a destroyed guest thread while writing its name. Stop and slot reservation also need a common cutoff so a late producer cannot delay or alter the capture after it closes. This makes the diagnostic flag atomic and publishes each record after its fields are ready. That improves capture integrity without adding a lock to the waits or presents that check the flag. When recording is enabled, the new stop path scans the capture and can wait up to five seconds, then discard completed records after the first unfinished slot. **`@fix` will do (20)** 1. **L1** `host/hos_chain.c:85`: The atomic load protects the pointer slot but leaves the pointed-to thread unowned. (more on the line) 2. **L2** `host/hos_chain.c:86`: A live thread name can still race with SetThreadName. (more on the line) 3. **L3** `host/hos_chain.c:43`: Checking the flag and reserving a slot are separate operations. (more on the line) 4. **L4** `host/hos_chain.c:111`: Removing .go from an active capture with zero reservations leaves the flag set. (more on the line) 5. **L5** (its part an edit can do) `host/hos_chain.c:73`: Keep the original cutoff and inspect the remaining slots after the deadline. Report the omitted count and preserve completed records if partial captures are supported, or report a failed capture. 6. **L6** (minor) `host/hos_chain.c:47`: The 1,025th distinct producer shares a thread ID with the 1,024th. (more on the line) 7. **L7** (minor) `host/hos_chain.c:88`: An empty guest thread name produces a two-field header that the real reader rejects. (more on the line) 8. **L8** `host/hos_chain.c:92`: The writer ignores header, payload and close errors and logs success even if a full disk leaves a truncated file. (more on the line) 9. **L9** (minor) `host/hos_chain.c:100`: The marker path silently truncates output paths longer than 1,020 bytes. A valid path in a deep Linux directory can therefore fail to start recording or poll a different marker. Size the buffer from the path and suffix, or reject an overlong path with a clear diagnostic before polling. 10. **L10** (minor) `host/hos_chain.c:104`: Any marker open failure is treated as removal. (more on the line) 11. **L11** (minor) `host/hos_chain.c:115`: The one-shot recorder keeps its buffer and continues waking every 100 ms to open the marker for the rest of the process, even after a failed write. (more on the line) 12. **L12** (minor) `host/hos_chain.c:123`: CreateThread's result is ignored. If thread creation fails, a requested capture silently does nothing. If it succeeds, the handle is leaked on Windows and retains the shim's handle reference on Linux. Log failure and close the successful handle if the thread is detached. 13. **L13** `tests/test_chain_recorder.py:76`: The fixture gives the recorder pointers into each worker's TLS, then joins the workers before the recorder reads their names. (more on the line) 14. **L14** `tests/test_chain_recorder.py:111`: The held note releases itself after two seconds even if the recorder has never observed the stop. (more on the line) 15. **L15** `tests/test_chain_recorder.py:223`: The Windows fixture omits the UTF-8 manifest that Longshot uses for narrow argv, getenv and fopen. (more on the line) 16. **L16** (minor) `tests/test_chain_recorder.py:41`: The fixture treats CC as a single executable. (more on the line) 17. **L17** (minor) `tests/test_chain_recorder.py:145`: On Linux a failed pthread_create leaves join with an uninitialized thread handle. (more on the line) 18. **L18** (minor) `tests/test_chain_recorder.py:233`: The fixture checks the held record's identity but accepts duplicate worker records, any valid kind for worker events and any caller address. (more on the line) 19. **L19** (minor) `docs/dev/reference/profiling.md:120`: The reference says the capture is written when the marker goes away or the buffer fills, without describing delayed or partial output. A developer who removes the marker and immediately quits can miss the file while it waits for a note. Document the final timeout and output policy and the completion log to wait for before exiting. 20. **L20** (minor): Measurements dismisses measurement even though the repository expects before-and-after proof for a fix. Put the zero-filled versus 4,001-whole-record result there with its actual commits and setup, then add the current comparison when it runs. Correct the suite tally as well. The reported results account for 43 executed checks plus one skip. **For the author (3)** (a run, a measurement or a decision) - **L5** (minor) `host/hos_chain.c:73`: The deadline throws away every later record, including whole records from unrelated threads. (more on the line) - **L21** (minor): The PR opened without the project's required author-side in-game run. The earlier fixture and syntax checks cannot establish that an actual capture opens in scripts/perf.py or that the disabled diagnostic leaves frame pacing within the usual spread. Complete the Windows and Linux validation, including the planned Steam Deck and affected Tier 1 runs, and record the base and head results. - **L22** (minor): The earlier Windows report covers selected checks only and has a Linux-only skip. Run the full required suite on the updated head, confirm any failures against the same base, and record Linux GCC or Clang and Windows llvm-mingw results for the recorder fixture. **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)`. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Yes for the reported Windows reproduction. On the earlier base, the test-only commit wrote one zero-filled record out of 4,001. The earlier fixed head wrote all 4,001 whole records from five threads. The current base and head have no reported rerun, and in-game behavior and frame pacing remain unverified. **Other systems:** The flag is read by guest synchronization, virtual-core scheduling, sleep, the job barrier and NVN presents. Their lock and wake paths are unchanged in the diff. Windows and Linux share the recorder, with Linux providing the Win32 thread and sleep shims. The binary layout and consumer are unchanged, but the consumer still depends on valid thread headers and a complete file. The change has no direct GPU-vendor, renderer ABI, audio, input, save, cache or Launcher changes. Windows syntax checks with and without Tracy were reported on the earlier head. Linux, Steam Deck and in-game frame pacing are pending. **Switches:** No switch was added or removed. HOS_CHAIN remains an opt-in developer diagnostic and defaults to off, which is appropriate. Its enabled stop behavior changes. The existing switch-table row still describes the default correctly, and the profiling reference needs the new stop behavior. **Reviewed:** a6fffab8d79c against 40bfceb0552e: Read all four changed files and both commits, plus AGENTS.md, CONTRIBUTING.md, the Forgejo template and developer standards. No docs/review-checklist.md was present. Read the relevant startup, thread lifetime and naming, macro caller, POSIX shim, capture reader and test-runner code. Skimmed the reported Windows runs, CI status and the earlier chain-reader fix. No generated or translated files changed.; nothing run (a code review). **Checked and fine:** - All production flag reads and writes use atomic operations. The release at startup and acquire in the note publish the allocated buffer, and the relaxed guards preserve the existing short-circuit conditions. - For each retained slot, the writer's acquire of kind orders the preceding record field writes before serialization. The record count is capped before indexing or multiplying by the record size. - The 32-byte record layout, kind values and HOSCHAIN1 version are unchanged. Existing source checks still match the declarations and layout. - The diff is one topic, both commits carry Assisted-by trailers, and there are no game-derived fixtures, build outputs, generated files, third-party edits or renderer API changes. The underlying title follows Conventional Commits. - The fixture explicitly sets HOS_CHAIN for its child and directs writes to its temporary directory. The described compiler and fixture execution match the code. **Testing:** Read-only review. The author reported a Windows game-free failure on the earlier base and a pass on the earlier fixed head, plus Windows syntax checks with and without Tracy. Seven reported failures also occurred on that earlier base. The supplied current CI has checks still running and lists no head-only failures. **Overlaps and merge order:** The PR reports one conflicting mutex-wait flag read in #1953 and in #1957, which carries that commit. Land #2019 first, then rebase both and keep the atomic read at its new location. If #1953 lands first, rebase #2019. The overlap and merge order come from the PR description. </details> --- <sub>Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -36,3 +41,3 @@
void hos_chain_note(int kind, uint64_t key, uint64_t lr) {
if (!hos_chain_on) return;
if (!__atomic_load_n(&hos_chain_on, __ATOMIC_ACQUIRE)) return;
const LONG64 i = InterlockedIncrement64(&nrecs) - 1;
Member

L3: Checking the flag and reserving a slot are separate operations. A producer can pass the acquire load, pause, then reserve after the recorder clears the flag. If it reserves just before the recorder snapshots the count and pauses again, the recorder can wait five seconds for a slot admitted after close. Put reservation and close under one atomic state or lock, then take the final count from that state.

**L3**: Checking the flag and reserving a slot are separate operations. A producer can pass the acquire load, pause, then reserve after the recorder clears the flag. If it reserves just before the recorder snapshots the count and pauses again, the recorder can wait five seconds for a slot admitted after close. Put reservation and close under one atomic state or lock, then take the final count from that state.
@ -40,3 +44,4 @@
if (i >= CHAIN_CAP) { __atomic_store_n(&hos_chain_on, 0, __ATOMIC_RELAXED); return; }
if (my_index < 0) {
const LONG k = InterlockedIncrement(&nthreads) - 1;
my_index = k < CHAIN_THREADS ? (int)k : CHAIN_THREADS - 1;
Member

L6 (minor): The 1,025th distinct producer shares a thread ID with the 1,024th. A capture with enough short-lived threads can pair one thread's wait with another thread's wake and attribute both to the same name. Stop admitting records with unrepresentable thread IDs and report the limit, or size the table for the record format.

**L6** (minor): The 1,025th distinct producer shares a thread ID with the 1,024th. A capture with enough short-lived threads can pair one thread's wait with another thread's wake and attribute both to the same name. Stop admitting records with unrepresentable thread IDs and report the limit, or size the table for the record format.
@ -67,2 +85,4 @@
HosThread *t = __atomic_load_n(&threads[i], __ATOMIC_RELAXED);
if (t) snprintf(name, sizeof name, "%s", hos_thread_name(t));
else snprintf(name, sizeof name, "host-%d", i); /* (a host thread: the vsync timer, the renderer's) */
for (char *q = name; *q; q++) if (*q == ' ' || *q == '\n') *q = '_';
Member

L7 (minor): An empty guest thread name produces a two-field header that the real reader rejects. A tab in a name can also cause the reader to drop part of it. Use a fallback for empty names, sanitize whitespace consistently with the reader and check the output with the real reader.

**L7** (minor): An empty guest thread name produces a two-field header that the real reader rejects. A tab in a name can also cause the reader to drop part of it. Use a fallback for empty names, sanitize whitespace consistently with the reader and check the output with the real reader.
@ -60,0 +70,4 @@
if (hos_now_ns() > give_up) {
hos_log("chain: record %lld of %lld still not filled in after 5 s: the file ends before it",
(long long)i, (long long)n);
n = i;
Member

L5 (minor): The deadline throws away every later record, including whole records from unrelated threads. If slot zero is held while other threads record frames, the file can contain zero records. Choose whether a timeout should fail the capture explicitly or preserve whole records with a clear indication that events were lost.

**L5** (minor): The deadline throws away every later record, including whole records from unrelated threads. If slot zero is held while other threads record frames, the file can contain zero records. Choose whether a timeout should fail the capture explicitly or preserve whole records with a clear indication that events were lost.
Member

L8: The writer ignores header, payload and close errors and logs success even if a full disk leaves a truncated file. Check the writes and fclose before reporting success. Write to a sibling temporary file and publish it only after a successful close so a failed capture cannot replace a valid one.

**L8**: The writer ignores header, payload and close errors and logs success even if a full disk leaves a truncated file. Check the writes and fclose before reporting success. Write to a sibling temporary file and publish it only after a successful close so a failed capture cannot replace a valid one.
@ -64,3 +83,3 @@
for (int i = 0; i < nt; i++) {
char name[64];
if (threads[i]) snprintf(name, sizeof name, "%s", hos_thread_name(threads[i]));
HosThread *t = __atomic_load_n(&threads[i], __ATOMIC_RELAXED);
Member

L1: The atomic load protects the pointer slot but leaves the pointed-to thread unowned. A worker can record an event, exit and be freed by DestroyThread before the recorder formats its name. Keep a recorder-owned name snapshot or resolve a stable thread ID through a lifetime-safe API, with a fallback for exited threads.

**L1**: The atomic load protects the pointer slot but leaves the pointed-to thread unowned. A worker can record an event, exit and be freed by DestroyThread before the recorder formats its name. Keep a recorder-owned name snapshot or resolve a stable thread ID through a lifetime-safe API, with a fallback for exited threads.
Member

L10 (minor): Any marker open failure is treated as removal. An existing marker that temporarily cannot be opened, for example because another Windows process opened it without sharing, can end a capture. Query marker existence without opening it for reading and distinguish absence from other errors.

**L10** (minor): Any marker open failure is treated as removal. An existing marker that temporarily cannot be opened, for example because another Windows process opened it without sharing, can end a capture. Query marker existence without opening it for reading and distinguish absence from other errors.
@ -65,2 +84,3 @@
char name[64];
if (threads[i]) snprintf(name, sizeof name, "%s", hos_thread_name(threads[i]));
HosThread *t = __atomic_load_n(&threads[i], __ATOMIC_RELAXED);
if (t) snprintf(name, sizeof name, "%s", hos_thread_name(t));
Member

L2: A live thread name can still race with SetThreadName. The acquire of a record's kind orders publication of that record, while another guest thread can rewrite the plain name array during snprintf. Copy the name through an accessor synchronized with the setter, and give the file writer ownership of the resulting bytes.

**L2**: A live thread name can still race with SetThreadName. The acquire of a record's kind orders publication of that record, while another guest thread can rewrite the plain name array during snprintf. Copy the name through an accessor synchronized with the setter, and give the file writer ownership of the resulting bytes.
@ -95,2 +112,4 @@
(!g || !__atomic_load_n(&hos_chain_on, __ATOMIC_RELAXED))) {
__atomic_store_n(&hos_chain_on, 0, __ATOMIC_RELAXED);
chain_write(path);
done = 1; /* (one recording a run) */
Member

L11 (minor): The one-shot recorder keeps its buffer and continues waking every 100 ms to open the marker for the rest of the process, even after a failed write. Exit the recorder after its final result and reclaim the buffer once admitted producers have drained. If the write times out, defer reclamation until those producers are done.

**L11** (minor): The one-shot recorder keeps its buffer and continues waking every 100 ms to open the marker for the rest of the process, even after a failed write. Exit the recorder after its final result and reclaim the buffer once admitted producers have drained. If the write times out, defer reclamation until those producers are done.
@ -93,2 +110,2 @@
hos_chain_on = 0;
Sleep(50); /* (notes under way finish) */
__atomic_store_n(&hos_chain_on, 1, __ATOMIC_RELEASE);
} else if (!done && __atomic_load_n(&nrecs, __ATOMIC_RELAXED) &&
Member

L4: Removing .go from an active capture with zero reservations leaves the flag set. If the first event happens seconds later, it enters a capture that should already have ended. Track an explicit active state and close it on marker removal even with zero records. Use the same state to stop when the last available slot is reserved, since a full buffer currently needs one more note to turn recording off.

**L4**: Removing .go from an active capture with zero reservations leaves the flag set. If the first event happens seconds later, it enters a capture that should already have ended. Track an explicit active state and close it on marker removal even with zero records. Use the same state to stop when the last available slot is reserved, since a full buffer currently needs one more note to turn recording off.
@ -0,0 +38,4 @@
clone's folder), else clang, gcc or cc on PATH"""
for c in (given, os.environ.get("CC")):
if c:
return shutil.which(c) or c
Member

L16 (minor): The fixture treats CC as a single executable. A setting such as CC='ccache clang' fails with an executable lookup error even though the toolchain is installed. Preserve direct executable paths, and parse command-style CC values into an argument list for both compile steps.

**L16** (minor): The fixture treats CC as a single executable. A setting such as CC='ccache clang' fails with an executable lookup error even though the toolchain is installed. Preserve direct executable paths, and parse command-style CC values into an argument list for both compile steps.
@ -0,0 +73,4 @@
#include <time.h>
#endif
struct HosThread { char name[32]; };
static _Thread_local struct HosThread me;
Member

L13: The fixture gives the recorder pointers into each worker's TLS, then joins the workers before the recorder reads their names. Those objects have ended their lifetime. Linux can reuse the TLS storage for the held thread, and Windows can release it. Use fixture-owned storage that stays live through the write, and cover exited-thread handling separately.

**L13**: The fixture gives the recorder pointers into each worker's TLS, then joins the workers before the recorder reads their names. Those objects have ended their lifetime. Linux can reuse the TLS storage for the held thread, and Windows can release it. Use fixture-owned storage that stays live through the write, and cover exited-thread handling separately.
@ -0,0 +108,4 @@
hold_next = 0;
__atomic_store_n(&held, 1, __ATOMIC_SEQ_CST);
const int64_t t0 = mono_ns();
while (!__atomic_load_n(&written, __ATOMIC_SEQ_CST) && mono_ns() - t0 < 2000000000ll) Sleep(1);
Member

L14: The held note releases itself after two seconds even if the recorder has never observed the stop. On a loaded runner the old code or a build with the wait deleted can then pass. Gate release on an observed stop and an explicit fixture handshake. Make missing handshakes fail, use monotonic deadlines for the fixture waits, and verify the wait-deleted build fails.

**L14**: The held note releases itself after two seconds even if the recorder has never observed the stop. On a loaded runner the old code or a build with the wait deleted can then pass. Gate release on an observed stop and an explicit fixture handshake. Make missing handshakes fail, use monotonic deadlines for the fixture waits, and verify the wait-deleted build fails.
@ -0,0 +142,4 @@
static void join(Thread t) { WaitForSingleObject(t, INFINITE); }
#else
typedef pthread_t Thread;
static Thread start(void *(*fn)(void *), void *arg) { pthread_t t; pthread_create(&t, NULL, fn, arg); return t; }
Member

L17 (minor): On Linux a failed pthread_create leaves join with an uninitialized thread handle. The Windows path also ignores creation and wait failures and leaves worker handles open, and marker removal is unchecked on both. Check these operations and report the specific setup failure before interpreting recording results.

**L17** (minor): On Linux a failed pthread_create leaves join with an uninitialized thread handle. The Windows path also ignores creation and wait failures and leaves worker handles open, and marker removal is unchecked on both. Check these operations and report the specific setup failure before interpreting recording results.
@ -0,0 +220,4 @@
str(ROOT / "host" / "posix" / "win32_posix.c"), "-o", str(shim)], check=True)
cmd[1:1] = ["-I", str(ROOT / "host" / "posix")]
cmd += [str(shim), "-pthread", "-ldl"]
subprocess.run(cmd + ["-o", str(exe)], check=True)
Member

L15: The Windows fixture omits the UTF-8 manifest that Longshot uses for narrow argv, getenv and fopen. An emoji in the temporary-directory path can make the child fail before it starts recording on a system using a legacy code page. Link the existing manifest resource and run the fixture from a deliberately non-ASCII directory.

**L15**: The Windows fixture omits the UTF-8 manifest that Longshot uses for narrow argv, getenv and fopen. An emoji in the temporary-directory path can make the child fail before it starts recording on a system using a legacy code page. Link the existing manifest resource and run the fixture from a deliberately non-ASCII directory.
@ -0,0 +230,4 @@
raise SystemExit("test_chain_recorder: FAILED: the fixture exited %d" % r.returncode)
names, recs = records(out)
want = WORKERS * NOTES + 1
bad = [(i, rec) for i, rec in enumerate(recs) if not (1 <= rec[4] <= 6 and rec[0] and rec[1] and rec[3] in names)]
Member

L18 (minor): The fixture checks the held record's identity but accepts duplicate worker records, any valid kind for worker events and any caller address. Its pre-recording probe also runs before the recorder starts. Compare the expected worker keys, kinds, names and a nonzero caller address, and exercise the started recorder while the marker is absent.

**L18** (minor): The fixture checks the held record's identity but accepts duplicate worker records, any valid kind for worker events and any caller address. Its pre-recording probe also runs before the recorder starts. Compare the expected worker keys, kinds, names and a nonzero caller address, and exercise the started recorder while the marker is absent.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
PR description / Description follows the template (pull_request_target) Successful in 16s
Website / Website on Cloudflare (pull_request_target) Has been skipped
This pull request is marked as a work in progress.
Some workflows are waiting to be reviewed.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/os-chain-flag-atomic:siantm-fix/os-chain-flag-atomic'
git switch 'siantm-fix/os-chain-flag-atomic'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 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!2019
No description provided.