Skip to content

fix(cron): open the store lock file without truncating it (#9266) - #10194

Merged
chenmingwei23 merged 1 commit into
mainfrom
fix/cron-lock-non-truncating-open-9266
Sep 11, 2026
Merged

chenmingwei23 merged 1 commit into
mainfrom
fix/cron-lock-non-truncating-open-9266

Conversation

@CrysisDeu

@CrysisDeu CrysisDeu commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CronService._file_lock opened the cron store's .crons.lock sidecar with lock.open("w") and then spun on the non-blocking platform_compat.try_acquire_lock. "w" truncates at open() — before any acquire attempt. On Windows the acquire routes to msvcrt.locking, and a truncating open of a lock file whose first byte another holder has locked raises a sharing violation: the contending opener gets PermissionError at open() time, before the spin starts, instead of spinning until the holder releases. POSIX flock tolerates 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_CREAT in one syscall, never truncates, mode 0o644) — the same shape already used by session_pid.py, webhooks.py, mcp_tools/control.py and the aws_control backends, and the same class as work_ledger._open_lock (#9237) and _McpFileLock. The try_acquire_lock spin, the CronStoreBusy deadline 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_truncate seeds .crons.lock with 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, because msvcrt.locking is a mandatory lock on byte 0 and an in-context read would itself raise PermissionError on 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)\() misses try_acquire_lock sites, and its 2-line open/acquire window misses opens that sit a few lines above the acquire. Adding try_acquire_lock to 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_file helper over a hand-rolled touch() + 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

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of 43db91c9c583dc1597eca3a848ff5774c22fe2d8 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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

  • Land the "pattern harvest" ratchet extension (try_acquire_lock in the alternation, wider open/acquire window) as the promised follow-up soon — the new test pins only this site, while the ratchet pins the class.

[DESIGN-REVIEWED] 43db91c

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review — ✅ no blocking findings

Reviewed 43db91c9c583dc1597eca3a848ff5774c22fe2d8 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 43db91c

Verdict parsed from the review's SHA-scoped output markers for commit 43db91c9c583dc1597eca3a848ff5774c22fe2d8.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 43db91c9c583dc1597eca3a848ff5774c22fe2d8: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 43db91c9c583dc1597eca3a848ff5774c22fe2d8 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 43db91c

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 43db91c9c583dc1597eca3a848ff5774c22fe2d8: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of 43db91c9c583dc1597eca3a848ff5774c22fe2d8 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Nothing to check.

What this change ships

Inventory (3 items) — 3 justified

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

  1. A second process contending for the cron store now waits for release instead of crashing at open on Windows — justified
  2. The .crons.lock file's contents are no longer wiped on every acquire — justified
  3. A new regression test pins the non-truncating open (fails on base, on every platform) — justified

Audit notes: the fix reuses the existing platform_compat.open_lock_file (GH-9248) rather than adding a second spelling — 8 prior consumer sites counted (session_pid.py, webhooks.py, mcp_tools/control.py, aws_control/*). Sibling sweep: grepped try_acquire_lock( across src/kiro_crew (20 call sites) and read each site's opener — all already open non-truncating (O_RDWR | O_CREAT or a+b); this was the last truncating one, so the fix sits at cause level for its class with 0 unfixed siblings. The two deploy/ exemptions in test_no_lock_site_opens_truncating are owned by a recorded in-flight fix, not this PR.

[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
@CrysisDeu
CrysisDeu force-pushed the fix/cron-lock-non-truncating-open-9266 branch from 0caa50e to 43db91c Compare September 11, 2026 21:15
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Sep 11, 2026
@chenmingwei23
chenmingwei23 enabled auto-merge (squash) September 11, 2026 23:06

@chenmingwei23 chenmingwei23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chenmingwei23
chenmingwei23 merged commit 2316de3 into main Sep 11, 2026
71 checks passed
@chenmingwei23
chenmingwei23 deleted the fix/cron-lock-non-truncating-open-9266 branch September 11, 2026 23:07
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 11, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dwu96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

amadsalmon pushed a commit to amadsalmon/KiroCrew that referenced this pull request Sep 22, 2026
…#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cron store lock is opened truncating before the try_acquire_lock spin (#9248 sweep)

4 participants