Skip to content

runtime: preserve overlay files when deletion markers fail - #4885

Open
illusionart453-max wants to merge 3 commits into
ItsNotRexed:mainfrom
illusionart453-max:fix/overlay-marker-failure-preservation
Open

illusionart453-max wants to merge 3 commits into
ItsNotRexed:mainfrom
illusionart453-max:fix/overlay-marker-failure-preservation

Conversation

@illusionart453-max

@illusionart453-max illusionart453-max commented Nov 23, 2026 •

Copy link
Copy Markdown

A failed GAME: delete or move could lose newer overlay contents: the old code removed the overlay source or replaced the destination before creating the marker that hides the original package file. Marker creation could then fail after those changes, exposing older packaged data despite reporting an operation failure.

Required markers are now prepared before DeleteFile*, RemoveDirectory*, or the file path of MoveFile* changes the overlay. Failed host operations roll back only the attempt-created marker, retain pre-existing markers and preserve GetLastError. Rollback failures are reported. Write-through moves flush the marker before changing either file.

Marker transactions hold the process mutex and an exclusive Windows byte-range lock on <overlay>/.gta6rt-copy/.marker-lock before reading the marker state, through commit or rollback. Another process sharing the overlay therefore cannot adopt an uncommitted marker. Using the filesystem lock also handles different path spellings. The persistent private lock file is never replaced or deleted by the protocol. If locking fails, source/destination changes are refused. The lock is held across copies/flushes, which can delay other mutations; this tradeoff and harmless empty marker-parent leftovers are documented. See LockFileEx for the Windows locking/handle-close behavior.

New RT item, ID pending: claim before implementation.

Validation on Windows 11, MSVC 19.44, Windows SDK 10.0.26100, RelWithDebInfo, head eb7b78b:

  • Built file_system_test with /W4, no compiler warnings.
  • 295 checks, 0 failures. CTest file_system passed (1/1).
  • The original blocked-marker tests exercise 12 ANSI/Unicode, package/overlay, delete/move/directory cases, including write-through replacements and retries. Six locked-source cases verify original errors and new/existing-marker rollback. Against base 64b45d0 the initial fixture had 268 checks / 16 failures.
  • Two-process regression: A holds the marker lock and prepares a marker; B starts a real production-shim delete or move in a hidden child process and must wait. A removes its uncommitted marker and releases the lock; B then succeeds with its own committed deletion marker, leaving the package source hidden. B uses an overlay path containing ... On previous head 2598b85 these two scenarios produced 4 failures (292-check initial regression): B returned while A held the transaction and the package source reappeared after rollback.
  • An obstructed lock directory refuses delete/move without changing either overlay file or the package. The staging-file check permits only the documented persistent lock file and still rejects leftover copy-up files.
  • python -I tests/test_docs_links.py: 6 passed.
  • python -I tests/test_text_write_line_endings.py: 3 passed.
  • python -I tests/test_ctest_registration.py: 4 passed.
  • git diff --check: clean.

This handles reported filesystem failures and concurrent cooperating processes; it is not power-loss recovery or a general transactional filesystem. RT-240's default overlay location remains a maintainer decision. #3651 owns logical-parent checks/same-name moves, and #3489 owns delete-on-close opens.

CPU-only synthetic files; no game files or GPU used. Host GPU RTX 5060, driver 610.88. AI-assisted (Codex).

@tupacfan04

Copy link
Copy Markdown
Contributor

APPROVE. Built file_system_test (MSVC 19.51, RelWithDebInfo, /W4) with zero warnings: 268 checks, 0 failures. Negative control on base 64b45d07 gives 268 checks / 16 failures, including the source-content loss and changed destination from the body.

The diff is correct: markers are prepared before DeleteFileW (runtime/shims/file_system.cpp:819), RemoveDirectoryW (:917), and before the move replaces either path (:969); rollback deletes only attempt-created markers and restores GetLastError (:237-242); MOVEFILE_WRITE_THROUGH flushes the marker before the move (:972). Docs and line-ending tests pass; diff clean.

Three non-blocking nits:

  • The process-wide mutex at :249-253 is held across CopyFileW/flush_file (:977, :984), serializing all GAME: deletes and moves. The resurrection race justifies it, but the cost should be stated.
  • The rollback at :240 ignores a DeleteFileW failure and can leave empty directories from create_directories (:209).
  • tests/test_file_system.cpp:856 has no concurrency test for the serialization claim, and the ANSI move omits MOVEFILE_WRITE_THROUGH.

AI-assisted review, no game files.

@Ayinaki

Ayinaki commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested b557671 on Windows 10.0.26200, MSVC 14.52 (Visual Studio 18 2026, x64, RelWithDebInfo): pass. file_system_test is CPU-only, no GPU path.

Built and ran file_system_test: 268 checks, 0 failures, including the new host-failure-preserves-both-paths case ("PASS host failure preserves the source and destination") and the deletion-marker rollback cases.

@illusionart453-max

Copy link
Copy Markdown
Author

Thanks @tupacfan04. Follow-up 2598b855 addresses the diagnostics/documentation notes: the docs now state that the global mutation lock is held across copy/flush and can delay unrelated GAME: deletes/moves, and explain harmless empty marker-parent leftovers. A failed rollback DeleteFileW now reports its error while restoring the original operation's GetLastError.

The ANSI replacement-move regression already includes MOVEFILE_WRITE_THROUGH at runtime/test/file_system_test.cpp:148; it was present in b557671. The fixture currently has no deterministic concurrency test, so I am keeping the serialization claim based on code inspection and the explicit process-wide mutex rather than presenting one as tested.

Rebuilt with MSVC 19.44 /W4: 268 checks, 0 failures. Docs links 6 passed, text-line-ending tests 3 passed, git diff --check clean. CPU-only synthetic files, AI-assisted (Codex).

@editrice

Copy link
Copy Markdown

[P1] PendingMarker needs cross-process ownership (current head 2598b855, runtime/shims/file_system.cpp:231-255). The new static mutex serializes threads only within one gta6rt process, while markers are files in the shared overlay. A can create a pending marker; B, in another process, sees it as a regular file at :232, treats it as pre-existing and successfully deletes/moves the overlay source; then A’s host operation fails and its destructor removes that marker at :238-247. B returned success, but the packaged source becomes visible again. The current 268-check fixture exercises one process, so it cannot detect this interleaving. Please give pending markers cross-process ownership/locking (or otherwise prevent adopting another process’s uncommitted marker), with a two-process regression. Synthetic paths suffice; no game files needed. AI-assisted review.

@kapitche

kapitche commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 2598b85 merged with main 3faac55 on native Windows (MSVC 14.51, RelWithDebInfo, Ninja) and Linux (docs tests only), Python 3.12.16 / 3.13.1: pass

Windows: built file_system_test and ran ctest file_system: Passed (all checks in the new marker-failure and marker-rollback scenarios). Linux: all tests/test_docs_*.py pass (runtime_imports.md changes).
Negative control (previous head b557671, same test): runtime/shims/file_system.cpp checked out from main with the PR's file_system_test.cpp, rebuilt: 268 checks, 16 failures (the marker-failure checks "preserves the overlay source" and "preserves both destination versions" fail on main), so the new test demonstrates the fix. The newest commit (2598b85) only adds a stderr report when rollback of a marker fails and doc text; I did not re-run the negative control on it, only the PR build and ctest.
Read the diff: no verified issues.

@illusionart453-max

Copy link
Copy Markdown
Author

@editrice, fixed in eb7b78b. The process mutex is now paired with a Windows byte-range lock on a persistent file inside the private copy directory, acquired before marker-state reads and held through rollback/commit. Processes opening the same overlay through different spellings share the same file lock; the file is kept in place rather than unlinked/recreated.

The new child-process regression reproduces your interleaving for delete and move. Against 2598b85, the initial 292-check fixture gives four failures: B finishes while A owns the pending marker, and A's rollback exposes the package after B succeeded. Fixed: all 295 checks pass, including aliased overlay paths and lock-acquisition failure preserving both files. CTest file_system passes; docs/registration/line-ending checks pass, diff clean. No game files, CPU only, AI-assisted (Codex).

@ricardotanaka

ricardotanaka commented Nov 23, 2026 •

Copy link
Copy Markdown

Re-tested the current head eb7b78b7 (the cross-process lock; after the check above on 2598b855) merged onto main 3faac557, native Windows 11 26200 (pt-BR), MSVC 19.44, RelWithDebInfo: pass. Since the fix is meant to give one lock to every spelling of the overlay path, I ran file_system_test.exe with %TEMP% set to three spellings and folders:

  • 8.3 short form of a folder under a user profile with a space (C:\Users\XXXXXX~1\...): 295 check(s), 0 failure(s), exit 0.
  • the same folder in long form (C:\Users\First Last\...): 295 check(s), 0 failure(s), exit 0.
  • a folder with spaces and a non-ASCII character (C:\dev\lw\tmp com espaço ç): 295 check(s), 0 failure(s), exit 0.
  • 10 rounds of 4 copies at once in that last folder: 40 of 40 runs print 295 check(s), 0 failure(s), nothing on stderr.

None of the runs left anything behind in the %TEMP% folder (item count the same before and after).

AI-assisted (Claude Code).

@amarovndres

Copy link
Copy Markdown

Tested eb7b78b7 on Windows 11 Pro (build 26200), NTFS, with file_system_test built from this head with MSVC (VS 2022 Build Tools, Windows SDK 10.0.26100): pass.

  • ctest -R '^file_system$': passed, 295 PASS / 0 FAIL check lines. main d974c88a built the same way: 159 / 0. The extra 136 are this PR's cases, among them the two-process run: process B, the test exe relaunched with --marker-child, reaches its file operation and blocks on the overlay lock under a different path spelling. Also lock failure preserves both overlay files and the package, marker failure preserves both destination versions, and retry succeeds after the marker obstruction is removed.
  • I read the diff first. The only new process the test starts is itself as process B.

AI-assisted (Claude Code). Synthetic files only, no game files.

@editrice

Copy link
Copy Markdown

@illusionart453-max I independently rechecked the cross-process fix at eb7b78b7301f3deea68d2c290af930ec8ac93d5d on Windows 11 / MSVC 19.44 using synthetic CPU files. file_system_test passed 295/295. As a direct negative control for my earlier P1, I rebuilt immediate parent 2598b855 with the head’s identical test harness but the parent production file_system.cpp: 289/295 passed. Both delete and move children completed while process A held its pending-marker transaction; A’s rollback then removed the adopted marker and exposed the packaged source. At this head, B waits, then commits its own marker after A releases the lock; overlay/../overlay also converges on the same persistent lock.

The marker-mutating A/W entry points route through the three locked W implementations. The lock is acquired before marker/view reads and held through commit or rollback; lock acquisition failure precedes source/destination mutation. This resolves my reported race. My local /Zi compiler probe hit C1902, so the build used /Z7; the test result above is from that build. This is a CPU-only check of the reported interleaving, not a claim about power-loss recovery. No game files. AI-assisted verification.

@kapitche

Copy link
Copy Markdown
Contributor

Tested eb7b78b merged with main 3faac55 on native Windows 11 (MSVC 14.51, RelWithDebInfo, Ninja; file_system_test target, no GPU) and Linux (docs tests only), Python 3.12.16: pass

This re-tests the cross-process lock head; my earlier comment (#4885 (comment)) was for 2598b85.

@memorypressure

Copy link
Copy Markdown

Verified statically on current main (3faac55, merges cleanly). The PendingMarker pattern is sound: process mutex plus the exclusive byte-range lock on the marker-lock file (persistent, never replaced), marker-before-destructive ordering in all three entry points (the ANSI variants and move_file_w delegate to the locked _w paths — verified), rollback deletes only a marker created by this operation, and GetLastError is preserved across the destructor's own calls. The new coverage (obstructed marker directory, rollback both ways, a real second process racing a different overlay spelling) targets exactly the failure mode described. The ctest file_system suite needs a Windows build and was not run here; on the Python side test_exe_checklist, test_pe_inspect and the docs guards all pass. CI reports no checks for this branch. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@illusionart453-max

Copy link
Copy Markdown
Author

@kapitche Verified the integration note: I merged the declared #4621 dependency and #4965 into a local review branch, keeping all test blocks and moving our marker/child-process checks after the API cases so they retain the intended configuration. The resulting file_system_test passes 360/360; no production conflict resolution was needed. This branch stays local, with our canonical PR unchanged.

A targeted additional probe found a marker-preflight defect in #4965's new ReplaceFileW path, also reproduced on #4965 alone (211 checks / 5 failures). Report and test-only patch are on the owning PR: #4965 (review). The original #4885 test is restored and passes 295/295. AI-assisted, CPU only, no game files.

BeegSchmooze added a commit to BeegSchmooze/Leonida that referenced this pull request Nov 23, 2026
ReplaceFile wrote the marker that hides a package replacement after the
host's ReplaceFile had consumed the overlay copy, so a marker that
couldn't be written failed the call after the replace was done, and the
package's replacement became visible again under its name (the failure
ItsNotRexed#4885 fixes for DeleteFile and MoveFile). The marker is now written
after both copy-ups and before ReplaceFile, and removed again when
ReplaceFile fails.

file_system: 212 checks, 0 failures. Three new checks: an obstructed
marker folder fails the call with nothing replaced (fails with the old
order), the retry succeeds, and a refused ReplaceFile leaves no marker.
ctest 151/151, docs tests 30/30.

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

Copy link
Copy Markdown
Contributor

Tested together with #4965, which adds MoveFileWithProgress* and ReplaceFile* to the same file. Both of those write deletion markers, so they're inside this PR's scope. Windows 11, MSVC 19.51, RelWithDebInfo.

Merge onto #4965: 2 conflicts, both mechanical. In RemoveDirectory I kept #4965's T:\ refusal together with your PendingMarker lock. In the test file both sides are kept. Resolved branch: BeegSchmooze:merge-4965-4885.

Three checks added: with the marker folder obstructed (your method), ReplaceFileW fails and nothing is replaced. The retry succeeds once the obstruction is gone. A ReplaceFileW the host refuses leaves no marker. The first one fails with the old order, which I checked with a negative control.

Results: file_system_test 348 checks, 0 failures, including all your marker-failure, rollback and cross-process checks. Full ctest on the merged tree: 155/155 passed, one disabled (gameinput, no SDL3).

AI-assisted (Claude Code). No game files used.

BeegSchmooze added a commit to BeegSchmooze/Leonida that referenced this pull request Nov 23, 2026
ReplaceFile wrote the marker that hides a package replacement after the
host's ReplaceFile had consumed the overlay copy, so a marker that
couldn't be written failed the call after the replace was done, and the
package's replacement became visible again under its name (the failure
ItsNotRexed#4885 fixes for DeleteFile and MoveFile). The marker is now written
after both copy-ups and before ReplaceFile, and removed again when
ReplaceFile fails.

file_system: 212 checks, 0 failures. Three new checks: an obstructed
marker folder fails the call with nothing replaced (fails with the old
order), the retry succeeds, and a refused ReplaceFile leaves no marker.
ctest 151/151, docs tests 30/30.

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

Copy link
Copy Markdown
Author

Integration update at main 22037f7 + this PR: resolving source/test conflicts while preserving all checks gives 582 checks, initially 16 read-only failures. The merged DeleteFile code still uses if (!removed) for the package read-only test, but this PR moves the overlay deletion later, so removed is still false even when a writable overlay supplies the visible attributes. Checking !std::filesystem::exists(overlay_path, ec) instead restores the intended visible-overlay rule. Baseline then passes 582/582. This adjustment is committed in the local directory-over-file follow-up branch, which depends on #4885, alongside 46 new checks (628/628 final). Full configured CPU suite on that branch passes 104 tests, skips 7 game-input cases, disables GameInput, zero failures. Publishing the follow-up is awaiting workflow-scope reauthorization after GitHub rejected its newer upstream history. AI-assisted (Codex), Windows/MSVC 19.44, synthetic files only.

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.

9 participants