From 6d335c37bc0d87aced2ec71a165290f687483715 Mon Sep 17 00:00:00 2001 From: Cheolmin Jo Date: Sun, 22 Nov 2026 17:52:48 +0900 Subject: [PATCH] Close the legacy lock-directory takeover race Taking over a stale legacy lock directory used remove_all, which could delete a lock file another process had created at the same path in the meantime and let two processes hold the lock. Remove only the legacy owner file and then rmdir, which refuses a regular file; leave any other contents alone and keep waiting. Record the in-process exclusion and lock-file accumulation limits in TODO. Verified on Windows x64: store-lock-test and sdk-store-test pass. Co-Authored-By: Claude Sonnet 5.5 --- HISTORY.md | 4 ++++ TODO.md | 7 +++++++ include/crtcli/store_lock.h | 10 ++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 13a2b7f..8ad70b5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -11,6 +11,10 @@ inventing commits or test results. Older months may later be summarized under ## 2026-11-22 +### Close the legacy lock-directory takeover race + +Review of the file-lock change found that `Lock` removed a stale legacy lock directory with `remove_all`, which could delete a lock *file* another process had created at that path in the meantime and let two processes hold the lock. The takeover now removes only the legacy `owner` file and then calls `rmdir`, which refuses a regular file; if the directory holds anything else it is left alone and the caller keeps waiting until the deadline. Verified on Windows x64 only: `store-lock-test` (9 checks) and `sdk-store-test` (113 checks) pass. The race has no deterministic test; it is closed by construction. The review also recorded two open limits in [TODO](TODO.md) (no in-process exclusion, lock files never removed). CI and other hosts are pending. + ### Replace the store directory lock with an OS file lock `Lock` in `include/crtcli/store_lock.h` is now an exclusive `flock` on a small file that the operating system diff --git a/TODO.md b/TODO.md index 8e0e4a0..85a9c0d 100644 --- a/TODO.md +++ b/TODO.md @@ -102,6 +102,13 @@ and SDK promotion and pin adoption, affected native hosts and Windows C++ remain open. - [x] Store `Lock` replaced by an OS file lock with a multi-process regression; verified on Windows/x64 only ([HISTORY](HISTORY.md), 2026-11-22). Rerun the scheduled Integration job and Linux/macOS/arm64 before closing. +- [ ] No in-process exclusion in `Lock`: CRT `flock` is built on `fcntl` record locks, so two `Lock` objects in one process do not + exclude each other. Today's stores never publish the same key from two threads of one process. Before adding + threaded publication, add an in-process mutex per lock path (or a CRT per-open-file-description `flock`) and a + thread-level regression. +- [ ] Lock files are never removed, so `.locks` gains one empty file per SDK content or port build key (`recover()` skips them). + Decide whether a bounded cleanup is worth the risk of racing a live holder; removing a lock file under a holder + breaks exclusion, so any cleanup must hold the lock first. - [ ] **P1-02** Improve the documented new/install/build/run path with scoped SDK/LLVM/OS SDK diagnosis and concrete missing-input guidance. Reuse explicit pinned SDK acquisition and declared tool paths; official defaults/trust/channels require diff --git a/include/crtcli/store_lock.h b/include/crtcli/store_lock.h index ad5625d..c60a575 100644 --- a/include/crtcli/store_lock.h +++ b/include/crtcli/store_lock.h @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace crtcli { @@ -93,9 +94,14 @@ class Lock { auto age = fs::file_time_type::clock::now() - fs::last_write_time(path_, error); if (error || age < std::chrono::seconds(10)) return false; } + // Remove only what the earlier layout created: its owner file, then the directory itself. rmdir refuses a + // regular file, so a lock file that another process created at this path after the checks above survives + // (remove_all would have deleted it and let two processes hold the lock). Anything else in the directory + // is not ours to delete: leave it and keep waiting until the deadline. std::error_code ignored; - fs::remove_all(path_, ignored); - return true; + fs::remove(path_ / "owner", ignored); + if (rmdir(path_.string().c_str()) == 0) return true; + return errno == ENOENT || errno == ENOTDIR; // the path changed under us: re-examine it at once } fs::path path_; int fd_ = -1;