victory-gpu: fix protect/open_unlocked race leaving block read-only #457

Open
github-bot wants to merge 2 commits from github-bot/gh-457 into main AGit
Owner

@⁠kwaadrat on GitHub · 2026-12-02 11:47 UTC · original
kwaadrat:fix/watch-protect-opening-race → main · +63 −10 · 1 files · 2 commits · open

Fixes a race condition between protect_alias / protect_here and open_unlocked (VICTORY_FAULT_UNLOCKED=1) that could leave a block PAGE_READONLY in OS memory while PROTECTED and g.protected are cleared, causing subsequent writes to crash with unhandled write fault ... protected=false opening=false open_done=false.

Root cause

  1. protect_alias and protect_here set PROTECTED.set(block) ahead of VirtualProtect(..., PAGE_READONLY).
  2. While VirtualProtect(..., PAGE_READONLY) has not yet completed (which can be a multi-ms wineserver IPC under Wine/Proton), another thread can fault, see PROTECTED == 1, enter open_unlocked(block) without guards(), set OPENING, call VirtualProtect(..., PAGE_READWRITE), and push Opened to OPENED.
  3. protect_alias / protect_here then completes VirtualProtect(..., PAGE_READONLY) (re-protecting the block) and inserts block into g.protected.
  4. The next settle() pops Opened, removes block from g.protected, and clears PROTECTED + OPENING, leaving the block PAGE_READONLY with PROTECTED == 0.
  5. Similarly, if VirtualProtect in open_unlocked failed (!done), open_unlocked still pushed Opened to OPENED, causing settle() to clear PROTECTED even though the block remained read-only.

Fix

  • Extract reopen_if_opening(block) and call it after VirtualProtect(..., PAGE_READONLY) in protect_alias and protect_here: if OPENING is set on block, restore PAGE_READWRITE, clear PROTECTED, and keep block out of g.protected / g.clean.
  • In open_unlocked, if !done, clear TOLD / OPENING and return false without pushing to OPENED.
  • Add unit test protect_here_restores_block_when_opening_races in crates/victory-gpu/src/watch.rs.

Verification

  • Verified on Linux (Proton Experimental / vkd3d-proton, RTX 3060 12 GB) with VICTORY_FAULT_UNLOCKED=1: eliminated the reproducible multi-thread write ...: read-only crash during startup/prologue (previously observed in #407).
**@⁠kwaadrat** on GitHub · 2026-12-02 11:47 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/457) `kwaadrat:fix/watch-protect-opening-race` → `main` · +63 −10 · 1 files · 2 commits · open Fixes a race condition between `protect_alias` / `protect_here` and `open_unlocked` (`VICTORY_FAULT_UNLOCKED=1`) that could leave a block `PAGE_READONLY` in OS memory while `PROTECTED` and `g.protected` are cleared, causing subsequent writes to crash with `unhandled write fault ... protected=false opening=false open_done=false`. ### Root cause 1. `protect_alias` and `protect_here` set `PROTECTED.set(block)` ahead of `VirtualProtect(..., PAGE_READONLY)`. 2. While `VirtualProtect(..., PAGE_READONLY)` has not yet completed (which can be a multi-ms `wineserver` IPC under Wine/Proton), another thread can fault, see `PROTECTED == 1`, enter `open_unlocked(block)` without `guards()`, set `OPENING`, call `VirtualProtect(..., PAGE_READWRITE)`, and push `Opened` to `OPENED`. 3. `protect_alias` / `protect_here` then completes `VirtualProtect(..., PAGE_READONLY)` (re-protecting the block) and inserts `block` into `g.protected`. 4. The next `settle()` pops `Opened`, removes `block` from `g.protected`, and clears `PROTECTED` + `OPENING`, leaving the block `PAGE_READONLY` with `PROTECTED == 0`. 5. Similarly, if `VirtualProtect` in `open_unlocked` failed (`!done`), `open_unlocked` still pushed `Opened` to `OPENED`, causing `settle()` to clear `PROTECTED` even though the block remained read-only. ### Fix - Extract `reopen_if_opening(block)` and call it after `VirtualProtect(..., PAGE_READONLY)` in `protect_alias` and `protect_here`: if `OPENING` is set on `block`, restore `PAGE_READWRITE`, clear `PROTECTED`, and keep `block` out of `g.protected` / `g.clean`. - In `open_unlocked`, if `!done`, clear `TOLD` / `OPENING` and return `false` without pushing to `OPENED`. - Add unit test `protect_here_restores_block_when_opening_races` in `crates/victory-gpu/src/watch.rs`. ### Verification - Verified on Linux (Proton Experimental / `vkd3d-proton`, RTX 3060 12 GB) with `VICTORY_FAULT_UNLOCKED=1`: eliminated the reproducible multi-thread `write ...: read-only` crash during startup/prologue (previously observed in #407).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 11:52:29 +00:00
Author
Owner

@⁠GValdezN on GitHub · 2026-12-02 16:51 UTC · original

This is a correctness fix that should land before the perf PRs. The race window is real: PROTECTED set before VirtualProtect(PAGE_READONLY) completes means a concurrent fault can enter open_unlocked, push to OPENED, and later settle() clears PROTECTED while the block is still read-only.

The reopen_if_opening() recovery is clean — check OPENING, restore PAGE_READWRITE, clear PROTECTED. The early return in open_unlocked on !done correctly avoids pushing a failed open to OPENED.

Minor note: in protect_here's fast path, note(g, from, to) runs before the OPENING check, so blocks later removed from g.protected via reopen_if_opening may still appear in notes. Is that intentional (the note is best-effort) or should it be guarded?

This interacts with #434's reopen_striped_block — both handle the same class of race. If #434 is rebased on top of this, it could potentially simplify its recovery path.

**@⁠GValdezN** on GitHub · 2026-12-02 16:51 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7352545030) This is a correctness fix that should land before the perf PRs. The race window is real: `PROTECTED` set before `VirtualProtect(PAGE_READONLY)` completes means a concurrent fault can enter `open_unlocked`, push to `OPENED`, and later `settle()` clears `PROTECTED` while the block is still read-only. The `reopen_if_opening()` recovery is clean — check OPENING, restore PAGE_READWRITE, clear PROTECTED. The early return in `open_unlocked` on `!done` correctly avoids pushing a failed open to OPENED. Minor note: in `protect_here`'s fast path, `note(g, from, to)` runs before the OPENING check, so blocks later removed from `g.protected` via `reopen_if_opening` may still appear in notes. Is that intentional (the note is best-effort) or should it be guarded? This interacts with #434's `reopen_striped_block` — both handle the same class of race. If #434 is rebased on top of this, it could potentially simplify its recovery path.
Author
Owner

@⁠kwaadrat on GitHub · 2026-12-02 20:33 UTC · original

Thanks for the review!

Regarding note(g, from, to) in protect_here's fast path: note is the local closure that inserts from..to into g.protected and g.clean (both guarded by g: &mut Guards). Right after it, the if OPENING.any(from, to) block loops over from..to and immediately removes any reopened block via g.protected.remove(block), g.clean.remove(block), and PUBLISHED.clear(block) before returning (while keeping insert_all(from..to) intact for the common uncontended case).

This invariant is also covered by protect_here_restores_block_when_opening_races, which asserts !g.protected.contains(b) and !g.clean.contains(b).

**@⁠kwaadrat** on GitHub · 2026-12-02 20:33 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7357919408) Thanks for the review! Regarding `note(g, from, to)` in `protect_here`'s fast path: `note` is the local closure that inserts `from..to` into `g.protected` and `g.clean` (both guarded by `g: &mut Guards`). Right after it, the `if OPENING.any(from, to)` block loops over `from..to` and immediately removes any reopened block via `g.protected.remove(block)`, `g.clean.remove(block)`, and `PUBLISHED.clear(block)` before returning (while keeping `insert_all(from..to)` intact for the common uncontended case). This invariant is also covered by `protect_here_restores_block_when_opening_races`, which asserts `!g.protected.contains(b)` and `!g.clean.contains(b)`.
Author
Owner

@⁠tillo88 on GitHub · 2026-12-03 00:38 UTC · original

Replay-bench measurement: no measurable difference on v0.8.3.3 in 3 of 4 runs; one slow run, cause unknown

Measured: PR head f001988 merged onto release v0.8.3.3 (same code as main 7dbcdcb), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 4 runs of v0.8.3.3 without the PR, same route (~6.5 min).

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release run 1 36.54 19.08 29.00 50.42 60.01 32
release run 2 36.45 20.15 28.10 38.59 45.48 13
release run 3 36.15 19.30 29.35 38.74 46.08 10
release run 4 36.69 19.55 28.20 37.38 46.28 15
PR #457 run 1 36.38 19.55 28.90 39.30 46.65 13
PR #457 run 2 26.91 24.42 35.30 153.31 175.25 248
PR #457 run 3 36.36 20.14 28.10 37.95 45.29 14
PR #457 run 4 36.24 20.25 28.60 38.71 45.89 14

Mean fps over the three normal runs: PR 36.33 vs release 36.46 (-0.4%), which is no measurable difference. Run 2 was slower for part of the route (mean 26.91 fps, 248 frames over 100 ms); the layer logged no error in that run. Earlier the same evening, before any PR was measured, two release runs with identical switches fell into a similar slow state (6-9 fps average) for an unknown reason, so we cannot tell whether this run is related to the PR. Including it, the PR mean is 33.97 fps.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

**@⁠tillo88** on GitHub · 2026-12-03 00:38 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7363429303) **Replay-bench measurement: no measurable difference on v0.8.3.3 in 3 of 4 runs; one slow run, cause unknown** Measured: PR head `f001988` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), 4 measured runs in two separate jobs (each job after a warm-up run that is not counted), against 4 runs of v0.8.3.3 without the PR, same route (~6.5 min). | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release run 1 | 36.54 | 19.08 | 29.00 | 50.42 | 60.01 | 32 | | release run 2 | 36.45 | 20.15 | 28.10 | 38.59 | 45.48 | 13 | | release run 3 | 36.15 | 19.30 | 29.35 | 38.74 | 46.08 | 10 | | release run 4 | 36.69 | 19.55 | 28.20 | 37.38 | 46.28 | 15 | | PR #457 run 1 | 36.38 | 19.55 | 28.90 | 39.30 | 46.65 | 13 | | PR #457 run 2 | 26.91 | 24.42 | 35.30 | 153.31 | 175.25 | 248 | | PR #457 run 3 | 36.36 | 20.14 | 28.10 | 37.95 | 45.29 | 14 | | PR #457 run 4 | 36.24 | 20.25 | 28.60 | 38.71 | 45.89 | 14 | Mean fps over the three normal runs: PR 36.33 vs release 36.46 (-0.4%), which is **no measurable difference**. Run 2 was slower for part of the route (mean 26.91 fps, 248 frames over 100 ms); the layer logged no error in that run. Earlier the same evening, before any PR was measured, two release runs with identical switches fell into a similar slow state (6-9 fps average) for an unknown reason, so we cannot tell whether this run is related to the PR. Including it, the PR mean is 33.97 fps. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
Author
Owner

@⁠kwaadrat on GitHub · 2026-12-03 06:56 UTC · original

Thanks for running the replay-bench on Windows 11, @⁠tillo88!

Seeing zero crashes and no measurable delta in the 3 normal runs (36.33 vs 36.46 fps, -0.4%) is the expected result here: #457 is purely a correctness fix, and the extra work only triggers when OPENING.any(from, to) sees a concurrent open_unlocked race — the uncontended fast path stays unchanged.

The race itself shows up on Linux / Proton Experimental (vkd3d-proton, VICTORY_FAULT_UNLOCKED=1): without #457, protect_alias/protect_here racing with open_unlocked can leave a block PAGE_READONLY (0xc0000005 write fault), whereas with #457 (alongside #460) we ran crash-free across multiple 7–10 min Proton runs.

**@⁠kwaadrat** on GitHub · 2026-12-03 06:56 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7370405028) Thanks for running the replay-bench on Windows 11, @⁠tillo88! Seeing zero crashes and no measurable delta in the 3 normal runs (`36.33` vs `36.46 fps`, `-0.4%`) is the expected result here: #457 is purely a correctness fix, and the extra work only triggers when `OPENING.any(from, to)` sees a concurrent `open_unlocked` race — the uncontended fast path stays unchanged. The race itself shows up on **Linux / Proton Experimental** (`vkd3d-proton`, `VICTORY_FAULT_UNLOCKED=1`): without #457, `protect_alias`/`protect_here` racing with `open_unlocked` can leave a block `PAGE_READONLY` (`0xc0000005` write fault), whereas with #457 (alongside #460) we ran crash-free across multiple 7–10 min Proton runs.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/457/head:github-bot/gh-457'
git switch 'github-bot/gh-457'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-457'
git switch 'github-bot/gh-457'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-457'
git switch 'github-bot/gh-457'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-457'
git switch 'main'
git merge --squash 'github-bot/gh-457'
git switch 'main'
git merge --ff-only 'github-bot/gh-457'
git switch 'main'
git merge 'github-bot/gh-457'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.