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 from upstream-pr/688 into main 2026-11-25 23:00:46 +00:00
aslan2142 commented 2026-11-24 12:07:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-24T12:07:17Z.


Claimed in the coordination issue as: OLD-9215 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7130909581

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5471822784 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/688). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-24T12:07:17Z. --- Claimed in the coordination issue as: OLD-9215 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7130909581 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).
abnalstlll-byte commented 2026-11-24 12:18:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T12:18:42Z.

Review: APPROVED


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!

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039060935 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/688#pullrequestreview-6039060935). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T12:18:42Z. **Review:** APPROVED --- ### 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!
aslan2142 commented 2026-11-25 23:00:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T23:00:48Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7177044694 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/688#issuecomment-7177044694). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T23:00:48Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!688
No description provided.