Keep pinned blocks writable on guard()'s fast path too #60

Closed
github-bot wants to merge 1 commit from github-bot/gh-60 into main AGit
Owner

@⁠romainhedouin on GitHub · 2026-11-25 13:13 UTC · original
romainhedouin:fix/guard-fast-path-pins → main · +72 −5 · 1 files · 1 commits · closed without merging

Bug fix. Commit 579a8d9 made blocks the kernel is about to write (an asynchronous read) stay writable for two seconds ("pins"), because guarding them again made the kernel's write fail and the title stop. But guard() has two paths, and only the slow one checked pins. When a checked range starts at an already-guarded block (the common case: part of a larger guarded buffer), the fast path guarded a pinned block again anyway. Now the fast path treats a pinned block exactly like the slow path does (left writable, reported written, its region marked changed), and both paths drop pins that have ended (the fast path never did, so pins could pile up).

Range starting at a guarded block, a later block pinned Before After
Pinned block's protection made read-only again stays writable
Kernel read into it can fail lands
Reported written once at every check until the pin ends
Ended pins dropped only on the slow path on both paths

Adds a_pinned_block_is_left_writable_by_a_check_from_a_guarded_one (a real ReadFile into the pinned block after a check from the guarded one). Type-checked for Windows (cargo clippy --target x86_64-pc-windows-msvc, with and without --all-features, no new warnings); the test needs Windows and was not run.

🤖 Generated with Claude Code

**@⁠romainhedouin** on GitHub · 2026-11-25 13:13 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/60) `romainhedouin:fix/guard-fast-path-pins` → `main` · +72 −5 · 1 files · 1 commits · closed without merging Bug fix. Commit 579a8d9 made blocks the kernel is about to write (an asynchronous read) stay writable for two seconds ("pins"), because guarding them again made the kernel's write fail and the title stop. But `guard()` has two paths, and only the slow one checked pins. When a checked range starts at an already-guarded block (the common case: part of a larger guarded buffer), the fast path guarded a pinned block again anyway. Now the fast path treats a pinned block exactly like the slow path does (left writable, reported written, its region marked changed), and both paths drop pins that have ended (the fast path never did, so pins could pile up). | Range starting at a guarded block, a later block pinned | Before | After | |---|---|---| | Pinned block's protection | made read-only again | stays writable | | Kernel read into it | can fail | lands | | Reported written | once | at every check until the pin ends | | Ended pins dropped | only on the slow path | on both paths | Adds `a_pinned_block_is_left_writable_by_a_check_from_a_guarded_one` (a real `ReadFile` into the pinned block after a check from the guarded one). Type-checked for Windows (`cargo clippy --target x86_64-pc-windows-msvc`, with and without `--all-features`, no new warnings); the test needs Windows and was not run. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:34:45 +00:00
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 20:10 UTC · original

Ran this on Windows, since the description says the test was not run: RX 7900 XT, Windows 11 26300, Rust 1.98.1, 6f6e2aa merged on main 4f62c2a (no conflicts).

  • a_pinned_block_is_left_writable_by_a_check_from_a_guarded_one: passed 3 of 3 runs.
  • With only the fast path's g.pinned(block) branch taken out (keeping the test), it fails 3 of 3 with the kernel can still write it. So the test catches the race.
  • watch:: with --test-threads=1: 8 passed, 0 failed.
  • Full victory-gpu --lib, default threads, two runs: 114 passed / 0 failed, then 113 / 1. That one failure was frame::tests::waits_end_on_ticks_and_count_frames, which also fails on main under load.

The same race is what #211 reports. A local reproduction on 4f62c2a does the following: guard a two-block view, writing() into the second block, check the range again (fast path), then ReadFile into that block. On 4f62c2a the ReadFile fails with 998 (ERROR_NOACCESS) every time; with this change it succeeds. #237 works around the same symptom from the DirectStorage side, by retrying a rejected read through a staging buffer. This PR fixes the cause in guard(), so the two don't conflict, but with this one the retry should rarely be needed. Not tested in the game.

**@⁠adrianmjim** on GitHub · 2026-11-26 20:10 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/60#issuecomment-7202382465) Ran this on Windows, since the description says the test was not run: RX 7900 XT, Windows 11 26300, Rust 1.98.1, `6f6e2aa` merged on `main` `4f62c2a` (no conflicts). - `a_pinned_block_is_left_writable_by_a_check_from_a_guarded_one`: passed 3 of 3 runs. - With only the fast path's `g.pinned(block)` branch taken out (keeping the test), it fails 3 of 3 with `the kernel can still write it`. So the test catches the race. - `watch::` with `--test-threads=1`: 8 passed, 0 failed. - Full `victory-gpu --lib`, default threads, two runs: 114 passed / 0 failed, then 113 / 1. That one failure was `frame::tests::waits_end_on_ticks_and_count_frames`, which also fails on `main` under load. The same race is what #211 reports. A local reproduction on `4f62c2a` does the following: guard a two-block view, `writing()` into the second block, check the range again (fast path), then `ReadFile` into that block. On `4f62c2a` the `ReadFile` fails with 998 (`ERROR_NOACCESS`) every time; with this change it succeeds. #237 works around the same symptom from the DirectStorage side, by retrying a rejected read through a staging buffer. This PR fixes the cause in `guard()`, so the two don't conflict, but with this one the retry should rarely be needed. Not tested in the game.
github-bot closed this pull request 2026-11-27 01:00:02 +00:00
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 21:47 UTC · original

Follow-up on #237: measured there (#237), it restages only on 1784, not on 998, and the reproduction of this race still fails with 998 on it (3 of 3). So the two are not alternatives: #60 removes the race in guard(), #237 covers a rejected read whose protection changed mid-read.

**@⁠adrianmjim** on GitHub · 2026-11-26 21:47 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/60#issuecomment-7204055927) Follow-up on #237: measured there (https://git.gtahub.st/github/Belobug--VIctory-Belobug-AMD/pulls/237), it restages only on 1784, not on 998, and the reproduction of this race still fails with 998 on it (3 of 3). So the two are not alternatives: #60 removes the race in `guard()`, #237 covers a rejected read whose protection changed mid-read.
Author
Owner

@⁠romainhedouin on GitHub · 2026-11-26 22:24 UTC · original

Closing: a-publier has no time-based pins. Kernel writes are scoped (kernel_writing, watch.rs:3249), and left_alone is checked on every path that makes a block read-only again, guard's fast path included.

**@⁠romainhedouin** on GitHub · 2026-11-26 22:24 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/60#issuecomment-7204650050) Closing: `a-publier` has no time-based pins. Kernel writes are scoped (`kernel_writing`, watch.rs:3249), and `left_alone` is checked on every path that makes a block read-only again, `guard`'s fast path included.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.