hle: fix SRW wait race and order the native waits probe #31

Merged
shabouwcaw merged 3 commits from upstream-pr/31 into main 2026-11-26 10:48:53 +00:00
shabouwcaw commented 2026-11-26 10:40:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by shabouwcaw on 2026-11-26T10:40:38Z.


The native waits probe exposed two distinct problems: an SRW acquisition can sleep on an unlocked word after losing a CAS race, and the critical-section try check assumes its competing thread runs within 80 ms.

Addresses #20. The reported one-in-three failure rate was not reproduced; the failures and reproducer below are the evidence for this change.

  • After a failed acquisition CAS, srw_try reloads the word. If the previous owner has released it, srw_acquire could mark that available word with WAITERS and sleep indefinitely with nobody left to wake it. Retry acquisition when the observed state permits the requested ownership, including a reader joining other readers. Keep the existing compare/exchange and address-wait protocol for incompatible ownership.
  • Hold the critical section on the probe's calling thread until the competing TryEnterCriticalSection thread finishes. Remove the timed holder thread and event. Require successful thread completion, rejection while owned, and successful acquisition after release (so an always-failing stub cannot pass); retain the 5-second hang guard and stop before reusing worker state on failure.
  • Add a focused x86_64 macOS regression for the available-word states after a failed try (exclusive/shared, with/without the waiter bit, and shared readers). It fails against the original wait logic, and wakes/joins the worker before asserting on failure. Existing wait-result, timeout, signaling, critical-section, SRW and condition-variable checks remain.

This follows the documented Windows SRW shared-reader/exclusive-writer semantics, without promising fairness. docs/native-host.md records the race and the architecture requirement.

Validation on Apple Silicon, explicitly using x86_64-apple-darwin under Rosetta:

Check Before After
Original waits test with bounded CPU load 29/30 passed, then 40/40 passed 70/70 passed on final 9880186
Temporary 120 ms scheduling delay before creating the competing try thread 0/5 passed 5/5 passed
Focused available-SRW-word regression Failed: slept on word 0, exclusive Passed for all six states

The natural failure reported probe_sync: missing bits 0x0040 (shifted T_SRW) after about 21 seconds; the delay reproducer consistently reported 0x0020 (T_CS_TRY). The SRW state regression demonstrates the deadlock path; the natural failure did not capture the internal lock word, so attributing that particular run to this path is an inference. Each stress batch used six nice CPU workers, cleaned up afterward; the tail of the first baseline batch briefly overlapped the next experiment. These counts are observations, not an estimate of the original failure rate. The temporary delay is not committed. A separate mutation making both TryEnterCriticalSection calls always fail was correctly rejected with missing bit 0x0020; that mutation was also removed.

The Mach-O test executable was verified as x86_64, --list included the waits test, and every stress invocation required 1 passed; 0 failed. Each process rebuilt the PE/DLL fixture through build-native-probe.sh; no stale C fixture was reused.

Also passed (all builds under nice, at most six jobs):

  • cargo test -j6 --locked --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: 267 passed, 0 failed, 4 ignored, including all 16 native_probe tests and the new regression.
  • cargo test -j6 --locked -p victory-shader -p victory-hle -p victory -p victory-gpu -p xtask: 500 passed, 0 failed, 4 ignored on arm64. This supplements the Rosetta runs; it does not execute the waits test.
  • Windows-target workspace Clippy with --all-targets --locked -- -D warnings, both default and --all-features configurations.
  • cargo run -j6 --locked --quiet -p xtask -- policy: 363 files OK.
  • Changed Rust file formatting and git diff --check.
  • Upstream Windows CI passed on final commit 9880186: both Clippy configurations and workspace tests.

No game content or Apple SDK binaries are included. The finite stress sample cannot rule out other independent causes of #20; this PR fixes the two demonstrated defects rather than increasing timeouts or suppressing assertions.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6624840833 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/31). Originally posted by [shabouwcaw](https://github.com/shabouwcaw) on 2026-11-26T10:40:38Z. --- The native waits probe exposed two distinct problems: an SRW acquisition can sleep on an unlocked word after losing a CAS race, and the critical-section try check assumes its competing thread runs within 80 ms. Addresses #20. The reported one-in-three failure rate was not reproduced; the failures and reproducer below are the evidence for this change. - After a failed acquisition CAS, `srw_try` reloads the word. If the previous owner has released it, `srw_acquire` could mark that available word with `WAITERS` and sleep indefinitely with nobody left to wake it. Retry acquisition when the observed state permits the requested ownership, including a reader joining other readers. Keep the existing compare/exchange and address-wait protocol for incompatible ownership. - Hold the critical section on the probe's calling thread until the competing `TryEnterCriticalSection` thread finishes. Remove the timed holder thread and event. Require successful thread completion, rejection while owned, and successful acquisition after release (so an always-failing stub cannot pass); retain the 5-second hang guard and stop before reusing worker state on failure. - Add a focused x86_64 macOS regression for the available-word states after a failed try (exclusive/shared, with/without the waiter bit, and shared readers). It fails against the original wait logic, and wakes/joins the worker before asserting on failure. Existing wait-result, timeout, signaling, critical-section, SRW and condition-variable checks remain. This follows the documented [Windows SRW shared-reader/exclusive-writer semantics](https://learn.microsoft.com/en-us/windows/win32/sync/slim-reader-writer--srw--locks), without promising fairness. `docs/native-host.md` records the race and the architecture requirement. Validation on Apple Silicon, explicitly using `x86_64-apple-darwin` under Rosetta: | Check | Before | After | | --- | --- | --- | | Original waits test with bounded CPU load | 29/30 passed, then 40/40 passed | 70/70 passed on final `9880186` | | Temporary 120 ms scheduling delay before creating the competing try thread | 0/5 passed | 5/5 passed | | Focused available-SRW-word regression | Failed: slept on word `0`, exclusive | Passed for all six states | The natural failure reported `probe_sync: missing bits 0x0040` (shifted `T_SRW`) after about 21 seconds; the delay reproducer consistently reported `0x0020` (`T_CS_TRY`). The SRW state regression demonstrates the deadlock path; the natural failure did not capture the internal lock word, so attributing that particular run to this path is an inference. Each stress batch used six `nice` CPU workers, cleaned up afterward; the tail of the first baseline batch briefly overlapped the next experiment. These counts are observations, not an estimate of the original failure rate. The temporary delay is not committed. A separate mutation making both `TryEnterCriticalSection` calls always fail was correctly rejected with missing bit `0x0020`; that mutation was also removed. The Mach-O test executable was verified as x86_64, `--list` included the waits test, and every stress invocation required `1 passed; 0 failed`. Each process rebuilt the PE/DLL fixture through `build-native-probe.sh`; no stale C fixture was reused. Also passed (all builds under `nice`, at most six jobs): - `cargo test -j6 --locked --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory`: **267 passed, 0 failed, 4 ignored**, including all **16 native_probe tests** and the new regression. - `cargo test -j6 --locked -p victory-shader -p victory-hle -p victory -p victory-gpu -p xtask`: **500 passed, 0 failed, 4 ignored** on arm64. This supplements the Rosetta runs; it does not execute the waits test. - Windows-target workspace Clippy with `--all-targets --locked -- -D warnings`, both default and `--all-features` configurations. - `cargo run -j6 --locked --quiet -p xtask -- policy`: **363 files OK**. - Changed Rust file formatting and `git diff --check`. - [Upstream Windows CI](https://github.com/LakshanSivakumar/Native-Victory-macOS/actions/runs/45803822558) passed on final commit `9880186`: both Clippy configurations and workspace tests. No game content or Apple SDK binaries are included. The finite stress sample cannot rule out other independent causes of #20; this PR fixes the two demonstrated defects rather than increasing timeouts or suppressing assertions.
LakshanSivakumar commented 2026-11-26 10:45:23 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T10:45:23Z.

Review: APPROVED


Great catch. Sleeping on an available SRW word after a lost CAS is a real lost-wakeup, and the regression covers every state. Tested merged with current main: x86_64-apple-darwin hle/loader/victory (220 + 16 native_probe), the arm64 suites, clippy (Windows target) and policy all pass. We suspect this may also explain a 30 s main-loop stall that trips the title's hang watchdog during story loading, and we're testing that now.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6072722644 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/31#pullrequestreview-6072722644). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T10:45:23Z. **Review:** APPROVED --- Great catch. Sleeping on an available SRW word after a lost CAS is a real lost-wakeup, and the regression covers every state. Tested merged with current main: x86_64-apple-darwin hle/loader/victory (220 + 16 native_probe), the arm64 suites, clippy (Windows target) and policy all pass. We suspect this may also explain a 30 s main-loop stall that trips the title's hang watchdog during story loading, and we're testing that now.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!31
No description provided.