Repository navigation
fix(harness): give each gated subscription its own turn lease - #3593
King52HerTz wants to merge 1 commit into
Conversation
withGatedTurn() and withGatedStream() allocated their AtomicReference<TurnLease> outside defer(), so every subscription to the returned publisher shared one reference. A second subscriber overwrote the first one's lease, which means the first turn's completion closed a lease it never acquired - and the overwritten lease was never closed at all, leaving the session permanently busy. Cancelling a queued subscriber likewise ended a turn that was still running, letting a subsequent turn enter the same session concurrently. Acquire inside defer() and release in that same subscription's doFinally, so a lease is closed by exactly the subscription that took it. The two helpers are now package-private, following the shape buildRuntimeContext() already uses so this is directly unit-testable.
|
Recording the CI attribution here so a reviewer doesn't have to re-derive it: both failing legs break on tests that this branch does not touch. ubuntu-latest — windows-latest — What this change was actually verified against: Since both signatures are outside the change, I'd rather not fold a rebase or an empty commit into this branch just to make the checks look green — but if a re-run or a rebase would make the review easier, tell me which and I'll push it. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Closing this PR to avoid maintaining overlapping fixes for the same gateway turn-lease issue. The earlier PR #3550 addresses this problem; please continue the discussion there. Thanks. |
Problem
Closes #3549.
withGatedTurn()andwithGatedStream()allocated theirAtomicReference<TurnLease>outsidedefer(), so every subscription to the returned publisher shared one reference. Two consequences, both worse than "closes the wrong lease":doFinallyreads the shared reference and closes the second subscriber's lease - ending the serialisation of a turn that is still queued - while the first turn's own lease is never closed at all. WithLocalSessionTurnGatethat means the session stays busy indefinitely.run(...)/runStream(...)return these publishers to callers that legitimately subscribe more than once (a UI re-subscribing after a disconnect, or a reusedMono), so this is reachable without any exotic setup.Change
Acquire the lease inside
defer()and release it in that same subscription'sdoFinally, so a lease is closed by exactly the subscription that acquired it, once, on any terminal signal (complete, error or cancel). Nothing else about the flow changes: sameTurnBusyException-> empty, same interrupt -> error, samesubscribeOn(boundedElastic()).The two helpers become package-private so the invariant is unit-testable directly, following
buildRuntimeContext(), which is already package-private in this class for the same reason.Relationship to #3533
That PR changes
LocalSessionTurnGateitself - reference-countedGateentries,retire()eviction and an idempotent per-lease release. This PR is in a different file (HarnessGateway) and does not overlap it: making the release idempotent cannot fix a release performed by the wrong subscriber, which is what the new test drives. The reporter reaches the same conclusion in the issue. So #3533 can land before or after this with no interaction on either side.Deliberately not included
I did not change
LocalSessionTurnGateor add lease-identity checks to the interface. If a caller wants defence in depth against a lease closed by a non-owner, that belongs in the gate implementation, and #3533 is already the active change there.Testing
Scope note: I ran the full test suites of the two modules this change touches (core and harness). I did not run the whole 97-module reactor locally; the wider modules only consume this key through opaque gateway/session lookups and nothing outside
agentscope-harnessasserts a literal canonical key (checked by grepping every module's tests), so the remaining risk is what CI covers.The test drives the real public shape: one publisher, two subscriptions, each turn controlled by its own sink, against a recording
SessionTurnGate. It asserts the first turn's completion releases the first lease and not the queued one, and that cancelling the queued stream releases only its own.withGatedTurnreverted to the shared reference (main's behaviour)doFinally