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

Open
kwaadrat wants to merge 2 commits from upstream-pr/457 into main
kwaadrat commented 2026-12-02 11:47:16 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T11:47:16Z.


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).
<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5605751842 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/457). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T11:47:16Z. --- 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).
GValdezN commented 2026-12-02 16:51:00 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T16:51:00Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7352545030 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7352545030). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T16:51:00Z. --- 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.
kwaadrat commented 2026-12-02 20:33:08 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T20:33:08Z.


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

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7357919408 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7357919408). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T20:33:08Z. --- 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)`.
tillo88 commented 2026-12-03 00:38:54 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-03T00:38:54Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7363429303 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7363429303). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-03T00:38:54Z. --- **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.
kwaadrat commented 2026-12-03 06:56:17 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-03T06:56:17Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7370405028 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/457#issuecomment-7370405028). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-03T06:56:17Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/457:upstream-pr/457'
git switch 'upstream-pr/457'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/457'
git switch 'upstream-pr/457'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/457'
git switch 'upstream-pr/457'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/457'
git switch 'main'
git merge --squash 'upstream-pr/457'
git switch 'main'
git merge --ff-only 'upstream-pr/457'
git switch 'main'
git merge 'upstream-pr/457'
git push origin 'main'
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
readmet3xt-lab/foxhound!457
No description provided.