Repository navigation
Description
What happened?
JSONRPCTransport.close() is a no-op in JSONRPCTransport.java. Client.close() delegates to its transport, so Client.close() cannot cancel SSE streams that Client.sendMessage() or Client.subscribeToTask() opens. The compat-0.3 implementation also has a no-op JSONRPCTransport_v0_3.close().
Version: I reproduced the bug on main at 9c7eabf5 on Linux with JDK 25.0.5.1, Maven 3.9.16, and the default JdkA2AHttpClient. 1.5.1.Final has the same no-op close().
To reproduce, add this test to JSONRPCTransportStreamingTest. A local server sends one TASK_STATE_WORKING event and keeps the stream open. After close() returns, the test waits up to 10 seconds for the server to detect the disconnect. It needs these imports: java.io.IOException, java.io.OutputStream, java.net.InetSocketAddress, java.nio.charset.StandardCharsets, java.util.concurrent.CopyOnWriteArrayList, com.google.gson.JsonParser, and com.sun.net.httpserver.HttpServer.
closeCancelsInFlightStream
@Test
public void closeCancelsInFlightStream() throws Exception {
CountDownLatch disconnected = new CountDownLatch(1);
HttpServer sseServer = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
sseServer.createContext("/", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
String id = JsonParser.parseString(body).getAsJsonObject().get("id").getAsString();
String event = "data: {\"jsonrpc\":\"2.0\",\"id\":\"" + id + "\",\"result\":{\"task\":{\"id\":\"task\","
+ "\"contextId\":\"ctx\",\"status\":{\"state\":\"TASK_STATE_WORKING\"}}}}\n\n";
exchange.getResponseHeaders().add("Content-Type", "text/event-stream");
exchange.sendResponseHeaders(200, 0);
OutputStream out = exchange.getResponseBody();
try {
out.write(event.getBytes(StandardCharsets.UTF_8));
// Keep the stream open, like an agent that is still working, until a write fails.
while (true) {
out.flush();
Thread.sleep(100);
out.write(":\n\n".getBytes(StandardCharsets.UTF_8));
}
} catch (IOException | InterruptedException e) {
disconnected.countDown();
}
});
sseServer.start();
try {
JSONRPCTransport transport = new JSONRPCTransport("http://127.0.0.1:" + sseServer.getAddress().getPort());
CountDownLatch received = new CountDownLatch(1);
List<Throwable> errors = new CopyOnWriteArrayList<>();
transport.subscribeToTask(new TaskIdParams("task"), e -> received.countDown(), errors::add, null);
assertTrue(received.await(10, TimeUnit.SECONDS), "no event received");
transport.close();
assertTrue(disconnected.await(10, TimeUnit.SECONDS), "connection still open 10 s after close()");
assertEquals(Collections.singletonList(null), errors);
} finally {
sseServer.stop(0);
}
}Expected: close() cancels the stream and releases its connection. The error handler receives null once. ClientTransport documents null as the signal for normal completion.
Actual: the test fails with connection still open 10 s after close().
Impact: an application cannot use close() to release a stream that the application no longer needs, for example after the user cancels a request.
The helloworld example shows a related symptom. I ran the helloworld client with mvn exec:java. After Response: Hello World, the client printed Streaming error occurred: selector manager closed. The message appeared because main returned before the stream ended. The helloworld client never calls client.close(). The client's error handler calls error.getMessage() even when error is null for normal completion.
Workaround: as #1194 describes, wrap the injected A2AHttpClient so that the wrapper keeps each future that postAsyncSSE() returns. JdkA2AHttpClient can report an IOException to the error handler after cancel(true). When you no longer need a stream, mark that stream as cancelled. Then call cancel(true) on its future. Ignore later terminal errors only for that marked stream.
Suggested fix: track active streams and cancel their futures in close(), as #1202 does for REST. Have the listener call signalTerminal(null) before cancel(true). signalTerminal() from #1173 drops terminal signals after the first. AbstractSSEEventListener.handleEvent() already signals normal completion before cancellation for final events. I patched JSONRPCTransport.close() to call cancel(true) without signalTerminal(null). The error handler received IOException("Request cancelled"). JdkA2AHttpClient.isCancellation() returns false because that exception has no CancellationException in its cause chain.
I can open a PR for the native and compat-0.3 JSON-RPC transports and the helloworld example, with regression tests.
Relevant log output
JSONRPCTransportStreamingTest.closeCancelsInFlightStream:210 connection still open 10 s after close() ==> expected: <true> but was: <false>Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
fix(jsonrpc): return early from trackStream() for a null future
fix(jsonrpc): cancel in-flight streams on close (a2aproject#1355)