DStorage: preserve GetLastError across heap allocation in open_file and permit shared write/delete #1930
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1932"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T01:10:00Z.
Resolves VIC-BT-037 from #1249:
Summary
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.
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.
Tests:
Adds unit test \open_file_preserves_error_and_allows_shared_access\ asserting that opening a missing file accurately returns \
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:10:39Z.
Review: COMMENTED
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.
Imported from Marvisak/VIctory. Originally posted by AgneyaK on 2026-11-25T02:27:54Z.
Tested
ce91efamerged ontomain940d32f(clean), Windows 11 26300, Rust 1.98.1 MSVC.main'sdstorage.rs,open_file_preserves_error_and_allows_shared_accessfails at the first open with-2147024864(0x80070020,ERROR_SHARING_VIOLATION):mainopens withFILE_SHARE_READonly, and the test holds a write handle. With the PR it passes.GetLastErrorordering half isn't really tested. Themissing.binassertion (0x80070002) is the resultmaingives too:from_utf16_lossyallocating 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.cargo test -p victory-hle --lib --locked5 runs,144 passedeach.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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T08:18:11Z.
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 afterCreateFileWfails, before the path is decoded withString::from_utf16_lossy. The path is decoded once for both branches. The share mode is back toFILE_SHARE_READalone, so the behaviour change from the earlier head is gone. By my reading the result is the same asmainin every case seen so far, and only the order is safer.open_file_reports_missing_file_errorchecks that a missing file maps to0x80070002. That also passes onmain, 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:
checkpassing.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
Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T11:03:13Z.
Updated in head
9325572: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.Retain Read-Only Share Mode:
FILE_SHARE_WRITE | FILE_SHARE_DELETE, keepingFILE_SHARE_READto prevent torn reads on title package archives.Rebased on Main:
origin/main(940d32f), picking up the GPU-less CI test runner fixes.Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:04:33Z.
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
HeapAllocdoes not touch the last error on success, and the new test passes onmaintoo, 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