Skip to content

file_system: copy-up through a staging name, one at a time - #3063

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
airp1ane:fs-atomic-copy-up
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
airp1ane:fs-atomic-copy-up

Conversation

@airp1ane

Copy link
Copy Markdown
Contributor

New RT item, claimed in #3047.

When the title write-opens a package file, write_target (runtime/shims/file_system.cpp) copies it into the overlay first (copy-up). It copied straight to the overlay name, with no lock:

  • Crash: a process killed mid-copy left a truncated file under the overlay name. Reads prefer the overlay, so every later run read the truncated copy instead of the intact package file.
  • Threads: two threads write-opening the same package file both saw no overlay copy and both copied. The second copy_file (copy_options::none) failed on the now-taken name, so that open failed.

Fix:

  • Copy-up writes into <overlay>\.gta6rt-copy under a private name, then MoveFileExW renames it into place. After a crash, the only leftover is in that folder.
  • A mutex taken only by copy-ups makes them one at a time. A second thread waits, then finds the first copy and skips its own. Without the mutex, copy-then-rename alone still failed about 2 runs in 100: the losing thread's rename onto the existing name briefly held that file, and the winning thread's open got ERROR_SHARING_VIOLATION.
  • Listings hide .gta6rt-copy like .gta6rt-deleted (internal_dir, used by empty_in_view and collect).
  • docs/runtime_imports.md (the "Console paths and the write overlay" rules) mentions the staging folder.

Test (file_system_test):

  • Two threads write-open the same 256 KiB package file at once, 50 rounds on fresh files. Every open must succeed and the overlay copy must match the package. This fails 3 of 3 runs on main and passes 200 of 200 with this PR.
  • No staging file is left behind.
  • A leftover from a simulated crash in .gta6rt-copy doesn't appear in a GAME:\* listing.

Tested on Windows 11, MSVC (VS 2026), RTX 4060 Ti / NVIDIA 621.24: ctest -C RelWithDebInfo 122/122. No game files.

I haven't confirmed that the game write-opens its own package files. This matters only if it does, but then a crash at the wrong moment corrupts that file for every later run.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

A write-open of a package file copied it straight to its overlay name with
no lock. A process killed mid-copy left a truncated file there that every
later read preferred over the package; two threads write-opening the same
package file both copied, and the second copy_file failed on the taken name,
so that open failed.

write_target now copies into <overlay>\.gta6rt-copy under a private name and
renames it into place, under a mutex that only copy-ups take. Listings hide
the staging folder like .gta6rt-deleted. Without the mutex, copy-then-rename
alone still failed about 2 in 100 runs: a losing rename onto the existing
name briefly holds it, and the winner's open got ERROR_SHARING_VIOLATION.

file_system_test: two threads write-open one 256 KiB package file, 50 rounds;
both opens succeed and the overlay copy matches (fails 3 of 3 runs on the
old code, passes 200 of 200 with this); no staging file is left; a crash
leftover is hidden from a GAME: listing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CharlyRien

Copy link
Copy Markdown

Tested fc1e005 (merged on current main) on Windows 11 23H2 (22631), MSVC 2022 / Ninja: pass. file_system_test passes, with real-time antivirus scanning active on fresh files. The same scanning makes loader fail on this machine (PE images ... are not readable straight away ... 464 ms), so the staging-name copy-up held up under it. The other ctest failures match main here (d3d12_vulkan_device and d3d12_typeless_rtv on the 23H2 in-box D3D12, see #2498). run_gpu_tests.py on an RTX 3090 is unchanged at 143/189 passed, 46 skipped.

AI-assisted test run.

@Bj0rnis

Bj0rnis commented Nov 22, 2026

Copy link
Copy Markdown

Tested fc1e0058 on Windows 11 (MSVC 19.44 / Ninja / Windows SDK 10.0.26100.0, built via scripts\build_runtime.ps1): pass

  • ctest -R "^(file_system)$" -V: 1/1 passed (138 check(s), 0 failure(s) in 0.84s), including the 4 new copy-up checks (two-thread concurrent write-open on a package file, intact overlay copy, no leftover staging file, and .gta6rt-copy hidden from GAME:\ listings).

(Scope: CPU/filesystem file_system unit suite only; full ctest was not re-run.)

AI-assisted (Antigravity / Gemini); no game files used.

@Kam471 Kam471 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.

Built the three TUs with /W4 /permissive- (0 warnings): file_system_test -> 138 checks, 0 failures, 6/6 runs; the same test with main's shim gives 138 checks, 3 failures, 3/3 runs (the two-thread check fails every time). A review-only failure probe passes 7/7: a blocked staging name and a failing copy both leave no target file, a retry copies correctly, and staging is empty afterward. The staging name is <overlay>\.gta6rt-copy\<pid>-<n> on the same overlay, and MoveFileExW(..., 0) means the target name only appears via rename; both error paths remove the temporary file. The copy mutex is acquired after roots()/g_mutex and nothing takes them in reverse, so there is no lock-order deadlock. No game data or absolute host paths. Approve.

Caveats: the mutex serializes every keep-contents write-open, not only actual copy-ups, so unrelated saves can queue behind a large copy; crash leftovers in the staging folder are hidden but never swept; DeleteFile is not under the same lock (pre-existing).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants