Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions include/crtcli/store_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <thread>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>

namespace crtcli {
Expand Down Expand Up @@ -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;
Expand Down
Loading