You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GPU: a write-back job that panics, or is never run, gives its pages back (Fixes #32) - #203
The three deferred CodeRabbit items from #17 (VICTORY_ASYNC_WRITEBACK=1, off by default), each checked against main today, with what was still missing fixed.
1. A snapshot per submission — already on main, nothing to fix
Shadow::writing (crates/victory-gpu/src/shadow.rs:2082) waits for any job still holding the range (wait_jobs, shadow.rs:2055) before it takes the pre-submission bytes, and takes them only for the parts no pending range holds yet (pending, shadow.rs:2095-2120); the range leaves pending only in copy_back_done (shadow.rs:2439), i.e. after the job that put its bytes back. So the worker's copy_back always compares against the bytes of its submission, never the latest state — and a later submission's same range finds the earlier job's entry and does not take a second snapshot. The lazy job.back/job.writes arrays belong to one submission (host::write_back_job, host.rs:2909).
2. The no-access read path — already on main, nothing to fix
The vectored handler (watch.rs:997) asks back_pending(at)before it looks at the access type at all, so a read, a write and an execute (and a guard fault) on a page a job holds all wait for that job; only a fault on a page no job holds is classified by access type. Kernel writes of ours are ordered with back_wait (host.rs:4082), and the worker itself never waits for its own job (back_open + EXCEPTION_CONTINUE_EXECUTION, watch.rs:1017).
3. The unwind guard — was incomplete; fixed here
The Release guard released the blocks on a panic but did not finish the job's Done, and nothing covered the other two ways a job ends without running:
a title thread that had faulted on a page of the panicking job waited on Done forever;
a worker that died on a panic left every later job unrun (submit's send fails) and dropped with its blocks still held, so those pages stayed PAGE_NOACCESS for the rest of the run.
Now Job's own Drop gives its blocks back and then finishes Done (order matters: a waiter waking on finish must find its page accessible), so the normal path, a panic in the merge, an empty job dropped on the floor and a job the worker never takes are all covered by one path; the Release guard is gone. The worker wraps each job in catch_unwind, says so once (a write-back job panicked: its blocks were given back and the worker goes on) and carries on with the next submission's copy-back instead of dying.
Tests
writeback::tests::a_job_dropped_without_running_gives_its_pages_back — a job holding two real pages (taken with back_begin over a VirtualAlloc of the test's own) dropped without running: VirtualQuery says PAGE_READWRITE again, and Done is finished.
writeback::tests::a_panicking_job_finishes_and_lets_its_waiters_go — a job whose body panics finishes, and a thread already waiting on it is let go.
writeback::tests::done_waits_for_the_finish unchanged. watch::BLOCK is now pub(crate) for the first test (one page, no behaviour change).
victory-gpu under Wine, VICTORY_TEST_ADAPTER=hw (--test-threads=1): 209 passed, 0 failed, 2 ignored, no skipped: no device.
cargo xtask policy: 244 files ok; name check clean
Runtime: release build, dry run without --execute: build.toml matched, 2 hooks, every module missing=0.
Off switch
None needed: the async copy-back is already opt-in (VICTORY_ASYNC_WRITEBACK=1, off by default) and nothing on the default path changes; no joint-session line, since the default run is untouched.
Notes
Not tested in a game run. #32 can be closed once this is merged; #17's flag may default on after a run with the worker exercised.
Summary by CodeRabbit
Bug Fixes
Improved GPU write-back reliability: resources are released when jobs finish or are dropped, and a failure in one job no longer prevents later jobs from being processed.
The async copy-back (VICTORY_ASYNC_WRITEBACK=1, off by default) holds the
title's blocks inaccessible while a job puts the GPU's bytes back. The guard
that gave them back covered a panic inside the merge only: it did not finish
the job's `Done`, so a title thread that had faulted on one of its pages waited
for a job that would never finish, and a worker that died on a panic left every
later submission's job on the floor - `send` fails, the job is dropped with its
blocks still held, and those pages stay PAGE_NOACCESS for the rest of the run.
A job now gives its blocks back and finishes itself in its own `Drop`, which
covers the normal path, a panic in the merge, a job the worker never takes and
an empty job dropped on the floor; the worker catches a job's panic, says so
once and goes on to the next job. The release order is the one the waiters
need: blocks first, then `Done`.
Refs #32.
Job cleanup and completion crates/victory-gpu/src/watch.rs, crates/victory-gpu/src/writeback.rs
BLOCK is crate-visible for write-back jobs and copies. Dropping a job releases its protected blocks and finishes its completion signal. Tests check cleanup when a job is dropped without running and during panic unwinding.
Worker execution and panic isolation crates/victory-gpu/src/writeback.rs
The worker performs merge and bottom-of-pipe writes through run. It catches panics per job, logs that the job’s copy-back was lost, and continues to later jobs.
Possibly related issues
pablogonz12/Scarlight#64 concerns the write-back block-release boundary. This change also updates job block release, but does not claim to fix the issue’s flaky test.
Possibly related PRs
pablogonz12/Scarlight#146: Both changes depend on the back_release and hold-release contract in watch.rs and writeback.rs.
pablogonz12/Scarlight#17: This PR changes panic cleanup and job-drop behavior in the async copy-back worker introduced by #17.
Suggested reviewers:fxceless
Change: Bug fix
Merge Risk: Minimal · up to ea353
This change makes write-back jobs return their protected pages and wake waiters when a job panics or is never run, and the worker keeps going. The feature is off by default. One speculative concern remains about stale shadow state after a panic, with no demonstrated trigger. No merge-blocking risk is established.
Security architecture risk: Moderate · up to ea353
The change prevents a failed write-back from leaving threads waiting indefinitely for that job. However, a failed job can leave an old snapshot behind, and a later submission that reuses the same memory may use it to decide which bytes to write back. This risk is limited to the opt-in asynchronous mode.
Retained concerns
Medium · reliability · inferred: If a merge panics before retiring a pending range, the job can finish and the worker can resume while the range still holds its old snapshot. A later declaration skips a fresh snapshot for those bytes, so a later merge can miss new GPU writes or copy back bytes that the later submission did not change. The base also left pending state on panic, but did not continue processing jobs on that worker.
Security review details
Security Blast Radius
inferred — The identified integrity risk is within the title process: it concerns later submissions that reuse affected ranges in the same Shadow mapping while asynchronous write-back is enabled. The inspected path does not establish cross-process reach or added privileges.
Resilience and Maintainability Implications
observed — Completion now means the job no longer holds its blocks, not that the results were published successfully. An unwind is logged, but the completion handle does not communicate failure to waiters.
Hardening Proposals
proposed — Distinguish termination from successful publication. Before allowing later work to reuse an affected range, retire or invalidate the failed job's snapshot and pending state without clearing another job's ownership. If partial copy-back cannot be reconciled, quarantine the affected range or stop asynchronous processing rather than reuse the stale state.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
The reason will be displayed to describe this comment to others. Learn more.
Additional comments: 1
crates/victory-gpu/src/writeback.rs-113-113: 1
113-113: Major · Data Integrity & Integration · Heavy lift
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Retire unfinished Shadow ranges before finishing Done.
If hand_back_now panics before Shadow::copy_back_done, this destructor leaves that range in Shadow::pending. The worker then continues with abandoned submission state. Shadow::update skips the range, and an identical Shadow::writing declaration returns without taking a new snapshot. Later submissions can therefore use stale bytes, not just lose the failed submission's copy-back. (raw.githubusercontent.com)
Add abandonment cleanup before finish(). Retire only unfinished registrations owned by this job; do not clear registrations from a later submission.
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: bc549b26-2b33-4de1-9b70-09c89593975e
Commits
Reviewing files that changed from the base of the PR and between 9b4c824 and ea3531a.
Files selected for processing: 2
crates/victory-gpu/src/watch.rs
crates/victory-gpu/src/writeback.rs
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #32.
The three deferred CodeRabbit items from #17 (
VICTORY_ASYNC_WRITEBACK=1, off by default), each checked againstmaintoday, with what was still missing fixed.1. A snapshot per submission — already on
main, nothing to fixShadow::writing(crates/victory-gpu/src/shadow.rs:2082) waits for any job still holding the range (wait_jobs,shadow.rs:2055) before it takes the pre-submission bytes, and takes them only for the parts no pending range holds yet (pending,shadow.rs:2095-2120); the range leavespendingonly incopy_back_done(shadow.rs:2439), i.e. after the job that put its bytes back. So the worker'scopy_backalways compares against the bytes of its submission, never the latest state — and a later submission's same range finds the earlier job's entry and does not take a second snapshot. The lazyjob.back/job.writesarrays belong to one submission (host::write_back_job,host.rs:2909).2. The no-access read path — already on
main, nothing to fixThe vectored handler (
watch.rs:997) asksback_pending(at)before it looks at the access type at all, so a read, a write and an execute (and a guard fault) on a page a job holds all wait for that job; only a fault on a page no job holds is classified by access type. Kernel writes of ours are ordered withback_wait(host.rs:4082), and the worker itself never waits for its own job (back_open+EXCEPTION_CONTINUE_EXECUTION,watch.rs:1017).3. The unwind guard — was incomplete; fixed here
The
Releaseguard released the blocks on a panic but did not finish the job'sDone, and nothing covered the other two ways a job ends without running:Doneforever;submit'ssendfails) and dropped with its blocks still held, so those pages stayedPAGE_NOACCESSfor the rest of the run.Now
Job's ownDropgives its blocks back and then finishesDone(order matters: a waiter waking onfinishmust find its page accessible), so the normal path, a panic in the merge, an empty job dropped on the floor and a job the worker never takes are all covered by one path; theReleaseguard is gone. The worker wraps each job incatch_unwind, says so once (a write-back job panicked: its blocks were given back and the worker goes on) and carries on with the next submission's copy-back instead of dying.Tests
writeback::tests::a_job_dropped_without_running_gives_its_pages_back— a job holding two real pages (taken withback_beginover aVirtualAllocof the test's own) dropped without running:VirtualQuerysaysPAGE_READWRITEagain, andDoneis finished.writeback::tests::a_panicking_job_finishes_and_lets_its_waiters_go— a job whose body panics finishes, and a thread already waiting on it is let go.writeback::tests::done_waits_for_the_finishunchanged.watch::BLOCKis nowpub(crate)for the first test (one page, no behaviour change).Gates
cargo xwin clippy --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings: clean.victory-gpuunder Wine,VICTORY_TEST_ADAPTER=hw(--test-threads=1): 209 passed, 0 failed, 2 ignored, noskipped: no device.cargo xtask policy: 244 files ok; name check clean--execute:build.toml matched, 2 hooks, every modulemissing=0.Off switch
None needed: the async copy-back is already opt-in (
VICTORY_ASYNC_WRITEBACK=1, off by default) and nothing on the default path changes; no joint-session line, since the default run is untouched.Notes
Not tested in a game run. #32 can be closed once this is merged; #17's flag may default on after a run with the worker exercised.
Summary by CodeRabbit