Repository navigation
fix(jsonrpc): cancel in-flight streams on close - #1355
Merged
Merged
Conversation
JSONRPCTransport.close() and JSONRPCTransport_v0_3.close() were
no-ops. Client.close() and Client_v0_3.close() therefore left their
active SSE streams open.
Both transports now track their active streams and close them in
close(). AbstractSSEEventListener and SSEEventListener_v0_3 each get
a new closeStream() method. closeStream() calls signalTerminal(null)
before it cancels the stream's future. The auto-close paths now call
closeStream() too.
When closeStream() cancels the future, JdkA2AHttpClient releases the
connection. For VertxA2AHttpClient or AndroidA2AHttpClient,
cancel(true) does not release the connection.
After cancel(true), JdkA2AHttpClient can pass
IOException("Request cancelled") to the error handler.
signalTerminal() accepts only the first terminal signal. Because
closeStream() calls signalTerminal(null) first, signalTerminal()
drops the later IOException.
HelloWorldClient now closes its Client with try-with-resources.
streamingErrorHandler now returns when error is null, because null
means normal completion.
This fixes a2aproject#1354
4 tasks done
kabir
requested changes
Nov 24, 2026
kabir
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR! A small issue which may or may not be important.
| } | ||
| } | ||
|
|
||
| private void trackStream(CompletableFuture<Void> stream, SSEEventListener listener) { |
Collaborator
There was a problem hiding this comment.
The 0.3 version has this at the start, do we also need this here?
if (stream == null) {
return; // the client did not return a future, so there is nothing to close
}
Contributor
Author
There was a problem hiding this comment.
Thank you for the review, @kabir. Yes, we need the null check here too. I left it out because this package is @NullMarked and the 0.3 package isn't. A custom A2AHttpClient can still return null from postAsyncSSE(), so I've added the same check in de6b7e0.
When A2AHttpClient.PostBuilder.postAsyncSSE() returns null, sendMessageStreaming() and subscribeToTask() throw a NullPointerException from trackStream(). JSONRPCTransport_v0_3 already returns early in that case. Return early in JSONRPCTransport.trackStream() as well. This fixes a2aproject#1354
Collaborator
|
Thanks @sonic8-8 ! |
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.
Description
Problem
JSONRPCTransport.close()andJSONRPCTransport_v0_3.close()were no-ops.Client.close()andClient_v0_3.close()therefore left their active SSE streams open.Fix
Both transports now track their active streams and close them in
close(), as #1202 does for REST.With
JdkA2AHttpClient, the default HTTP client,close()releases the connection. For Vert.x or Android,close()reports normal completion but does not cancel the active network stream.cancel(true)alone is not enough. Aftercancel(true),JdkA2AHttpClientcan passIOException("Request cancelled")to the error handler. Soclose()reports normal completion first, in the order that the auto-close path already uses:SSEEventListener_v0_3gets the samecloseStream(), which calls its existingrequestClose().These behaviors and API additions need review:
close()runs the error handler on the calling thread, once for each open stream.RuntimeException,close()continues the loop over streams. For other exception instances,close()callsaddSuppressed()on the first exception. After the loop,close()rethrows the first exception.trackStream()finds the transport closed, the listener'scloseStream()runs beforetrackStream()returns.closeStream(Future<Void>)is a new public method onAbstractSSEEventListenerandSSEEventListener_v0_3.HelloWorldClientcloses itsClientwith try-with-resources.streamingErrorHandlerreturns whenerrorisnull. Undermvn exec:javaonmain,HelloWorldClientprintedStreaming error occurred: selector manager closedafter the response. With the transport fix but without thenullcheck,streamingErrorHandlerthrows aNullPointerException.#1202 can call the same
closeStream()so that REST also reports normal completion onclose().No README change is needed.
Tests
JSONRPCTransportCloseTesthas 6 tests.JSONRPCTransportClose_v0_3_Testhas 5 tests. The tests cover these cases:close()close()during stream setupcloseReleasesOpenJdkStreamAndReportsNormalCompletionrunsJdkA2AHttpClientagainst a local server. Onmain, it fails becauseclose()leaves the stream open and the handler receives nothing. It also fails with a cancel-onlyclose()like #1202's, because the handler receivesIOException("Request cancelled"). All five tests inJSONRPCTransportClose_v0_3_Testuse a stub HTTP client. For an incomplete future, the stub reportsIOException("Request cancelled")oncancel(true).I ran mutation checks on
closeStream(),trackStream(), andcloseStreams(). Each mutation made at least one new test fail.On JDK 25,
mvn clean installsucceeds. Maven skips 121 of the 4049 tests. I did not run JDK 17 or 21 locally.CONTRIBUTINGGuide.fix:which represents bug fixes, and correlates to a SemVer patch.feat:represents a new feature, and correlates to a SemVer minor.feat!:, orfix!:,refactor!:, etc., which represent a breaking change (indicated by the!) and will result in a SemVer major.Fixes #1354 🦕