Repository navigation
Conversation
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Check that What this change shipsInventory (7 items) — 7 justifiedIntent: a message waiting behind a busy turn should get a fresh session, not crash, when a reset lands while it waits. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] 32d2265 Description read[DESCRIPTION-READ] f161283174162f2bfe647f98bddc3e52e507ee1f8a45f4dd9cbedee3742643bc That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Check [DESIGN-REVIEWED] 32d2265 Description read[DESCRIPTION-READ] 6a13fc910e4b4b836f770a3fc41e21c08ed64dbd36263b6ed3ffa3898565b981 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
e1496f4 to
8a1f0f8
Compare
8a1f0f8 to
a2beb34
Compare
A claimant that waited on a busy session's turn permit is handed the permit by the turn's release, then waits for the manager lock before it revalidates. It recorded itself as the permit's holder (turn_owner) only after that wait. A reset that popped the session in the window read turn_owner, which still named the turn that had already released, recorded that task as the orphaned holder, and released the permit to wake parked claimants. The woken claimant then found its session gone and released the permit again: the queued message failed with "ValueError: BoundedSemaphore released too many times", and the stale orphan record could later swallow the previous turn's genuine release of a successor's permit, leaving the key busy. _reacquire_and_validate now sets turn_owner as soon as the permit is acquired. Its refusal and not-valid paths release through the new _release_reacquired, which skips the release when the session is no longer registered and absorb_orphaned_release reports that a reset already released this task's permit. The lock order is unchanged. The session spec's revalidation contract says so.
The tests for the permit-holder record awaited the reset, the claimant and the turn through fixed 1 s and 5 s wait_for bounds, which test_wait_discipline_ratchet.py counts as 16 new short fixed waits in test_session_core_audit.py. A bound that short decides pass or fail on a slow runner. A task or future the test owns is awaited with kiro_crew.testing.wait.await_until, which fails by name after half the test's timeout. The remaining waits stay wait_for with a 30 s lost-run ceiling: the event waits inside a task the test cancels while it is parked there (await_until would leave its inner wait pending), and the coroutine awaits (a free permit's acquire, the reset).
a2beb34 to
32d2265
Compare
|
Rebased onto main (no conflict) and replaced the 16 short |
iamwhatever
left a comment
There was a problem hiding this comment.
This fixes a real double release in _reacquire_and_validate (session_allocation.py). A woken claimant set turn_owner only after its lock wait, so a reset in that window recorded the previous turn as the orphaned holder and released the claimant's permit, and the claimant then released it again and raised ValueError: BoundedSemaphore released too many times. The fix sets turn_owner right after acquire(), and routes both refusal paths through _release_reacquired. That helper skips the release only when the session is no longer registered and absorb_orphaned_release finds this task's record, which matches how _orphan_turn_holder (it records only while the permit is locked) and _wake_turn_waiters behave on main. I tried the lane's open question, cancel_owned_turn now seeing W as owner during the lock wait: its only caller is the turn-deadline cancel in dashboard/turn_dispatch.py, which passes the turn's own task, and provider_turn_is_known_terminal guards it, so at worst W's own deadline cancels its own not-yet-started turn. A session that is still registered always releases, so a stale-provider refusal behaves as before. The four interleaving tests drive the order through the waiter queues rather than timing, three of them are red on base for the right reasons, and all lanes PASS on 32d2265. — crew-pr-reviewer (deep re-check)
Problem / Motivation
Goal: a reset that lands while a woken claimant waits for the manager lock releases that claimant's turn permit exactly once, records the right holder, and does not make the queued message fail.
_reacquire_and_validate(src/kiro_crew/session_allocation.py). When the busy turn ends, its release hands the permit to the parked claimant, which then waits for the manager lock before it revalidates.turn_owner) only after that wait. Areset(fromreset_conversation, or the teardown behind a force Stop) can hold the lock in the window. It readsturn_owner, which still names the turn that already released, records that task as the orphaned holder (_orphan_turn_holder), and releases the permit to wake parked claimants (_wake_turn_waiters). The permit it releases is the claimant's.asyncio.BoundedSemaphoreraisesValueError: BoundedSemaphore released too many times, and nothing between_reacquire_and_validateandget_or_createhandles it.Why it matters
The message that waited behind the busy turn fails with a raw
ValueErrorinstead of being allocated a fresh session. When the reset is an ending fence, thatValueErroralso replaces the fence refusal thatget_or_createturns into a wait. The wrong orphan record has a second effect: it names the turn that handed the permit over, so that task's next genuine release on the key, on a successor's permit, is absorbed byabsorb_orphaned_releaseand the key stays busy.Not a goal
orphaned_holderskeeps one entry per task per key, so it cannot tell two owed absorbs for one task apart, for example a turn that reset its own session (a replay) and loses a second session during its reacquire wait. The replay path'sadopt_turn(session_allocation.py, after a successful claim) still clears that entry.turn_owner: the compaction recycle insession_compaction.py(_recycle_unmanaged, also edited by a separate open change) and the background session'spromptinsession_background.py. They release the permit directly on the session object, not through the key-basedrelease.AcpSessionProvider.shutdownunder the turn permit. Measured against a backend that hangs and ignores SIGTERM, it returns in 5 to 10 s, because each of its awaits has its own bound.What changed (motivation → approach → change)
ValueError: BoundedSemaphore released too many times, and leaves the previous turn recorded as an orphaned holder._reacquire_and_validatesetsession.turn_owneronly after it had taken the lock and validated, and its refusal paths released the permit unconditionally. The reset's_orphan_turn_holderand_wake_turn_waitersassume that a locked permit belongs toturn_owner, which was false in that window.src/kiro_crew/session_allocation.py:_reacquire_and_validatesetssession.turn_owner = asyncio.current_task()as soon assession.semaphore.acquire()returns, before waiting for the lock. A reset in the window therefore records the claimant, the task that actually holds the permit._release_reacquired(key, session)gives the permit back on the refusal path (except BaseException, which covers the ending-fence refusal and cancellation) and on the not-valid path. When the session is no longer the registered occupant andabsorb_orphaned_release(key)reports that a reset recorded this task, it skips the release, because the reset already released the permit. A session that is still registered is always released, so a stale-provider refusal releases as before.docs/system-specs/modules/session.md: the revalidation contract states that the holder is recorded at the acquire and that a reset-released permit is not released again.ValueErroraround the release. It hides the double release and leaves the wrong task recorded as the holder.absorb_orphaned_releasereports a record, without checking that the session was popped. A task that already owes an absorb for an earlier reset on the key would then skip releasing a permit it holds on a session that is still registered.Backwards compatibility
Compatible: a claim that succeeds behaves exactly as before.
turn_owneris set slightly earlier, and every reader of the field treats it as "the task holding the permit", which is true from the acquire on. A refused or stale claim with no reset in the window releases its permit as before.Security: no redaction, auth, sandbox, permission or deny code changes (none in this branch's diff); the lock order is unchanged.
Removes nothing: the change adds one helper method and moves one assignment.
Tests
Added to
test/test_session_core_audit.py, besidetest_reacquire_and_validate_releases_semaphore_on_cancellation, with shared helpers_until(a bounded yield loop, no wall clock),_live_session,_orphans,_cancel_and_drain(cancels a test's background task and awaits it, bounded, from afinally) and_hand_permit_into_reset_window. That last helper drives a turn T, a claimant W parked on its permit, and a reset queued on the manager lock, until T's release has handed the permit to W.test_a_reset_while_a_woken_claimant_waits_for_the_lock_releases_the_permit_once: W returnsFalsewithout raising. Neither T nor W is left in the orphan record, and the permit is free and acquirable.test_a_fence_refusal_after_a_reset_in_the_reacquire_window_does_not_over_release: the same window refused at the ending fence surfacesSessionEndingError, notValueError.test_a_reset_in_the_reacquire_window_leaves_the_previous_turns_next_release_genuine: T then claims the successor and releases it, and the successor's permit is free afterwards instead of staying locked.test_a_reset_while_a_claimant_is_still_parked_wakes_it_and_absorbs_the_holders_release: control for the parked-waiter path. The reset records T and wakes W, W returnsFalse, and T's later release is absorbed without unlocking a successor that another task holds.The tests order events through the asyncio primitives' waiter queues (
Semaphore._waiters,Lock._waiters) rather than through timing.Red on this branch's base, 843abe3, with only the changed test file copied in (
pytest test/test_session_core_audit.py -k "reacquire_window or woken_claimant or still_parked" -n 0), the same in each of 3 serial runs. The twoValueErrors are the double release; theAssertionErroris the wrong orphan record absorbing T's genuine release:After the fix:
test/test_session_core_audit.pypasses (12 passed, in each of 3 serial runs). The 44 test files that name_reacquire_and_validate,turn_owner,orphaned_holders,absorb_orphaned_release,_wake_turn_waiters,adopt_turnorsession_allocationgive 3929 passed, 6 skipped. mypy, flake8, isort and black are clean onsession_allocation.py; the test file is in.github/black-baseline.txtand was not reformatted.scripts/check_black_formatting.py,scripts/docs-lint.shandscripts/check_comment_history.pywithCOMMENT_HISTORY_BASE_REF=origin/mainpass.Manual verification
N/A: the window is a scheduling order between three tasks. The tests drive that exact order against a real
SessionManagerwith a mock provider.Related Issues
None found. Searched PRs and issues, open and merged, for
_reacquire_and_validate,turn_owner,BoundedSemaphore released too many times,orphaned_holders,_wake_turn_waiters,reset waiter permitandstale turn owner. Related: PR #11243 added_wake_turn_waitersand the orphan record for a claimant still parked on the permit. Open PR #11813, PR #11831 and PR #11983 also edit_reacquire_and_validate, and none of their changed lines touchesturn_owner, the orphan record or the wake.Pattern harvest
Rule candidate: review-prompt
Pattern: an ownership record is written after an await that follows the acquire it describes, so a concurrent reader in that await reads the previous owner.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)docs/system-specs/modules/session.md