Skip to content

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

Open
ashrafsdrop wants to merge 1 commit into
Marvisak:mainfrom
ashrafsdrop:dstorage/open-file-share-error
Open

ashrafsdrop wants to merge 1 commit into
Marvisak:mainfrom
ashrafsdrop:dstorage/open-file-share-error

Conversation

@ashrafsdrop

Copy link
Copy Markdown

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 \

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff.

DStorage: preserve GetLastError across heap allocation: error-code preservation fix — classic Win32 bug class, good catch.

@AgneyaK

AgneyaK commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@ashrafsdrop
ashrafsdrop force-pushed the dstorage/open-file-share-error branch from ce91efa to 9325572 Compare November 25, 2026 11:02
@ashrafsdrop

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants