Repository navigation
fix(cron): open the store lock file without truncating it (#9266) - #10194
Conversation
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Root-cause fix routed through the established GH-9248 helper, pinned by a cross-platform regression test; nothing bolted on, nothing widened. Suggestions
[DESIGN-REVIEWED] 43db91c |
Opus 4.8 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
First Principles Review (Fable 5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (3 items) — 3 justifiedIntent: stop a contending cron-store lock acquirer from crashing at open (Windows sharing violation) because the lock file was truncated before the acquire — a FIX (closes #9266, part of the GH-9248 sweep).
Audit notes: the fix reuses the existing [FIRST-PRINCIPLES-REVIEWED] 43db91c |
CronService._file_lock opened .crons.lock with "w", which truncates at open() -- before the non-blocking try_acquire_lock spin runs. On Windows, truncating a file whose first byte another holder has locked under msvcrt.locking raises a sharing violation: the contending acquirer gets PermissionError at open() instead of spinning until release. POSIX flock tolerates it, so the defect is invisible on Linux. Route the open through platform_compat.open_lock_file (the GH-9248 helper: O_RDWR|O_CREAT in one syscall, never truncates, mode 0o644), which is what session_pid.py, webhooks.py, mcp_tools/control.py and the aws_control backends already use. The try_acquire_lock spin, the CronStoreBusy deadline and the release path are unchanged. Same class as work_ledger._open_lock (PR #9237) and _McpFileLock. Regression test seeds .crons.lock with bytes, drives a real acquire and release through _file_lock, and asserts the seeded bytes survive -- this fails on every platform if a truncating open comes back. Part of the #9248 sweep (umbrella stays open). Closes #9266
0caa50e to
43db91c
Compare
chenmingwei23
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (2 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: fix -- open the cron store lock file without truncating it, clear root cause with a regression test.
iamwhatever
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (2 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: the cron store lock file was opened with a truncating mode (lock.open("w")), emptying it before the try_acquire_lock spin ever ran and crashing a contending Windows opener with PermissionError at open() time; swapped for a non-truncating create-or-open (platform_compat.open_lock_file) plus a regression test that seeds the file and asserts the bytes survive a real acquire/release cycle, so the defect fails on every platform if it returns.
dwu96
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (2 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: fix with a clear root cause -- the cron store lock was opened with a truncating mode before the acquire spin, so a contending opener failed at open() on Windows; the fix swaps in the non-truncating open_lock_file helper already used by the work-ledger lock, plus one red-first regression test.
…#9266) (kirodotdev#10194) CronService._file_lock opened .crons.lock with "w", which truncates at open() -- before the non-blocking try_acquire_lock spin runs. On Windows, truncating a file whose first byte another holder has locked under msvcrt.locking raises a sharing violation: the contending acquirer gets PermissionError at open() instead of spinning until release. POSIX flock tolerates it, so the defect is invisible on Linux. Route the open through platform_compat.open_lock_file (the kirodotdevGH-9248 helper: O_RDWR|O_CREAT in one syscall, never truncates, mode 0o644), which is what session_pid.py, webhooks.py, mcp_tools/control.py and the aws_control backends already use. The try_acquire_lock spin, the CronStoreBusy deadline and the release path are unchanged. Same class as work_ledger._open_lock (PR kirodotdev#9237) and _McpFileLock. Regression test seeds .crons.lock with bytes, drives a real acquire and release through _file_lock, and asserts the seeded bytes survive -- this fails on every platform if a truncating open comes back. Part of the kirodotdev#9248 sweep (umbrella stays open). Closes kirodotdev#9266
Summary
CronService._file_lockopened the cron store's.crons.locksidecar withlock.open("w")and then spun on the non-blockingplatform_compat.try_acquire_lock."w"truncates atopen()— before any acquire attempt. On Windows the acquire routes tomsvcrt.locking, and a truncating open of a lock file whose first byte another holder has locked raises a sharing violation: the contending opener getsPermissionErroratopen()time, before the spin starts, instead of spinning until the holder releases. POSIXflocktolerates it, so the defect is invisible on Linux.Fix
Route the open through the existing GH-9248 helper
platform_compat.open_lock_file(O_RDWR | O_CREATin one syscall, never truncates, mode0o644) — the same shape already used bysession_pid.py,webhooks.py,mcp_tools/control.pyand theaws_controlbackends, and the same class aswork_ledger._open_lock(#9237) and_McpFileLock. Thetry_acquire_lockspin, theCronStoreBusydeadline and the release path are unchanged. The shared-helper consolidation across remaining inline sites stays out of scope (#9267); no other lock site is touched.Test
test/test_cron_locking_regression.py::TestFileLockNonBlocking::test_file_lock_open_does_not_truncateseeds.crons.lockwith bytes, drives a real acquire/release through_file_lock's spin path, and asserts the seeded bytes survive after release. This fails on every platform (not just Windows) if a truncating open comes back. The survival check runs only after release, becausemsvcrt.lockingis a mandatory lock on byte 0 and an in-context read would itself raisePermissionErroron Windows.Pattern harvest
Rule candidate: extend the fleet-wide ratchet
test_no_lock_site_opens_truncating— its acquire regex (\b(file_lock|flock_exclusive|acquire_lock)\() missestry_acquire_locksites, and its 2-line open/acquire window misses opens that sit a few lines above the acquire. Addingtry_acquire_lockto the alternation and widening the window to ~5 lines would have caught this site (and any future revert) mechanically.Review
Two pre-push model-pinned review lanes: GPT lane PASS (0 findings), Opus lane PASS (0 blocking; its Medium finding — prefer the existing
open_lock_filehelper over a hand-rolledtouch()+open("r+")— was adopted). The server-side GPT 5.6 lane's round-1 blocking finding (the in-context seed read would hit the mandatorily locked byte 0 on Windows) is fixed in this revision by asserting only after release.Part of the #9248 sweep (umbrella stays open).
Closes #9266