Skip to content

fix(acp): orphan alias-lock sweep deletes under its own handle on Windows (backport #15633 to release/0.8.0) - #15699

Merged
bolichen97 merged 1 commit into
release/0.8.0from
backport/0.8.0-lock-sweep-windows
Oct 1, 2026
Merged

bolichen97 merged 1 commit into
release/0.8.0from
backport/0.8.0-lock-sweep-windows

Conversation

@bolichen97

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Backport #15633 to release/0.8.0, so that the orphan alias-lock sweep can delete genuine residue on Windows while it holds its own lock handle.

release/0.8.0 has the #15150 orphan-lock sweep (via #15295), so it has the Windows bug that #15651 describes. A CRT os.open descriptor omits FILE_SHARE_DELETE, so the sweep's own verification handle blocks the unlink with a sharing violation. Residue is never reclaimed, and the two sweep tests in test_native_skill_projection.py fail on Backend Tests (Windows) shard 2.

Why it matters

The fix is going into the internal 0.8.0.4 hotfix. Without it, Windows installs keep stale alias locks, and the release branch's Windows shard 2 stays red.

Not a goal

Anything beyond #15633. This is a cherry-pick -x 746028435c with the author kept (chenmingwei23).

What changed (motivation → approach → change)

Identical to #15633:

  • New platform_compat.open_lock_file_for_sweep. On Windows it calls CreateFileW with FILE_SHARE_READ|WRITE|DELETE; POSIX keeps O_RDWR|O_NOFOLLOW. The sweep uses it.
  • The squashed second commit of fix(acp): orphan alias-lock sweep deletes under its own handle on Windows #15633 (eventlog): retiring the member eventlog service now releases its cached CrewLog leases and handles deterministically and severs the registry-to-service cycle (CrewLog.release_ownership, MemberLog.close, MemberEventLogService.close, plus close-on-replace in set_service / get_service).

Conflict resolution, tests only; the five src/ files applied cleanly:

  • test/test_link_screen_hold_pin.py: release has no open_create_or_existing / open_lock_file in PRIMITIVES, so only open_lock_file_for_sweep is added, with its comment.
  • test/test_platform_compat.py: only TestOpenLockFileForSweep is appended. The TestOpenCreateOrExisting context from main is not brought in.
  • test/test_members_dm_thread.py: TestEventlogServiceRetirementReleasesHandles is added. Release does not have test: reset the member eventlog service singleton between tests #15534's TestEventlogSingletonDoesNotLeakAcrossTests class, so one docstring sentence that pointed at "the class above" was reworded. The tests themselves are unchanged.

Backwards compatibility

Compatible: the sweep's open is the same on POSIX, and the Windows open only adds delete-sharing. Service close only runs when the service is retired.

Tests

  • 1,192 passed across test_native_skill_projection.py, test_platform_compat.py, test_link_screen_hold_pin.py, test_members_dm_thread.py, test/eventlog, test_members.py, test_members_eventlog_wiring.py and test_eventlog_hooks.py. A wider run that also included test_crew_log* and test_native_skill_projection_* passed 3,092.
  • One failure also fails on unchanged release/0.8.0 in this environment: TestPeakRssIsThisProcesss::test_a_child_of_a_bloated_parent_reports_its_own_small_peak. It is unrelated.
  • An intermittent lease-teardown error in test_crew_log_edge_exhaustion.py during the combined run reproduces on the baseline without this change, and the file passes on its own.
  • mypy src/kiro_crew reports 0 issues. black 26.3.1, isort and flake8 are clean on the 8 changed files. The comment-history gate passes (base origin/release/0.8.0).
  • The Windows behaviour itself is proven by CI's Windows shard 2.

Manual verification

N/A: covered by Windows CI.

Related Issues

Backport of #15633. Fixes #15651 on release/0.8.0.

Pattern harvest

Not generalizable: this is a backport. Any pattern belongs on #15633.

Checklist

  • At most two commits, with a Conventional Commits title
  • Existing tests pass and new tests added
  • Self-review completed
  • Documentation updated (if applicable): N/A
  • No secrets, credentials, or internal references in the diff

…dows (#15633)

* fix(acp): orphan alias-lock sweep deletes under its own handle on Windows

The orphan-lock sweep removes an empty <alias>.lock only while holding its
own exclusive lock on it: the held lock is the proof no launcher owns the
inode, so the unlink happens WITHOUT first releasing (releasing before the
unlink reopens the race the proof closes). On POSIX an open fd never blocks
an unlink, so this is correct and the two sweep tests pass.

On Windows a CRT descriptor (os.open) opens with FILE_SHARE_READ |
FILE_SHARE_WRITE and OMITS FILE_SHARE_DELETE, so while the sweep's own
verification handle lives, os.unlink of that same name fails with a sharing
violation (PermissionError / WinError 32). The sweep then declines to remove
genuine residue -- its own handle is what blocks the delete -- so
test_leftover_alias_lock_files_are_swept_only_when_provably_residue (the
residue file never unlinks) and test_a_lock_file_a_launcher_still_holds_
is_never_swept (the free sibling never unlinks, removed == 0 != 1) fail
Backend Tests (Windows) shard 2. main does not run Windows shards on push,
so this landed on main (e3309cd, #15150) and only surfaced on PRs rebased
onto it.

Fix at the root with a pin on both platforms: a new
platform_compat.open_lock_file_for_sweep opens the lock file RDWR, and on
Windows uses CreateFileW with FILE_SHARE_READ_WRITE_DELETE so the unlink
lands while the handle (and its msvcrt byte-range lock) is still held --
preserving the hold-across-unlink guarantee identically on both platforms.
OPEN_EXISTING never creates (the sweep only ever removes names already
present); no OPEN_REPARSE_POINT (the caller already refuses a reparse point
at the name and re-checks identity under the lock). POSIX keeps O_RDWR |
O_NOFOLLOW, exactly the open it was doing, named.

Tests + gates:
- TestOpenLockFileForSweep in test_platform_compat.py pins the POSIX
  unlink-while-open path and the POSIX symlink refusal (both skipped off
  POSIX, where their real os.unlink-while-open / os.symlink semantics do not
  hold), and -- via a faked WinDLL that runs everywhere -- that the Windows
  CreateFileW requests delete-sharing + GENERIC_READ|WRITE and that a wrap
  failure closes the native handle. The two shard-2 tests exercise the real
  sweep on POSIX and continue to pass.
- The extraction moved the sweep's by-name open behind
  open_lock_file_for_sweep, one hop past the link-screen gate's one-hop
  resolver reach -- which would silently drop _sweep_orphan_alias_locks from
  the ratchet (the hazard uncured, merely unseen). open_lock_file_for_sweep
  is added to the gate's PRIMITIVES (exactly as open_lock_file already is for
  the same reason), and test/link_screen_sites.py is regenerated so the site
  stays declared.

* fix(eventlog): release member log handles deterministically on service retirement

test/test_members_dm_thread.py flakes on shard 8 across multiple PR boards
after #15534 merged, on both Linux and Windows. The CI traceback anchors it:

    test_returns_recorded_entries_newest_first_with_allowlist_fields
    assert record_activity(CREW, 'dashboard_chat-1', 'persistent', via='chat')
    AssertionError: assert False

The write side returns False on the FIRST call in a fresh test, after earlier
tests ran on the same xdist worker.

Root cause. get_service() memoises one MemberEventLogService per process and
rebuilds it only when the crew-log root moves (which every test does and
production never does). The service sits in a REFERENCE CYCLE: its projection
registry holds set_on_change(service._on_change), a bound method back to the
service. So set_service(None) -- which the autouse _reset_member_eventlog_singleton
floor calls at each boundary -- drops the last external reference but does not
free the graph; the cycle keeps it alive until the cyclic collector runs. Each
cached MemberLog's CrewLog releases its write lease through a weakref.finalize
that fires only then, and holds its file descriptor until then.

On POSIX that deferral is invisible (an advisory lock on a torn-down directory
harms nothing, and the next acquire against a fresh path just works), which is
why the leak could not be reproduced on Linux by running the file, targeted
interleaves, or the full real shard-8 selection. On Windows the still-open
descriptor keeps a mandatory lock that pins the (torn-down) home directory, so
the directory cannot be removed and the next writer on the inheriting worker
fails -- record_activity returns False. #15534 reset the singleton REFERENCE at
both ends, which was necessary but not sufficient: the reference-cycle keeps the
handles alive regardless.

Fix (root, deterministic). Retirement now releases the handles NOW instead of
waiting for GC:

* CrewLog.release_ownership() runs its lease finalizer explicitly (idempotent,
  atomic via weakref.finalize) and detaches it.
* MemberLog.close() releases its cached CrewLog and unloads.
* MemberEventLogService.close() closes every cached log, clears the caches, and
  severs the registry -> bound-method -> service cycle.
* set_service() closes the outgoing service when it is replaced; get_service()
  closes the previous service on a home-change rebuild. Neither happens while an
  append is in flight on the same instance (the home moves only at a test
  boundary), so nothing races a writer that still needs the lease.

Tests (whole-file guard, POSIX-provable). TestEventlogServiceRetirementReleasesHandles
pins the CAUSE rather than the Windows-only symptom: after set_service(None) the
retired service holds no cached logs and its registry cycle is severed; a
home-change rebuild retires the previous service; close() is idempotent and reads
re-open on demand. Verified to FAIL without the set_service close wiring and pass
with it.

Verification: full test_members_dm_thread.py + eventlog/test_eventlog.py +
test_members.py + test_members_eventlog_wiring.py + test_eventlog_hooks.py +
test_crew_log_member_unit_reclaim.py + test_members_roster_recency.py = 576
passed / 1 skipped (-n0). test_members_dm_thread.py stressed at -n4 x8 = 182
passed each. black (py312) / isort / flake8 / mypy clean on the changed modules.

---------

Co-authored-by: kirocrew-agent <agent@kirocrew.local>
(cherry picked from commit 7460284)
@bolichen97
bolichen97 requested a review from a team as a code owner October 1, 2026 03:17
@bolichen97
bolichen97 requested review from cruisercohen and removed request for a team October 1, 2026 03:17
@bolichen97
bolichen97 merged commit f8d6039 into release/0.8.0 Oct 1, 2026
12 of 17 checks passed
@bolichen97
bolichen97 deleted the backport/0.8.0-lock-sweep-windows branch October 1, 2026 03:18
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.

2 participants