Repository navigation
fix(agent): cancel late model stream subscriptions after the run stopped (issue #276) - #338
Merged
yuluo-yx merged 1 commit intoOct 30, 2026
Conversation
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.
Describe what this PR does / why we need it
Fixes #276: the standalone ReAct agent registered a model stream subscription and immediately requested
Long.MAX_VALUEfrom it. Cancellation only reached a subscription that was already parked in the slot, so a subscription that arrived after the run was cancelled still started the upstream model call — a stopped run kept spending tokens and holding a connection. The same window exists when the execution control is stopped before the stream is created (deadline/ownership loss):onLossfires while the slot is still empty, and the lateonSubscribeused to request anyway.Does this pull request fix one issue?
Fixes #276
Describe how you did it
onSubscribenow checkscancelled.get() || future.isCancelled()after winning the slot CAS; when the run is over it returns the slot and cancels the subscription without requesting. Thefuture.isCancelled()disjunct covers the control-stopped path, where the run-level flag is not set.cancel()and theonLosscallback now take the subscription withgetAndSet(null), so ownership transfer is atomic: the subscription is cancelled exactly once and a later slot cleanup cannot erase it.cancelledflag answers "may this run start?", the slot reference answers "who cancels?", and the cleanup keeps its existing shape.How was it tested / verified?
New offline
ReActStreamSubscriptionCancellationTestwith a deferred publisher and a counting subscription:onSubscribe→requests=0, cancels=1(the issue's repro);requests=1, cancels=0, oneFinished,onComplete;context.control().cancel()beforeonSubscribe→ no request, one cancel.Red before the fix:
Tests run: 5, Failures: 3. Green after:5/5;argi-agent-core103/103; checkstyle passes; downstreamargi-agent-frameworkregression shows no new failures.Additional notes