Skip to content

fix(jsonrpc): cancel in-flight streams on close - #1355

Merged
kabir merged 2 commits into
a2aproject:mainfrom
sonic8-8:issue-1354
Nov 26, 2026
Merged

kabir merged 2 commits into
a2aproject:mainfrom
sonic8-8:issue-1354

Conversation

@sonic8-8

Copy link
Copy Markdown
Contributor

Description

Problem

JSONRPCTransport.close() and JSONRPCTransport_v0_3.close() were no-ops. Client.close() and Client_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. After cancel(true), JdkA2AHttpClient can pass IOException("Request cancelled") to the error handler. So close() reports normal completion first, in the order that the auto-close path already uses:

 AbstractSSEEventListener
+  closeStream(future)
+    signalTerminal(null)   // the error handler receives null once
+    future.cancel(true)    // signalTerminal() drops the later IOException
   handleEvent(event, future)
     if shouldAutoClose(event)
-      signalTerminal(null)
-      future.cancel(true)
+      closeStream(future)

SSEEventListener_v0_3 gets the same closeStream(), which calls its existing requestClose().

These behaviors and API additions need review:

  • close() runs the error handler on the calling thread, once for each open stream.
  • If a handler throws a RuntimeException, close() continues the loop over streams. For other exception instances, close() calls addSuppressed() on the first exception. After the loop, close() rethrows the first exception.
  • If trackStream() finds the transport closed, the listener's closeStream() runs before trackStream() returns.
  • closeStream(Future<Void>) is a new public method on AbstractSSEEventListener and SSEEventListener_v0_3.

HelloWorldClient closes its Client with try-with-resources. streamingErrorHandler returns when error is null. Under mvn exec:java on main, HelloWorldClient printed Streaming error occurred: selector manager closed after the response. With the transport fix but without the null check, streamingErrorHandler throws a NullPointerException.

#1202 can call the same closeStream() so that REST also reports normal completion on close().

No README change is needed.

Tests

JSONRPCTransportCloseTest has 6 tests. JSONRPCTransportClose_v0_3_Test has 5 tests. The tests cover these cases:

  • a repeated close()
  • streams that already completed
  • close() during stream setup
  • handlers that throw

closeReleasesOpenJdkStreamAndReportsNormalCompletion runs JdkA2AHttpClient against a local server. On main, it fails because close() leaves the stream open and the handler receives nothing. It also fails with a cancel-only close() like #1202's, because the handler receives IOException("Request cancelled"). All five tests in JSONRPCTransportClose_v0_3_Test use a stub HTTP client. For an incomplete future, the stub reports IOException("Request cancelled") on cancel(true).

I ran mutation checks on closeStream(), trackStream(), and closeStreams(). Each mutation made at least one new test fail.

On JDK 25, mvn clean install succeeds. Maven skips 121 of the 4049 tests. I did not run JDK 17 or 21 locally.

Fixes #1354 🦕

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

@kabir kabir left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! A small issue which may or may not be important.

}
}

private void trackStream(CompletableFuture<Void> stream, SSEEventListener listener) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@sonic8-8
sonic8-8 requested a review from kabir November 25, 2026 05:19
@kabir

kabir commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks @sonic8-8 !

@kabir
kabir merged commit 3f321f1 into a2aproject:main Nov 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: JSONRPCTransport.close() does not end in-flight SSE streams

2 participants