[pr-fix][startup][shaders] Keep the primary library when a pending file is empty #275

Open
andrewpozdnakov7-jpg wants to merge 2 commits from andrewpozdnakov7-jpg/Darner:fix/preserve-primary-with-empty-pending-library into main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

When a pending pipeline file is zero bytes, a later startup can rename it over the populated primary. The old primary then disappears. Check the pending length while the writer lease is held and fall back to reading the primary for a zero-byte snapshot. A nonempty pending file still follows the normal promotion path.

Report: the second-start regression on main. Prepared by andrewpozdnakov7-jpg (GitHub profile).

How it was tested

  • Tested revision: 17069e3 on f3840d7.
  • Commands and results: A Windows fixture compiled the final buffered Blob::read, writer and read_scoped logic. A seven-byte primary plus an empty pending file returned zero bytes on main with a competing writer and after release; after release main had also replaced the primary on disk. The patch returned the seven-byte primary in both cases, preserved both files, and still promoted a nonempty pending file. Added an in-tree regression. The initial form passed cargo check --locked -p darner-gpu --lib --tests. Final fixture, xtask policy, and git diff --check passed.
  • Changes a default: no.
  • Review follow-up at 1bc75a3: hold the writer lease through empty-file cleanup and the primary read; use the primary for an empty legacy pending file. Windows fixtures passed with and without the lease, with a nonempty pending file, and in a combined read with #232. The focused Cargo check and asset policy passed. Independent checks on 17069e3 are in issuecomment-865701.
  • Independent Windows verification on 1bc75a3: both workspace Clippy gates, asset policy and all 32 targeted pipeline-library tests passed. Review result
  • Gates run on that revision: none of the three workspace-wide gates.
  • Coverage gaps: Windows file-level fixture with synthetic bytes and buffered reads. No gameplay or nonempty corrupt file validation.

Checklist

  • I ran this change in the game.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #232 covers a nonempty pending file when replacement is blocked. git merge-tree confirmed these changes can merge independently. #270 covers cache build identity.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: <!-- optional --> ## What it changes When a pending pipeline file is zero bytes, a later startup can rename it over the populated primary. The old primary then disappears. Check the pending length while the writer lease is held and fall back to reading the primary for a zero-byte snapshot. A nonempty pending file still follows the normal promotion path. Report: [the second-start regression on main](https://git.gtahub.st/TrustNo1/Darner/pulls/232#issuecomment-862384). Prepared by [andrewpozdnakov7-jpg](https://git.gtahub.st/andrewpozdnakov7-jpg) ([GitHub profile](https://github.com/andrewpozdnakov7-jpg)). ## How it was tested - Tested revision: 17069e3 on f3840d7. - Commands and results: A Windows fixture compiled the final buffered Blob::read, writer and read_scoped logic. A seven-byte primary plus an empty pending file returned zero bytes on main with a competing writer and after release; after release main had also replaced the primary on disk. The patch returned the seven-byte primary in both cases, preserved both files, and still promoted a nonempty pending file. Added an in-tree regression. The initial form passed cargo check --locked -p darner-gpu --lib --tests. Final fixture, xtask policy, and git diff --check passed. - Changes a default: no. - Review follow-up at 1bc75a3: hold the writer lease through empty-file cleanup and the primary read; use the primary for an empty legacy pending file. Windows fixtures passed with and without the lease, with a nonempty pending file, and in a combined read with #232. The focused Cargo check and asset policy passed. Independent checks on 17069e3 are in issuecomment-865701. - Independent Windows verification on 1bc75a3: both workspace Clippy gates, asset policy and all 32 targeted pipeline-library tests passed. [Review result](https://git.gtahub.st/TrustNo1/Darner/pulls/275#issuecomment-867577) - Gates run on that revision: none of the three workspace-wide gates. - Coverage gaps: Windows file-level fixture with synthetic bytes and buffered reads. No gameplay or nonempty corrupt file validation. ## Checklist - [ ] I ran this change in the game. - [x] This PR contains one change based on main. - [x] The diff follows docs/asset-policy.md. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #232 covers a nonempty pending file when replacement is blocked. git merge-tree confirmed these changes can merge independently. #270 covers cache build identity. - Replaces or overlaps: none found
An empty pending snapshot does not supersede a populated primary. Keep reading the primary while another writer holds the cache lock and skip replacing it after the lock becomes available. Reported and isolated at #232 (comment).

https://git.gtahub.st/andrewpozdnakov7-jpg
https://github.com/andrewpozdnakov7-jpg
First-time contributor

Independent Windows build, gate, and code-read review of 17069e3eb (based on main f3840d73d). Not run in-game.

Environment

  • Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (x86_64-pc-windows-msvc), MSVC 19.44.35230.

Measured results

  • Gate 1 (cargo clippy --workspace --all-targets --locked -- -D warnings): PASS
  • Gate 2 (cargo clippy --workspace --all-targets --all-features --locked -- -D warnings): PASS
  • cargo xtask policy: PASS (asset policy: 555 files ok)
  • cargo test --locked -p darner-gpu --lib pipeline::library: PASS (31 passed; 0 failed in 1.06 s, including new an_empty_pending_library_does_not_replace_the_primary)

Findings (blocking)

  1. Releases _writer lock then re-reads next unlocked (crates/darner-gpu/src/pipeline/library.rs:742-749):
    let Some(_writer) = writer(dir, std::time::Duration::ZERO)
        .filter(|_| !std::fs::metadata(&next).is_ok_and(|meta| meta.is_file() && meta.len() == 0))
    else {
        return match Blob::read(&next)? {
            Some(Blob::None) | None => Blob::read(&path),
            Some(blob) => Ok(Some(blob)),
        };
    };
    
    When writer() succeeds and next is 0 bytes, .filter(...) turns Some(_writer) into None, dropping the writer lease, and then enters the else branch and re-reads next unlocked via Blob::read(&next)?. If a competing process acquires LOCK_FILE in that window and locks/replaces next, an I/O error from Blob::read(&next)? bubbles up to load_cache (library.rs:763-767), which treats it as an unreadable adapter cache and returns cache = None (disabling pipeline library writes for the whole session).
  2. Leaves the stale 0-byte host-pipelines.next file on disk (library.rs:742-751, test line 39):
    • Even after acquiring the exclusive _writer lease (guaranteeing no active writer exists), the 0-byte NEXT_FILE is left behind rather than removed.
    • Keeping a 0-byte NEXT_FILE on disk causes replace_library (library.rs:1855, if !next.try_exists()? && std::fs::rename(&tmp, &path).is_ok()) to skip direct replacement of LIBRARY_FILE on the next save, causes made_by_another_build (library.rs:699) to see a phantom library even when LIBRARY_FILE is absent, and repeats the lock + metadata + read dance on every launch.
    • Suggested fix: Keep _writer bound instead of .filter(...); while holding _writer, check metadata(&next) (handling NotFound), and if meta.is_file() && meta.len() == 0, remove &next (let _ = std::fs::remove_file(&next);) and fall straight through to Blob::read(&path) without re-reading &next.

Minor non-blocking note

  • load_cache's legacy fallback (library.rs:776-780) still matches Some(_) on Blob::read(&root.join(NEXT_FILE)), so a 0-byte legacy NEXT_FILE (Some(Blob::None)) will still shadow legacy LIBRARY_FILE; matching Some(Blob::None) | None there fixes the legacy read consistently.

Verdict: Changes requested (keep _writer held while checking/removing the 0-byte NEXT_FILE instead of dropping the lease via .filter(...) and re-reading &next unlocked).

AI-assisted: yes (Gemini 4 Neon).

Independent Windows build, gate, and code-read review of `17069e3eb` (based on `main` `f3840d73d`). Not run in-game. ### Environment - Windows 11 Pro 26H2 (build 26300.10075), Intel Core i9-14900K, NVIDIA RTX A2000 12GB (driver 621.47), Rust 1.98.1 (`x86_64-pc-windows-msvc`), MSVC 19.44.35230. ### Measured results - Gate 1 (`cargo clippy --workspace --all-targets --locked -- -D warnings`): **PASS** - Gate 2 (`cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`): **PASS** - `cargo xtask policy`: **PASS** (`asset policy: 555 files ok`) - `cargo test --locked -p darner-gpu --lib pipeline::library`: **PASS** (`31 passed; 0 failed` in 1.06 s, including new `an_empty_pending_library_does_not_replace_the_primary`) ### Findings (blocking) 1. **Releases `_writer` lock then re-reads `next` unlocked (`crates/darner-gpu/src/pipeline/library.rs:742-749`):** ```rust let Some(_writer) = writer(dir, std::time::Duration::ZERO) .filter(|_| !std::fs::metadata(&next).is_ok_and(|meta| meta.is_file() && meta.len() == 0)) else { return match Blob::read(&next)? { Some(Blob::None) | None => Blob::read(&path), Some(blob) => Ok(Some(blob)), }; }; ``` When `writer()` succeeds and `next` is 0 bytes, `.filter(...)` turns `Some(_writer)` into `None`, **dropping the writer lease**, and then enters the `else` branch and re-reads `next` unlocked via `Blob::read(&next)?`. If a competing process acquires `LOCK_FILE` in that window and locks/replaces `next`, an I/O error from `Blob::read(&next)?` bubbles up to `load_cache` (`library.rs:763-767`), which treats it as an unreadable adapter cache and returns `cache = None` (disabling pipeline library writes for the whole session). 2. **Leaves the stale 0-byte `host-pipelines.next` file on disk (`library.rs:742-751`, test line 39):** - Even after acquiring the exclusive `_writer` lease (guaranteeing no active writer exists), the 0-byte `NEXT_FILE` is left behind rather than removed. - Keeping a 0-byte `NEXT_FILE` on disk causes `replace_library` (`library.rs:1855`, `if !next.try_exists()? && std::fs::rename(&tmp, &path).is_ok()`) to skip direct replacement of `LIBRARY_FILE` on the next save, causes `made_by_another_build` (`library.rs:699`) to see a phantom library even when `LIBRARY_FILE` is absent, and repeats the lock + metadata + read dance on every launch. - **Suggested fix:** Keep `_writer` bound instead of `.filter(...)`; while holding `_writer`, check `metadata(&next)` (handling `NotFound`), and if `meta.is_file() && meta.len() == 0`, remove `&next` (`let _ = std::fs::remove_file(&next);`) and fall straight through to `Blob::read(&path)` without re-reading `&next`. ### Minor non-blocking note - `load_cache`'s legacy fallback (`library.rs:776-780`) still matches `Some(_)` on `Blob::read(&root.join(NEXT_FILE))`, so a 0-byte legacy `NEXT_FILE` (`Some(Blob::None)`) will still shadow legacy `LIBRARY_FILE`; matching `Some(Blob::None) | None` there fixes the legacy read consistently. **Verdict:** Changes requested (keep `_writer` held while checking/removing the 0-byte `NEXT_FILE` instead of dropping the lease via `.filter(...)` and re-reading `&next` unlocked). AI-assisted: yes (Gemini 4 Neon).
Author
First-time contributor

Thanks, @lewis499. 1bc75a3 keeps the writer lease while it removes the zero-byte scoped pending file and reads the primary. The no-lease path still leaves the pending file in place. The legacy read now uses the populated primary when its pending file is empty as well.

The Windows fixture checked all three paths and confirmed that a nonempty pending file still promotes. I also checked the combined source with #232 using a busy primary and a nonempty pending file. The focused Cargo type check and asset policy passed. Your independent checks on 17069e3 are recorded in the PR.

Thanks, @lewis499. `1bc75a3` keeps the writer lease while it removes the zero-byte scoped pending file and reads the primary. The no-lease path still leaves the pending file in place. The legacy read now uses the populated primary when its pending file is empty as well. The Windows fixture checked all three paths and confirmed that a nonempty pending file still promotes. I also checked the combined source with #232 using a busy primary and a nonempty pending file. The focused Cargo type check and asset policy passed. Your independent checks on `17069e3` are recorded in the PR.
Keep the writer lease through inspection of the empty pending file and the read of the primary. Remove the empty scoped file once the writer owns the lease. Treat an empty legacy pending file as absent when choosing which library to read.

Review: #275 (comment)
https://git.gtahub.st/andrewpozdnakov7-jpg
https://github.com/andrewpozdnakov7-jpg
First-time contributor

PASS

Follow-up verification of 1bc75a3d7e40cb2d78fcd3dcab49411ae07a2674 (base f3840d73d3504c30218debfbc8795b9225447317).

Environment

Windows 11 Pro 26H2 build 26300.10075; Intel Core i9-14900K; NVIDIA RTX A2000 12 GB (driver 621.47); Rust 1.98.1 x86_64-pc-windows-msvc; MSVC 19.44.35230.

Measured results

  • cargo clippy --workspace --all-targets --locked -- -D warnings: PASS.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: PASS.
  • cargo xtask policy: PASS (555 files ok).
  • cargo test --locked -p darner-gpu --lib pipeline::library: 32 passed, 0 failed, 0 ignored.

Code-read audit

  • read_scoped retains the writer lease while checking and removing an empty NEXT_FILE, then reads the primary; when another writer owns the lease it uses read_pending_or_primary without promoting or deleting that writer's file (crates/darner-gpu/src/pipeline/library.rs:738-765).
  • Empty pending snapshots fall back to the primary, and both the locked scoped case and the legacy fallback are covered (:2349-2377). The metadata/exists/rename error path propagates to load_cache, which warns and falls back to the primary rather than silently promoting an unreadable pending file (:752-765, 768-781).

Not run in-game.

AI-assisted: yes (GitHub Copilot, Auto mode).

**PASS** Follow-up verification of `1bc75a3d7e40cb2d78fcd3dcab49411ae07a2674` (base `f3840d73d3504c30218debfbc8795b9225447317`). ### Environment Windows 11 Pro 26H2 build 26300.10075; Intel Core i9-14900K; NVIDIA RTX A2000 12 GB (driver 621.47); Rust 1.98.1 x86_64-pc-windows-msvc; MSVC 19.44.35230. ### Measured results - `cargo clippy --workspace --all-targets --locked -- -D warnings`: PASS. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: PASS. - `cargo xtask policy`: PASS (`555 files ok`). - `cargo test --locked -p darner-gpu --lib pipeline::library`: 32 passed, 0 failed, 0 ignored. ### Code-read audit - `read_scoped` retains the writer lease while checking and removing an empty `NEXT_FILE`, then reads the primary; when another writer owns the lease it uses `read_pending_or_primary` without promoting or deleting that writer's file (`crates/darner-gpu/src/pipeline/library.rs:738-765`). - Empty pending snapshots fall back to the primary, and both the locked scoped case and the legacy fallback are covered (`:2349-2377`). The metadata/exists/rename error path propagates to `load_cache`, which warns and falls back to the primary rather than silently promoting an unreadable pending file (`:752-765`, `768-781`). Not run in-game. AI-assisted: yes (GitHub Copilot, Auto mode).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/preserve-primary-with-empty-pending-library:andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'

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 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git rebase 'main'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git rebase 'main'
git switch 'main'
git merge --no-ff 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'main'
git merge --squash 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'main'
git merge --ff-only 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git switch 'main'
git merge 'andrewpozdnakov7-jpg-fix/preserve-primary-with-empty-pending-library'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
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
TrustNo1/Darner!275
No description provided.