fix(gpu/watch): restore read-write if open_unlocked races with VirtualProtect(PAGE_READONLY) #297

Manually merged
github merged 1 commit from github-bot/gh-297 into gh-pr/297/base 2026-12-01 23:20:54 +00:00 AGit
Owner

@⁠Androtim on GitHub · 2026-12-01 23:07 UTC · original
Androtim:fix/watch-protect-vs-open-unlocked-race → main · +130 −12 · 1 files · 1 commits · merged 2026-12-01 23:19 UTC

Summary

Fixes the intermittent STATUS_ACCESS_VIOLATION (0xc0000005) race in crates/victory-gpu/src/watch.rs reproduced in CI (land::tests::landings_beside_the_title_and_its_refreshes_lose_nothing).

Root Cause

  1. protect_here and protect_alias set PROTECTED.set(block) before calling VirtualProtect(..., PAGE_READONLY).
  2. A concurrent thread hitting on_write saw PROTECTED == 1 and entered open_unlocked(block): set OPENING(block) = 1, called VirtualProtect(block, PAGE_READWRITE) before protect_here / protect_alias's VirtualProtect(PAGE_READONLY) completed, set OPEN_DONE(block) = 1, and queued Opened { block }.
  3. protect_here / protect_alias then overwrote the block's protection back to PAGE_READONLY and inserted block into g.protected.
  4. The next shard lock settled Opened { block }, removing block from g.protected and clearing PROTECTED / OPENING without restoring PAGE_READWRITE — leaving the block permanently PAGE_READONLY in Windows while untracked in g.protected, so the next write fault (AtomicU32::swap at land/tests.rs:297) fell through on_write as EXCEPTION_CONTINUE_SEARCH (0xc0000005).

(Reproduced locally 2/100 times under default settings and reliably under VICTORY_STORE_BESIDE=0; symbolized via addr2line on 0x989c29 -> core::sync::atomic::AtomicU32::swap.)

Fix (crates/victory-gpu/src/watch.rs)

  1. Added restore_rw_block(block) helper and a Dekker post-check after VirtualProtect(..., PAGE_READONLY) in both protect_here and protect_alias:
    • In the fast-path batch of protect_here, !OPENING.any(from, to) adds zero extra syscalls on the uncontended path; if any block in from..to has OPENING set, only the raced block is restored to PAGE_READWRITE and excluded from g.protected / g.clean / PROTECTED.
    • Same post-check in protect_here's per-block path and protect_alias.
  2. In open_unlocked, if VirtualProtect(PAGE_READWRITE) fails or encounters non-RO/RW protection, undo TOLD and OPENING and return false without pushing a bogus Opened record so settle cannot strip g.protected without unprotecting.
  3. Added regression unit test protect_here_and_protect_alias_restore_rw_when_opening_races.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy: asset policy: 319 files ok
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked watch:: -- --test-threads=1: 56 passed; 0 failed
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked land:: -- --test-threads=1: 7 passed; 0 failed (1 ignored benchmark)
  • Stress test (land::tests::landings_beside_the_title_and_its_refreshes_lose_nothing --exact):
    • Default env: 100 / 100 passed (0 crashes)
    • VICTORY_STORE_BESIDE=0: 100 / 100 passed (0 crashes)
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --locked --all-targets -- -D warnings: 0 warnings
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --locked --all-targets --features diagnostics -- -D warnings: 0 warnings
  • Plain --release --bin MiniVI --locked: compiled cleanly, objdump -p imports standard Windows DLLs only.
**@⁠Androtim** on GitHub · 2026-12-01 23:07 UTC · [original](https://github.com/Androtim/MiniVI/pull/297) `Androtim:fix/watch-protect-vs-open-unlocked-race` → `main` · +130 −12 · 1 files · 1 commits · merged 2026-12-01 23:19 UTC ### Summary Fixes the intermittent `STATUS_ACCESS_VIOLATION` (`0xc0000005`) race in `crates/victory-gpu/src/watch.rs` reproduced in CI (`land::tests::landings_beside_the_title_and_its_refreshes_lose_nothing`). ### Root Cause 1. `protect_here` and `protect_alias` set `PROTECTED.set(block)` *before* calling `VirtualProtect(..., PAGE_READONLY)`. 2. A concurrent thread hitting `on_write` saw `PROTECTED == 1` and entered `open_unlocked(block)`: set `OPENING(block) = 1`, called `VirtualProtect(block, PAGE_READWRITE)` *before* `protect_here` / `protect_alias`'s `VirtualProtect(PAGE_READONLY)` completed, set `OPEN_DONE(block) = 1`, and queued `Opened { block }`. 3. `protect_here` / `protect_alias` then overwrote the block's protection back to `PAGE_READONLY` and inserted `block` into `g.protected`. 4. The next shard lock settled `Opened { block }`, removing `block` from `g.protected` and clearing `PROTECTED` / `OPENING` without restoring `PAGE_READWRITE` — leaving the block permanently `PAGE_READONLY` in Windows while untracked in `g.protected`, so the next write fault (`AtomicU32::swap` at `land/tests.rs:297`) fell through `on_write` as `EXCEPTION_CONTINUE_SEARCH` (`0xc0000005`). (Reproduced locally `2/100` times under default settings and reliably under `VICTORY_STORE_BESIDE=0`; symbolized via `addr2line` on `0x989c29` -> `core::sync::atomic::AtomicU32::swap`.) ### Fix (`crates/victory-gpu/src/watch.rs`) 1. Added `restore_rw_block(block)` helper and a Dekker post-check after `VirtualProtect(..., PAGE_READONLY)` in both `protect_here` and `protect_alias`: - In the fast-path batch of `protect_here`, `!OPENING.any(from, to)` adds zero extra syscalls on the uncontended path; if any block in `from..to` has `OPENING` set, only the raced block is restored to `PAGE_READWRITE` and excluded from `g.protected` / `g.clean` / `PROTECTED`. - Same post-check in `protect_here`'s per-block path and `protect_alias`. 2. In `open_unlocked`, if `VirtualProtect(PAGE_READWRITE)` fails or encounters non-RO/RW protection, undo `TOLD` and `OPENING` and return `false` without pushing a bogus `Opened` record so `settle` cannot strip `g.protected` without unprotecting. 3. Added regression unit test `protect_here_and_protect_alias_restore_rw_when_opening_races`. --- ### Verification - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy`: `asset policy: 319 files ok` - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked watch:: -- --test-threads=1`: `56 passed; 0 failed` - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-gpu --lib --locked land:: -- --test-threads=1`: `7 passed; 0 failed` (`1 ignored` benchmark) - Stress test (`land::tests::landings_beside_the_title_and_its_refreshes_lose_nothing --exact`): - Default env: **`100 / 100` passed** (`0` crashes) - `VICTORY_STORE_BESIDE=0`: **`100 / 100` passed** (`0` crashes) - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --locked --all-targets -- -D warnings`: 0 warnings - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --locked --all-targets --features diagnostics -- -D warnings`: 0 warnings - Plain `--release --bin MiniVI --locked`: compiled cleanly, `objdump -p` imports standard Windows DLLs only.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 23:10:23 +00:00
github-bot changed target branch from main to gh-pr/297/base 2026-12-01 23:20:53 +00:00
github manually merged commit 1517a0a54b into gh-pr/297/base 2026-12-01 23:20:54 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 23:18 UTC · original

Audited full diff (1517a0a5) and Dekker ordering across open_unlocked, protect_here, protect_alias, and settle:

  • Holding the guard shard lock g prevents settle from clearing OPENING while protect_here/protect_alias execute, and the VirtualProtect(PAGE_READONLY) syscall orders the protection change before the OPENING.any(...) check.
  • Zero extra syscalls on the uncontended fast path (!OPENING.any(from, to)), clean rollback of TOLD/OPENING when VirtualProtect(PAGE_READWRITE) rejects non-RO/RW memory in open_unlocked, and solid unit + 200-run stress verification (100/100 default + 100/100 VICTORY_STORE_BESIDE=0).
  • CI green (46766764774), no conflicts, zero new VICTORY_* toggles. Merging now.
**@⁠Androtim** on GitHub · 2026-12-01 23:18 UTC · [original](https://github.com/Androtim/MiniVI/pull/297#issuecomment-7329184588) Audited full diff (`1517a0a5`) and Dekker ordering across `open_unlocked`, `protect_here`, `protect_alias`, and `settle`: - Holding the guard shard lock `g` prevents `settle` from clearing `OPENING` while `protect_here`/`protect_alias` execute, and the `VirtualProtect(PAGE_READONLY)` syscall orders the protection change before the `OPENING.any(...)` check. - Zero extra syscalls on the uncontended fast path (`!OPENING.any(from, to)`), clean rollback of `TOLD`/`OPENING` when `VirtualProtect(PAGE_READWRITE)` rejects non-RO/RW memory in `open_unlocked`, and solid unit + 200-run stress verification (`100/100` default + `100/100` `VICTORY_STORE_BESIDE=0`). - CI green (`46766764774`), no conflicts, zero new `VICTORY_*` toggles. Merging now.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Androtim--MiniVI!297
No description provided.