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

Manually merged
github merged 1 commit from gh-pr/688/head into gh-pr/688/base 2026-11-26 09:16:33 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-24 12:07 UTC · original
aslan2142:game-rmdir-create-race → main · +113 −1 · 3 files · 1 commits · merged 2026-11-25 23:00 UTC

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

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 21625bca8; 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.

  • main at 21625bca8: 494 checks, 11 failures.
    • All 11 are Wine-only: MAXIMUM_ALLOWED opens, long paths, and the host ReadFile ERROR_NOACCESS probe.
  • This pull request's test on main's shim: 500 checks, 15 failures. The 11 above, plus the 4 new race checks.
    • Creating CreateFileW: both calls succeeded in 1,972 of 2,000 rounds.
    • CreateDirectoryW: both calls succeeded in 1,012 of 2,000 rounds.
  • This branch: 500 checks, the same 11 failures.
    • Creating CreateFileW: create first 1,964, remove first 36, both 0.
    • CreateDirectoryW: remove first 2,000, both 0.
  • This branch merged with #684: merges cleanly, 525 checks, the same 11 failures, and both races at 0.
  • Also checked: git diff --check is clean, and python -I scripts/check_no_game_data.py passes on the three changed files.
  • git merge-tree shows no conflict with the other open pull requests that touch file_system.cpp: #684, #400, #317, #287, #271 and #115.

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).

**@⁠aslan2142** on GitHub · 2026-11-24 12:07 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/688) `aslan2142:game-rmdir-create-race` → `main` · +113 −1 · 3 files · 1 commits · merged 2026-11-25 23:00 UTC Claimed in the coordination issue as: OLD-9215 https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 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 21625bca8; 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. - **main at 21625bca8:** 494 checks, 11 failures. - All 11 are Wine-only: `MAXIMUM_ALLOWED` opens, long paths, and the host `ReadFile` `ERROR_NOACCESS` probe. - **This pull request's test on main's shim:** 500 checks, 15 failures. The 11 above, plus the 4 new race checks. - Creating `CreateFileW`: both calls succeeded in 1,972 of 2,000 rounds. - `CreateDirectoryW`: both calls succeeded in 1,012 of 2,000 rounds. - **This branch:** 500 checks, the same 11 failures. - Creating `CreateFileW`: create first 1,964, remove first 36, both 0. - `CreateDirectoryW`: remove first 2,000, both 0. - **This branch merged with #684:** merges cleanly, 525 checks, the same 11 failures, and both races at 0. - Also checked: `git diff --check` is clean, and `python -I scripts/check_no_game_data.py` passes on the three changed files. - `git merge-tree` shows no conflict with the other open pull requests that touch `file_system.cpp`: #684, #400, #317, #287, #271 and #115. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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).
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:16:30 +00:00
github manually merged commit de8cb53d54 into gh-pr/688/base 2026-11-26 09:16:33 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 12:18 UTC · original · approved these changes

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 21625bca8 (0 conflicts).

LGTM!

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 12:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/688#pullrequestreview-6039060935) · **approved these changes** ### 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 21625bca8 (0 conflicts). LGTM!
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 23:00 UTC · original

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

  • Changed since review: nothing. It merges cleanly with main and with #684, and no review point is open.
  • Tested: the full Windows suite under Wine on main 5fab93967 with all nine of these runtime PRs merged together (#634, #643, #684, #657, #677, #638, #710, #688, #671), on llvmpipe and the gfx1036 iGPU (RADV): no test went from pass to fail. file_system fails only on the BCryptOpenAlgorithmProvider check, as on the baseline.
  • Checked on main d4681041e + de8cb53d5: it merges cleanly; the semantic suite passes (493 files, 6,318 tests, 0 files failed); the 15 docs and writer tests pass; check_no_game_data.py is clean on the 3 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 23:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/688#issuecomment-7177044694) Merged: `file_system` orders `RemoveDirectory` and a create inside the folder as on the host. - **Changed since review:** nothing. It merges cleanly with `main` and with #684, and no review point is open. - **Tested:** the full Windows suite under Wine on `main` `5fab93967` with all nine of these runtime PRs merged together (#634, #643, #684, #657, #677, #638, #710, #688, #671), on llvmpipe and the gfx1036 iGPU (RADV): no test went from pass to fail. `file_system` fails only on the `BCryptOpenAlgorithmProvider` check, as on the baseline. - **Checked on `main` `d4681041e` + `de8cb53d5`:** it merges cleanly; the semantic suite passes (493 files, 6,318 tests, 0 files failed); the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 3 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.