Skip to content

fix: avoid truncating lock files before acquiring lock on Windows - #9269

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
aniruddhaadak80:fix/lock-files-windows-defect-9248
Sep 14, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
aniruddhaadak80:fix/lock-files-windows-defect-9248

Conversation

@aniruddhaadak80

@aniruddhaadak80 aniruddhaadak80 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

pod_name_mutex (src/kiro_crew/pod/runtime.py) used raw fcntl.flock and degraded to a no-op on hosts without fcntl -- so on Windows the pod lifecycle mutex serialized nothing, and its lock file was opened with truncating mode "w".

Why it matters

down and up are independent entry points with no other per-name coordination, and both reclaim the isolated HOME on the down path. 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_mutex to the merged cross-platform convention (cf. #9237 / GH-9248, as in deploy/pending.py):

  • lock file opened touch(exist_ok=True) + "r+" (writable, which msvcrt.locking needs, but non-truncating)
  • exclusion via platform_compat.file_lock (fcntl.flock on POSIX, msvcrt.locking on Windows, fail-closed on both)
  • dropped the if fcntl is None: yield; return no-op branch and the now-unused guarded fcntl import; docstring updated
  • same-thread re-entry still takes the in-thread counter, never the OS lock

Tests

  • New 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.
  • Removed the three rt.fcntl is None skipifs 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; mypy on pod/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

  • Single commit with Conventional Commits title
  • Regression pins added and passing; neighboring mutex suites unaffected
  • Self-review completed; code follows project style guidelines
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

@aniruddhaadak80
aniruddhaadak80 requested review from a team and CrysisDeu September 7, 2026 17:25
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

Design-level review of 787f5fed2b1ef0543d08bf69645657ad793578e7 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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

  • The description narrates a pod/runtime.py migration ("dropped the if fcntl is None: yield; return no-op branch", "removed the three rt.fcntl is None skipifs") and the title says fix: avoid truncating lock files…on Windows, but the patch touches only test/test_pod.py — the base at this merge base already locks via open_lock_file + file_lock (src/kiro_crew/pod/runtime.py:1138) and has no fcntl remnants to remove. Merged as-is, a fix:-typed commit with zero production change enters history and the release changelog, so audits/bisects of the Lock files opened truncating before the acquire: five sites still carry the Windows defect fixed in 9237 #9248 truncation class attribute the Windows fix to a commit that doesn't contain it.
    Clears when: the PR is retitled/retyped as test: and the description rewritten to match the net diff (regression pins for behavior already shipped), or the missing runtime commit is shown to belong in this PR.

Suggestions

  • The non-truncation property is already pinned twice in base (TestOpenLockFile.test_preserves_existing_content and the fleet-wide truncating-open scan, test/test_platform_compat.py:4984,5015), and same-thread re-entry is exercised by test_a_writer_inside_the_pod_up_transaction_is_serialized_too; if the pins stay, position them as site-specific behavioral pins and drop the docstring's migration narration, which restates history the repo strips from comments.

[DESIGN-REVIEWED] 787f5fe

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ⏭️ skipped

Revision 787f5fed2b1ef0543d08bf69645657ad793578e7 ships no reviewable capability (docs, tests or generated files only), so there is nothing to inventory. Advisory — does not block merge.

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

Reviewed 787f5fed2b1ef0543d08bf69645657ad793578e7 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 787f5fe

@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed 787f5fed2b1ef0543d08bf69645657ad793578e7 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 787f5fe

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 7, 2026
jeeshofone added a commit to jeeshofone/KiroCrew that referenced this pull request Sep 8, 2026
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).
@aniruddhaadak80
aniruddhaadak80 force-pushed the fix/lock-files-windows-defect-9248 branch from ef31cca to 6ae9d0a Compare September 8, 2026 04:49
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Sep 8, 2026
jeeshofone added a commit to jeeshofone/KiroCrew that referenced this pull request Sep 8, 2026
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).
iamwhatever pushed a commit that referenced this pull request Sep 8, 2026
…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).
@aniruddhaadak80
aniruddhaadak80 force-pushed the fix/lock-files-windows-defect-9248 branch from 6ae9d0a to 1c804ec Compare September 10, 2026 14:45
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Reshaped into the real fix per the Design prescription (latest push, rebased onto current main, single commit):

  • pod_name_mutex is migrated to platform_compat.file_lock over a touch + "r+" lock file -- the merged convention from fix(work-ledger): stop the lock-file open from truncating on Windows #9237. Real exclusion on Windows via msvcrt, unchanged blocking flock on POSIX.
  • The if fcntl is None: yield; return no-op branch and the now-unused guarded fcntl import are gone; docstring updated. No reviewer-flagged comment survives.
  • Description rewritten to match the diff (no more five-site claim).
  • New TestPodNameMutexCrossPlatform pins the non-truncating open and same-thread re-entry on every host (both pass); the three rt.fcntl is None skipifs whose premise no longer holds are removed.
  • black/flake8/isort/mypy clean on both touched files; body carries ## Pattern harvest.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 10, 2026
@aniruddhaadak80
aniruddhaadak80 force-pushed the fix/lock-files-windows-defect-9248 branch from 1c804ec to 2010513 Compare September 13, 2026 07:00
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

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

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 13, 2026
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.
@aniruddhaadak80
aniruddhaadak80 force-pushed the fix/lock-files-windows-defect-9248 branch from 2010513 to 787f5fe Compare September 14, 2026 06:04
@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

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.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 14, 2026
@iamwhatever
iamwhatever enabled auto-merge (squash) September 14, 2026 07:06

@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: 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.

@iamwhatever
iamwhatever merged commit a828bf8 into kirodotdev:main Sep 14, 2026
84 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 14, 2026
amadsalmon pushed a commit to amadsalmon/KiroCrew that referenced this pull request Sep 22, 2026
…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).
amadsalmon pushed a commit to amadsalmon/KiroCrew that referenced this pull request Sep 22, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants