Repository navigation
fix: avoid truncating lock files before acquiring lock on Windows - #9269
iamwhatever merged 1 commit into
Conversation
Design Review (Fable 5, fork) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS The diff is test-only, but the title and description claim a runtime fix that is already on main — phantom description, wrong commit type. Watch
Suggestions
[DESIGN-REVIEWED] 787f5fe |
First Principles Review (Fable 5, fork) — ⏭️ skippedRevision |
GPT 5.6 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 4.8 Review (fork) — ✅ no blocking findingsReviewed |
A lock file opened with open(path, "w") is truncated before any lock is held; on Windows the acquire is msvcrt.locking on the already-truncated file, so a contending process can observe or produce an empty lock file and crash out of the critical section (kirodotdevGH-9248). Adds platform_compat.open_lock_file — a create-or-open (O_RDWR|O_CREAT, never truncating) opener yielding a raw fd for file_lock/flock_exclusive — and converts the four aws_control lock sites (backup, library, shares x2), the last unclaimed offenders after kirodotdev#9250/kirodotdev#9275/kirodotdev#9279/kirodotdev#9237. Pins the property fleet-wide: a contract test scans the source tree for truncating opens handed to a lock acquire, so a new offender fails with its file and line. deploy/pending.py and deploy/profiles.py are exempted while PR kirodotdev#9269 (which owns those sites) is in flight. Fixes kirodotdev#9248 (remaining sites).
ef31cca to
6ae9d0a
Compare
A lock file opened with open(path, "w") is truncated before any lock is held; on Windows the acquire is msvcrt.locking on the already-truncated file, so a contending process can observe or produce an empty lock file and crash out of the critical section (kirodotdevGH-9248). Adds platform_compat.open_lock_file — a create-or-open (O_RDWR|O_CREAT, never truncating) opener yielding a raw fd for file_lock/flock_exclusive — and converts the four aws_control lock sites (backup, library, shares x2), the last unclaimed offenders after kirodotdev#9250/kirodotdev#9275/kirodotdev#9279/kirodotdev#9237. Pins the property fleet-wide: a contract test scans the source tree for truncating opens handed to a lock acquire, so a new offender fails with its file and line. deploy/pending.py and deploy/profiles.py are exempted while PR kirodotdev#9269 (which owns those sites) is in flight. Fixes kirodotdev#9248 (remaining sites).
…9316) A lock file opened with open(path, "w") is truncated before any lock is held; on Windows the acquire is msvcrt.locking on the already-truncated file, so a contending process can observe or produce an empty lock file and crash out of the critical section (GH-9248). Adds platform_compat.open_lock_file — a create-or-open (O_RDWR|O_CREAT, never truncating) opener yielding a raw fd for file_lock/flock_exclusive — and converts the four aws_control lock sites (backup, library, shares x2), the last unclaimed offenders after #9250/#9275/#9279/#9237. Pins the property fleet-wide: a contract test scans the source tree for truncating opens handed to a lock acquire, so a new offender fails with its file and line. deploy/pending.py and deploy/profiles.py are exempted while PR #9269 (which owns those sites) is in flight. Fixes #9248 (remaining sites).
6ae9d0a to
1c804ec
Compare
|
Reshaped into the real fix per the Design prescription (latest push, rebased onto current main, single commit):
|
1c804ec to
2010513
Compare
|
Rebased onto current main (367 behind -> 0, 1c804ec...2010513). Honest scope note: upstream has since migrated pod_name_mutex to open_lock_file/file_lock itself, so I took upstream's runtime verbatim and this PR is now tests-only — the fcntl-skipif removals plus the cross-platform pins (non-truncating open GH-9248, same-thread re-entry) that no other PR carries. Commit message amended to match. Awaiting fresh CI (fork approval-gated). |
Upstream has since migrated pod_name_mutex to open_lock_file/file_lock, superseding this branch's runtime half (taken verbatim in the rebase). What remains is the unique value: drop the fcntl-only skipifs and pin the non-truncating lock open plus same-thread re-entry on every host.
2010513 to
787f5fe
Compare
|
Rebased onto upstream/main (was 100 commits behind; replayed the single lock-file commit cleanly, no conflicts). This also retriggers CI: the previous Backend Tests (Windows) (3) shard was cancelled at infra level, not a test failure. New head 787f5fe. |
iamwhatever
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: test (1 files). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, SAST annotations clean, security checklist all-NO, AI reviewers green. Category: test-only regression pins for pod_name_mutex in test/test_pod.py — one asserts the lock open is non-truncating (a "w" open would erase the file before the acquire, racing Windows contenders on an empty file, GH-9248), one asserts same-thread re-entry takes the in-thread counter instead of deadlocking on the OS lock; zero production files touched. CodeQL is not applicable on this fork PR (default-setup emits no check-run); SAST coverage is Semgrep only, latest run success with 0 annotations.
…irodotdev#9316) A lock file opened with open(path, "w") is truncated before any lock is held; on Windows the acquire is msvcrt.locking on the already-truncated file, so a contending process can observe or produce an empty lock file and crash out of the critical section (kirodotdevGH-9248). Adds platform_compat.open_lock_file — a create-or-open (O_RDWR|O_CREAT, never truncating) opener yielding a raw fd for file_lock/flock_exclusive — and converts the four aws_control lock sites (backup, library, shares x2), the last unclaimed offenders after kirodotdev#9250/kirodotdev#9275/kirodotdev#9279/kirodotdev#9237. Pins the property fleet-wide: a contract test scans the source tree for truncating opens handed to a lock acquire, so a new offender fails with its file and line. deploy/pending.py and deploy/profiles.py are exempted while PR kirodotdev#9269 (which owns those sites) is in flight. Fixes kirodotdev#9248 (remaining sites).
Upstream has since migrated pod_name_mutex to open_lock_file/file_lock, superseding this branch's runtime half (taken verbatim in the rebase). What remains is the unique value: drop the fcntl-only skipifs and pin the non-truncating lock open plus same-thread re-entry on every host. Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
Problem / Motivation
pod_name_mutex(src/kiro_crew/pod/runtime.py) used rawfcntl.flockand degraded to a no-op on hosts withoutfcntl-- so on Windows the pod lifecycle mutex serialized nothing, and its lock file was opened with truncating mode"w".Why it matters
downandupare independent entry points with no other per-name coordination, and both reclaim the isolated HOME on thedownpath. Without a working mutex on Windows, a stop that has just confirmed the service gone races a concurrent start whose checkout pin and service definition the stop's sweep would then delete.What changed (motivation → approach → change)
Migrated
pod_name_mutexto the merged cross-platform convention (cf. #9237 / GH-9248, as indeploy/pending.py):touch(exist_ok=True)+"r+"(writable, whichmsvcrt.lockingneeds, but non-truncating)platform_compat.file_lock(fcntl.flockon POSIX,msvcrt.lockingon Windows, fail-closed on both)if fcntl is None: yield; returnno-op branch and the now-unused guardedfcntlimport; docstring updatedTests
TestPodNameMutexCrossPlatform(test/test_pod.py, runs unskipped on every host): pins the non-truncating open (sentinel content survives acquisition) and same-thread re-entry (no deadlock). Both pass.rt.fcntl is Noneskipifs whose no-op premise no longer holds (concurrent port allocation, real-lock smoke, concurrent env writes); POSIX exclusion behavior is unchanged so those suites are unaffected.black --target-version py310,flake8,isort: clean on both touched files;mypyonpod/runtime.py: clean.Pattern harvest
Not generalizable: one-off migration of the last fcntl-only mutex to the established file_lock convention; no new pattern.
Related Issues
Related #9248 (lock-file truncation class); follows the convention merged in #9237.
Checklist
Contribution License Agreement