Skip to content

fix(harness): give each gated subscription its own turn lease - #3593

Closed
King52HerTz wants to merge 1 commit into
agentscope-ai:mainfrom
King52HerTz:fix/gateway-turn-lease-per-subscription
Closed

King52HerTz wants to merge 1 commit into
agentscope-ai:mainfrom
King52HerTz:fix/gateway-turn-lease-per-subscription

Conversation

@King52HerTz

Copy link
Copy Markdown
Contributor

Problem

Closes #3549.

withGatedTurn() and withGatedStream() allocated their AtomicReference<TurnLease> outside defer(), so every subscription to the returned publisher shared one reference. Two consequences, both worse than "closes the wrong lease":

  1. A second subscriber overwrites the first one's lease. When the first turn then completes, its doFinally reads 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. With LocalSessionTurnGate that means the session stays busy indefinitely.
  2. Cancelling a queued subscriber closes a lease belonging to a running turn, so a subsequent turn can enter the same session concurrently - which is the exact thing the gate exists to prevent.

run(...)/runStream(...) return these publishers to callers that legitimately subscribe more than once (a UI re-subscribing after a disconnect, or a reused Mono), so this is reachable without any exotic setup.

Change

Acquire the lease inside defer() and release it in that same subscription's doFinally, 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: same TurnBusyException -> empty, same interrupt -> error, same subscribeOn(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 LocalSessionTurnGate itself - reference-counted Gate entries, 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 LocalSessionTurnGate or 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

mvn -B -T1 test -pl :agentscope-harness -am -Dtest=HarnessGatewayTurnLeaseTest   # 2 tests, BUILD SUCCESS
mvn -B -T1 test -pl :agentscope-harness -am   # BUILD SUCCESS: 8850 tests, 0 failures, 0 errors, 54 skipped (core + harness, all their suites)

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-harness asserts 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.

What was broken Result
withGatedTurn reverted to the shared reference (main's behaviour) turn test red - "timed out waiting for the first lease to be released", i.e. the leak, nothing else
Stream side released only on completion instead of doFinally stream test red - a cancelled subscriber leaves its lease held, nothing else

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

Copy link
Copy Markdown
Contributor Author

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 — AgentProtocolTaskStoreHitlTest.submit_pausesOnPermissionAsking_andResumeDenyCompletes errors with IllegalStateException: task is already running: hitl-1 at AgentProtocolTaskStore.java:189 (test line 116). This PR touches only HarnessGateway.java plus the new HarnessGatewayTurnLeaseTest.java; AgentProtocolTaskStore contains no reference to HarnessGateway, withGatedTurn or TurnLease, and that test drives a mocked HarnessAgent, so the gated-turn path changed here is not on its code path. The same test, same message and same two line numbers failed on #3588 (same base commit c416c378e) about nine hours before this run, and the push run for that base commit is green. #3592 — also harness-only, same base, opened in the same batch — is green on both legs.

windows-latest — JdkHttpTransportTest in agentscope-core reports 2 failures + 1 error (testStreamErrorResponseWithEmptyBody 4.10 s, testExecute500ErrorResponse 2.82 s, testStreamHandlesEmptyLines 2.01 s). The same class runs 52/52 green on the ubuntu leg of this exact commit (9.4 s), and those three methods complete in 0.01–0.21 s in other windows jobs, so the elapsed times point at runner contention rather than behaviour.

What this change was actually verified against: -pl :agentscope-harness -am, 8858 tests, 0 failures — as stated in the description, not the full 97-module reactor. On CI, the ubuntu leg runs agentscope-harness at 1247 tests / 0 failures / 0 errors and both new lease tests pass there. One thing worth saying out loud rather than leaving implied: the windows leg aborts in agentscope-core, so HarnessGatewayTurnLeaseTest never executes on windows at all.

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

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jujn jujn added the P1 紧急不重要 label Nov 3, 2026
@King52HerTz

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Cancelling a queued subscription can release another active gateway turn lease

2 participants