DStorage: preserve GetLastError across heap allocation in open_file and permit shared write/delete #1932

Closed
github-bot wants to merge 1 commit from gh-pr/1932/head into gh-pr/1932/base
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 01:10 UTC · original
ashrafsdrop:dstorage/open-file-share-error → main · +28 −2 · 1 files · 1 commits · closed without merging

Resolves VIC-BT-037 from #1249:

Summary

  1. Preserve OS Error Code:
    In \crates/victory-hle/src/modules/dstorage.rs:open_file, previously \String::from_utf16_lossy\ was executed before \std::io::Error::last_os_error()\ when \CreateFileW\ failed. Because UTF-16 decoding performs heap allocations which can make Win32 calls resetting or mutating the thread-local error state, \GetLastError()\ could be clobbered before being mapped by \ rom_win32. Captures \last_os_error()\ immediately after the failure check.

  2. Permit Concurrent Write and Delete Access:
    Adds \FILE_SHARE_WRITE\ and \FILE_SHARE_DELETE\ to \FILE_SHARE_READ\ in \CreateFileW. Matches Windows DirectStorage behavior permitting concurrent streaming or file replacements without encountering \ERROR_SHARING_VIOLATION.

  3. Tests:
    Adds unit test \open_file_preserves_error_and_allows_shared_access\ asserting that opening a missing file accurately returns \

**@⁠ashrafsdrop** on GitHub · 2026-11-25 01:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1932) `ashrafsdrop:dstorage/open-file-share-error` → `main` · +28 −2 · 1 files · 1 commits · closed without merging Resolves VIC-BT-037 from #1249: ### Summary 1. **Preserve OS Error Code**: In \crates/victory-hle/src/modules/dstorage.rs:open_file\, previously \String::from_utf16_lossy\ was executed before \std::io::Error::last_os_error()\ when \CreateFileW\ failed. Because UTF-16 decoding performs heap allocations which can make Win32 calls resetting or mutating the thread-local error state, \GetLastError()\ could be clobbered before being mapped by \ rom_win32\. Captures \last_os_error()\ immediately after the failure check. 2. **Permit Concurrent Write and Delete Access**: Adds \FILE_SHARE_WRITE\ and \FILE_SHARE_DELETE\ to \FILE_SHARE_READ\ in \CreateFileW\. Matches Windows DirectStorage behavior permitting concurrent streaming or file replacements without encountering \ERROR_SHARING_VIOLATION\. 3. **Tests**: Adds unit test \open_file_preserves_error_and_allows_shared_access\ asserting that opening a missing file accurately returns \
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:12 +00:00
github-bot closed this pull request 2026-11-26 04:49:12 +00:00
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-25 02:27 UTC · original

Tested ce91efa merged onto main 940d32f (clean), Windows 11 26300, Rust 1.98.1 MSVC.

  • The share-mode half is covered. With only this PR's test module on main's dstorage.rs, open_file_preserves_error_and_allows_shared_access fails at the first open with -2147024864 (0x80070020, ERROR_SHARING_VIOLATION): main opens with FILE_SHARE_READ only, and the test holds a write handle. With the PR it passes.
  • The GetLastError ordering half isn't really tested. The missing.bin assertion (0x80070002) is the result main gives too: from_utf16_lossy allocating didn't change the last error in this run. The reorder is still the safer order. It just isn't shown by a failing test.
  • Stable: cargo test -p victory-hle --lib --locked 5 runs, 144 passed each. cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean.

Not tested: the title. Its DirectStorage files are package archives opened read-only, so I'd expect no change there.

AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.

**@⁠AgneyaK** on GitHub · 2026-11-25 02:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7150756993) Tested `ce91efa` merged onto `main` `940d32f` (clean), Windows 11 26300, Rust 1.98.1 MSVC. - **The share-mode half is covered.** With only this PR's test module on `main`'s `dstorage.rs`, `open_file_preserves_error_and_allows_shared_access` fails at the first open with `-2147024864` (`0x80070020`, `ERROR_SHARING_VIOLATION`): `main` opens with `FILE_SHARE_READ` only, and the test holds a write handle. With the PR it passes. - **The `GetLastError` ordering half isn't really tested.** The `missing.bin` assertion (`0x80070002`) is the result `main` gives too: `from_utf16_lossy` allocating didn't change the last error in this run. The reorder is still the safer order. It just isn't shown by a failing test. - **Stable:** `cargo test -p victory-hle --lib --locked` 5 runs, `144 passed` each. `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean. Not tested: the title. Its DirectStorage files are package archives opened read-only, so I'd expect no change there. AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:18 UTC · original · edited 2026-11-25 11:22 UTC

Automated triage for the maintainers.

Merge candidate. A small hardening change. Recommending a look, not approving.

  • crates/victory-hle/src/modules/dstorage.rs, open_file: the OS error is now read right after CreateFileW fails, before the path is decoded with String::from_utf16_lossy. The path is decoded once for both branches. The share mode is back to FILE_SHARE_READ alone, so the behaviour change from the earlier head is gone. By my reading the result is the same as main in every case seen so far, and only the order is safer.
  • The new test open_file_reports_missing_file_error checks that a missing file maps to 0x80070002. That also passes on main, so it guards the error mapping, not the reorder. Small nit: the test never releases the file object it opens, so the handle stays open until the process exits.

CI: check passing.

On your reply: both requested changes are in (error captured first with one decode, share-mode change dropped), so the verdict moves from needs-work.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7156858479) · edited 2026-11-25 11:22 UTC <!-- victory-triage v1 head=9325572cca0dd6a4c5b0316d6d72a1de0b9d4113 verdict=merge-candidate at=2026-11-25T11:22:07Z --> Automated triage for the maintainers. **Merge candidate.** A small hardening change. Recommending a look, not approving. - `crates/victory-hle/src/modules/dstorage.rs`, `open_file`: the OS error is now read right after `CreateFileW` fails, before the path is decoded with `String::from_utf16_lossy`. The path is decoded once for both branches. The share mode is back to `FILE_SHARE_READ` alone, so the behaviour change from the earlier head is gone. By my reading the result is the same as `main` in every case seen so far, and only the order is safer. - The new test `open_file_reports_missing_file_error` checks that a missing file maps to `0x80070002`. That also passes on `main`, so it guards the error mapping, not the reorder. Small nit: the test never releases the file object it opens, so the handle stays open until the process exits. CI: `check` passing. On your reply: both requested changes are in (error captured first with one decode, share-mode change dropped), so the verdict moves from needs-work. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 11:03 UTC · original

Updated in head 9325572:

  1. Immediate Error Capture & Single-Pass Decode:

    • let err = (handle as isize == -1).then(std::io::Error::last_os_error); captures the OS error immediately before path decoding, preventing heap allocations from clobbering thread-local error state.
    • Deduped the UTF-16 path decoding block to execute once for both error and success paths.
  2. Retain Read-Only Share Mode:

    • Dropped FILE_SHARE_WRITE | FILE_SHARE_DELETE, keeping FILE_SHARE_READ to prevent torn reads on title package archives.
  3. Rebased on Main:

    • Cleanly rebased onto latest origin/main (940d32f), picking up the GPU-less CI test runner fixes.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 11:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7160523948) Updated in head `9325572`: 1. **Immediate Error Capture & Single-Pass Decode**: - `let err = (handle as isize == -1).then(std::io::Error::last_os_error);` captures the OS error immediately before path decoding, preventing heap allocations from clobbering thread-local error state. - Deduped the UTF-16 path decoding block to execute once for both error and success paths. 2. **Retain Read-Only Share Mode**: - Dropped `FILE_SHARE_WRITE | FILE_SHARE_DELETE`, keeping `FILE_SHARE_READ` to prevent torn reads on title package archives. 3. **Rebased on Main**: - Cleanly rebased onto latest `origin/main` (`940d32f`), picking up the GPU-less CI test runner fixes.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:04 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because it changes nothing observable: the allocator's HeapAlloc does not touch the last error on success, and the new test passes on main too, as noted in the thread. The share-mode change in the title is no longer part of the diff.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7175903110) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because it changes nothing observable: the allocator's `HeapAlloc` does not touch the last error on success, and the new test passes on `main` too, as noted in the thread. The share-mode change in the title is no longer part of the diff. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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/Marvisak--VIctory!1932
No description provided.