fix(gpu/texture/ahead): TOCTOU race in claim()/listed() and failed copy-queue batches still flip descriptors #317

Closed
opened 2026-12-02 09:14:12 +00:00 by RUIZ-Enrique-25002136 · 1 comment
RUIZ-Enrique-25002136 commented 2026-12-02 09:14:12 +00:00 (Migrated from github.com)
Contributor

Imported from Androtim/MiniVI. Originally posted by RUIZ-Enrique-25002136 on 2026-12-02T09:14:12Z.


Summary

Two concurrency/error-handling bugs exist in the asynchronous copy-queue texture uploader (crates/victory-gpu/src/texture/ahead.rs):

  1. A TOCTOU race window between setting copy.ahead = PENDING and inserting the job into ON_THEIR_WAY allows concurrent copy-queue and direct-queue uploads/state transitions to the same ID3D12Resource.
  2. When Worker::copy(&jobs, value) returns Err, Worker::work still calls finish(jobs) unconditionally, marking failed textures uploaded and flipping shader-visible descriptors to uninitialized VRAM (and resetting a potentially still-in-flight command allocator).

Evidence (file:line & Code Proof)

Bug A — TOCTOU Window Between claim() and listed()

  • crates/victory-gpu/src/texture/ahead.rs:107-110, 125-147 (claim):
    if copy.ahead.compare_exchange(NONE, PENDING, Relaxed, Relaxed).is_err() {
        return turned_down();
    }
    super::PICKING.set(Some(there));
    let staging = super::staging(resource, &copy, first);
    ...
    let mut way = ON_THEIR_WAY.lock().unwrap_or_else(|e| e.into_inner());
    way.push((job.copy.serial, job.clone()));
    
  • crates/victory-gpu/src/texture/ahead.rs:177-187 (listed):
    if copy.ahead.compare_exchange(NONE, LISTED, Relaxed, Relaxed) != Err(PENDING) {
        return;
    }
    let mut way = ON_THEIR_WAY.lock().unwrap_or_else(|e| e.into_inner());
    while way.iter().any(|(serial, _)| *serial == copy.serial) && ... { ... }
    
    Between CAS NONE -> PENDING (line 125) and way.push(...) (line 144), claim() allocates staging outside ON_THEIR_WAY. A concurrent listed(&copy) sees Err(PENDING), locks ON_THEIR_WAY, finds no entry for copy.serial, and returns immediately—allowing the direct-queue list to copy into copy.texture concurrently with the copy-queue upload. Likewise, a concurrent claim() at lines 107-109 sees PENDING with no ON_THEIR_WAY entry and falls back to a direct-queue upload.

Bug B — Unconditional finish(jobs) & Allocator Reset After Worker::copy Error

  • crates/victory-gpu/src/texture/ahead.rs:259-264 (Worker::work) & 320-358 (finish):
    value += 1;
    if let Err(e) = self.copy(&jobs, value) {
        tracing::warn!(error = %e, "uploads ahead did not run");
    }
    finish(jobs);
    
    finish(jobs) unconditionally sets job.staging.uploaded.store(true, Relaxed), job.staging.dirty.store(false, Relaxed), job.copy.ahead.store(DONE, Release), and runs all job.then descriptor-flip callbacks even when self.copy failed. Moreover, if self.copy failed after ExecuteCommandLists (e.g., Signal or SetEventOnCompletion failed), the next iteration calls self.allocator.Reset() without confirmed GPU completion.

Impact

  • Bug A: Cross-queue concurrent writes and D3D12 resource-state violations on the same texture under VICTORY_UPLOAD_AHEAD=1.
  • Bug B: Any transient failure in Worker::copy permanently suppresses re-uploads for every texture in the batch and exposes uninitialized VRAM to shaders.

AI-Ready Fix Specification

  1. Fix claim() / listed() synchronization: Track an in-flight reservation state for copy.serial (e.g., insert a reservation entry in a set/map guarded by ON_THEIR_WAY before/atomically with NONE -> PENDING, and notify LANDED if staging() returns None or the job is turned down) so listed() and claim() wait on LANDED whenever copy.ahead == PENDING until the job either enters ON_THEIR_WAY or reverts to NONE/LISTED.
  2. Fix Worker::work / finish: Pass ok: bool into finish(jobs, ok). When !ok:
    • Decrement BYTES, remove entries from ON_THEIR_WAY, revert job.copy.ahead to LISTED (or NONE), leave uploaded = false and dirty = true, do not run job.then callbacks, and notify LANDED.
    • If ExecuteCommandLists ran in Worker::copy before Signal/SetEventOnCompletion failed, retire/recreate the command allocator instead of resetting it immediately.
<!-- greyfinch-sync:Androtim/MiniVI:issue:6741193776 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/issues/317). Originally posted by [RUIZ-Enrique-25002136](https://github.com/RUIZ-Enrique-25002136) on 2026-12-02T09:14:12Z. --- ## Summary Two concurrency/error-handling bugs exist in the asynchronous copy-queue texture uploader (`crates/victory-gpu/src/texture/ahead.rs`): 1. A TOCTOU race window between setting `copy.ahead = PENDING` and inserting the job into `ON_THEIR_WAY` allows concurrent copy-queue and direct-queue uploads/state transitions to the same `ID3D12Resource`. 2. When `Worker::copy(&jobs, value)` returns `Err`, `Worker::work` still calls `finish(jobs)` unconditionally, marking failed textures uploaded and flipping shader-visible descriptors to uninitialized VRAM (and resetting a potentially still-in-flight command allocator). ## Evidence (`file:line` & Code Proof) ### Bug A — TOCTOU Window Between `claim()` and `listed()` - **`crates/victory-gpu/src/texture/ahead.rs:107-110, 125-147` (`claim`):** ```rust if copy.ahead.compare_exchange(NONE, PENDING, Relaxed, Relaxed).is_err() { return turned_down(); } super::PICKING.set(Some(there)); let staging = super::staging(resource, &copy, first); ... let mut way = ON_THEIR_WAY.lock().unwrap_or_else(|e| e.into_inner()); way.push((job.copy.serial, job.clone())); ``` - **`crates/victory-gpu/src/texture/ahead.rs:177-187` (`listed`):** ```rust if copy.ahead.compare_exchange(NONE, LISTED, Relaxed, Relaxed) != Err(PENDING) { return; } let mut way = ON_THEIR_WAY.lock().unwrap_or_else(|e| e.into_inner()); while way.iter().any(|(serial, _)| *serial == copy.serial) && ... { ... } ``` Between CAS `NONE -> PENDING` (line 125) and `way.push(...)` (line 144), `claim()` allocates staging outside `ON_THEIR_WAY`. A concurrent `listed(&copy)` sees `Err(PENDING)`, locks `ON_THEIR_WAY`, finds no entry for `copy.serial`, and returns immediately—allowing the direct-queue list to copy into `copy.texture` concurrently with the copy-queue upload. Likewise, a concurrent `claim()` at lines 107-109 sees `PENDING` with no `ON_THEIR_WAY` entry and falls back to a direct-queue upload. ### Bug B — Unconditional `finish(jobs)` & Allocator Reset After `Worker::copy` Error - **`crates/victory-gpu/src/texture/ahead.rs:259-264` (`Worker::work`) & `320-358` (`finish`):** ```rust value += 1; if let Err(e) = self.copy(&jobs, value) { tracing::warn!(error = %e, "uploads ahead did not run"); } finish(jobs); ``` `finish(jobs)` unconditionally sets `job.staging.uploaded.store(true, Relaxed)`, `job.staging.dirty.store(false, Relaxed)`, `job.copy.ahead.store(DONE, Release)`, and runs all `job.then` descriptor-flip callbacks even when `self.copy` failed. Moreover, if `self.copy` failed *after* `ExecuteCommandLists` (e.g., `Signal` or `SetEventOnCompletion` failed), the next iteration calls `self.allocator.Reset()` without confirmed GPU completion. ## Impact - **Bug A:** Cross-queue concurrent writes and D3D12 resource-state violations on the same texture under `VICTORY_UPLOAD_AHEAD=1`. - **Bug B:** Any transient failure in `Worker::copy` permanently suppresses re-uploads for every texture in the batch and exposes uninitialized VRAM to shaders. ## AI-Ready Fix Specification 1. **Fix `claim()` / `listed()` synchronization:** Track an in-flight reservation state for `copy.serial` (e.g., insert a reservation entry in a set/map guarded by `ON_THEIR_WAY` before/atomically with `NONE -> PENDING`, and notify `LANDED` if `staging()` returns `None` or the job is turned down) so `listed()` and `claim()` wait on `LANDED` whenever `copy.ahead == PENDING` until the job either enters `ON_THEIR_WAY` or reverts to `NONE`/`LISTED`. 2. **Fix `Worker::work` / `finish`:** Pass `ok: bool` into `finish(jobs, ok)`. When `!ok`: - Decrement `BYTES`, remove entries from `ON_THEIR_WAY`, revert `job.copy.ahead` to `LISTED` (or `NONE`), leave `uploaded = false` and `dirty = true`, do **not** run `job.then` callbacks, and notify `LANDED`. - If `ExecuteCommandLists` ran in `Worker::copy` before `Signal`/`SetEventOnCompletion` failed, retire/recreate the command allocator instead of resetting it immediately.
github-actions[bot] commented 2026-12-02 09:14:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from Androtim/MiniVI. Originally posted by github-actions[bot] on 2026-12-02T09:14:20Z.


Please attach both logs from the run that went wrong so we can investigate without guessing. Paste %LOCALAPPDATA% into the File Explorer address bar to find them:

  1. Play log in %LOCALAPPDATA%\victory\logs\: the newest .log from that run (the console prints its path at startup on the Log: ... line).
  2. Crash log: %LOCALAPPDATA%\victory\data\<TitleId>\crashes.log, and if present, the newest .crash.log in %LOCALAPPDATA%\victory\data\<TitleId>\temp\.

Please also include:

  • GPU & VRAM, Windows version (10 or 11)
  • MiniVI preset / launcher flags (play-minivi.ps1 preset, DLSS/FG/RT, custom VICTORY_* vars) and commit SHA

(Clean-room policy: please only attach text logs/screenshots — do not upload copyrighted game files or executables.)

<!-- greyfinch-sync:Androtim/MiniVI:comment:7340585828 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/issues/317#issuecomment-7340585828). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-02T09:14:20Z. --- Please attach both logs from the run that went wrong so we can investigate without guessing. Paste `%LOCALAPPDATA%` into the File Explorer address bar to find them: 1. **Play log** in `%LOCALAPPDATA%\victory\logs\`: the newest `.log` from that run (the console prints its path at startup on the `Log: ...` line). 2. **Crash log**: `%LOCALAPPDATA%\victory\data\<TitleId>\crashes.log`, and if present, the newest `.crash.log` in `%LOCALAPPDATA%\victory\data\<TitleId>\temp\`. Please also include: - **GPU & VRAM**, **Windows version** (10 or 11) - **MiniVI preset / launcher flags** (`play-minivi.ps1` preset, DLSS/FG/RT, custom `VICTORY_*` vars) and **commit SHA** *(Clean-room policy: please only attach text logs/screenshots — do not upload copyrighted game files or executables.)*
Sign in to join this conversation.
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
Androtim/MiniVI#317
No description provided.