Repository navigation
fix(acp): orphan alias-lock sweep deletes under its own handle on Windows (backport #15633 to release/0.8.0) - #15699
Merged
Conversation
…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)
This was referenced Oct 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.opendescriptor omitsFILE_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 intest_native_skill_projection.pyfail 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 746028435cwith the author kept (chenmingwei23).What changed (motivation → approach → change)
Identical to #15633:
platform_compat.open_lock_file_for_sweep. On Windows it callsCreateFileWwithFILE_SHARE_READ|WRITE|DELETE; POSIX keepsO_RDWR|O_NOFOLLOW. The sweep uses it.CrewLogleases and handles deterministically and severs the registry-to-service cycle (CrewLog.release_ownership,MemberLog.close,MemberEventLogService.close, plus close-on-replace inset_service/get_service).Conflict resolution, tests only; the five
src/files applied cleanly:test/test_link_screen_hold_pin.py: release has noopen_create_or_existing/open_lock_fileinPRIMITIVES, so onlyopen_lock_file_for_sweepis added, with its comment.test/test_platform_compat.py: onlyTestOpenLockFileForSweepis appended. TheTestOpenCreateOrExistingcontext from main is not brought in.test/test_members_dm_thread.py:TestEventlogServiceRetirementReleasesHandlesis added. Release does not have test: reset the member eventlog service singleton between tests #15534'sTestEventlogSingletonDoesNotLeakAcrossTestsclass, 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
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.pyandtest_eventlog_hooks.py. A wider run that also includedtest_crew_log*andtest_native_skill_projection_*passed 3,092.TestPeakRssIsThisProcesss::test_a_child_of_a_bloated_parent_reports_its_own_small_peak. It is unrelated.test_crew_log_edge_exhaustion.pyduring the combined run reproduces on the baseline without this change, and the file passes on its own.mypy src/kiro_crewreports 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).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