Skip to content

OLD-9215: restore old upstream PR 9215 - file_system: RemoveDirectory and a create inside the folder are ordered as on the host - #688

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
aslan2142:game-rmdir-create-race
Nov 25, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
aslan2142:game-rmdir-create-race

Conversation

@aslan2142

Copy link
Copy Markdown
Collaborator

Claimed in the coordination issue as: OLD-9215 #14 (comment)

Restoration of my original pull request, old upstream PR 9215 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Ported onto main at 21625bc; the write-open keeps main's long-path disk_path() for its overlay path, and the new race test sits after main's tests that recreate a removed package folder. Nothing else changed from the original.

What and why

  • The bug: a GAME: write-open or CreateDirectory* inside a folder could run its "does the folder exist" check, and then a RemoveDirectory* of that folder on another thread could run its emptiness check and write its marker. No lock covered either step.
    • Both calls then succeeded, and the removed folder came back with the new entry in it.
    • On Windows one of the two fails: the create with ERROR_PATH_NOT_FOUND, or the remove with ERROR_DIR_NOT_EMPTY.
  • The fix: a striped lock keyed by the folder's upper-cased name, made of 64 namespace-scope std::mutex (no function-local static).
    • write_target holds the lock of the folder it opens in, from the parent check until the Target is gone after the host CreateFile/CreateFile2.
    • CreateDirectory* holds the parent's lock from its check to the host call, which includes main's marker clean-up.
    • RemoveDirectory* holds the folder's lock from its view to the marker.
    • Reads take no lock.
  • The test: file_system_test races the two calls on two threads, 2,000 rounds for a creating CreateFileW (CREATE_NEW, CREATE_ALWAYS, OPEN_ALWAYS) and 2,000 for CreateDirectoryW. It checks three things: the two never both succeed, the loser fails with the host's error, and a removed folder stays removed.
  • Docs: docs/runtime_imports.md describes the ordering.
  • Not covered here: CreateDirectory of a removed package folder X racing RemoveDirectory of X itself. Main's CreateDirectory deletes X's removal marker after it makes the overlay folder, and a remove in between leaves X visible again although both calls succeeded.
    • A 2,000-round stress loop (not part of this PR) left X present in 1,993 rounds on main and 1,956 on this branch.
    • The locks here don't cover it, because the remove locks X and the create locks X's parent.
    • I'm reporting it separately in the coordination issue.

Tested

Linux, Wine 11.22 (Staging), file_system_test built with clang 23 for x86_64-w64-mingw32. MSVC /W4 /permissive- is left to CI.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted (Claude Code, Claude Opus 5.5).

…ed as on the host

Restores old upstream PR 9215 in ItsNotRexed/Leonida (closed unmerged when that repository went private), ported
onto the current main: the write-open keeps main's long-path disk_path() for its overlay path, and the new race test
sits after main's tests that recreate a removed package folder.

A GAME: write-open checked that its folder exists, mirrored it into the overlay and then opened the file, and
RemoveDirectory checked that the folder was empty and wrote its marker, neither under a lock. A remove that ran
between the open's check and its host CreateFile succeeded on an empty folder, and the open then made the folder
and the file again: both calls succeeded and the removed folder was back. CreateDirectory of a folder inside it had
the same gap. On Windows one of the two fails: the create with ERROR_PATH_NOT_FOUND or the remove with
ERROR_DIR_NOT_EMPTY.

A striped lock keyed by the folder's upper-cased name (64 namespace-scope std::mutex, no function-local static):
write_target holds the lock of the folder it opens in from the parent check until the Target is gone, after the
host CreateFile/CreateFile2; CreateDirectory holds the parent's from its check to the host call; RemoveDirectory
holds the folder's from its view to the marker. Reads take no lock.

file_system_test races the two calls on two threads, 2,000 rounds each for a creating CreateFileW (CREATE_NEW,
CREATE_ALWAYS, OPEN_ALWAYS) and for CreateDirectoryW, and checks that they never both succeed, that the loser
fails with the host's error and that a removed folder stays removed.

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

@abnalstlll-byte abnalstlll-byte 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.

Technical Review: APPROVE (OLD-9215)

  1. Concurrency Correctness:

    • Eliminates the race condition where simultaneous GAME: RemoveDirectory and CreateDirectory/CreateFile inside the folder both succeed, resulting in resurrected deleted folders.
    • Striped 64-mutex locking keyed by folder name ensures fine-grained synchronization with zero lock contention across unrelated folder trees.
    • Comprehensive synthetic multithreaded race test in runtime/test/file_system_test.cpp (2,000 stress iterations, 0 double-successes).
  2. Clean-Room & Verification:

    • Clean-room confirmed via check_no_game_data.py.
    • Clean merge against main at 21625bc (0 conflicts).

LGTM!

@aslan2142

Copy link
Copy Markdown
Collaborator Author

Merged: file_system orders RemoveDirectory and a create inside the folder as on the host.

AI-assisted (Claude Code, Claude Opus 5.5).

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.

2 participants