Skip to content

Commit 03f2499

Browse files
kabirclaude
andcommitted
enhance: Add Task final state auto-close to JSONRPC and REST client transports
Enhance client-side auto-close logic to handle both TaskStatusUpdateEvent and Task objects with final states. This handles edge cases like late subscriptions to already-completed tasks and ensures no connection leaks in any scenario. Changes: - SSEEventListener (JSONRPC): Check both TaskStatusUpdateEvent.isFinal() and Task.status().state().isFinal() before closing connection - RestSSEEventListener (REST): Add complete auto-close logic (was missing entirely) - Pass future parameter through to handleMessage() - Check both event types for final states - Both transports now have identical, defensive auto-close behavior Benefits: - Handles late subscriptions to completed tasks gracefully - Prevents connection leaks in all scenarios (not just TaskStatusUpdateEvent) - Consistent behavior across JSONRPC and REST transports - Defensive programming - better to close on final events than leak connections Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 14afb27 commit 03f2499

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎client/transport/jsonrpc/src/main/java/io/a2a/client/transport/jsonrpc/sse/SSEEventListener.java‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import io.a2a.jsonrpc.common.json.JsonProcessingException;
1111
import io.a2a.spec.A2AError;
1212
import io.a2a.spec.StreamingEventKind;
13+
import io.a2a.spec.Task;
14+
import io.a2a.spec.TaskState;
1315
import io.a2a.spec.TaskStatusUpdateEvent;
1416
import org.jspecify.annotations.Nullable;
1517

@@ -66,13 +68,21 @@ private void handleMessage(String message, @Nullable Future<Void> future) {
6668
eventHandler.accept(event);
6769

6870
// Client-side auto-close on final events to prevent connection leaks
69-
// With ReplicatedQueueManager fix (sending TaskStatusUpdateEvent consistently),
70-
// both local and remote instances send the same event type, so this works correctly
71-
if (event instanceof TaskStatusUpdateEvent && ((TaskStatusUpdateEvent) event).isFinal()) {
72-
if (future != null) {
73-
future.cancel(true); // close SSE channel
71+
// Handles both TaskStatusUpdateEvent and Task objects with final states
72+
// This covers late subscriptions to completed tasks and ensures no connection leaks
73+
boolean shouldClose = false;
74+
if (event instanceof TaskStatusUpdateEvent tue && tue.isFinal()) {
75+
shouldClose = true;
76+
} else if (event instanceof Task task) {
77+
TaskState state = task.status().state();
78+
if (state.isFinal()) {
79+
shouldClose = true;
7480
}
7581
}
82+
83+
if (shouldClose && future != null) {
84+
future.cancel(true); // close SSE channel
85+
}
7686
} catch (A2AError error) {
7787
if (errorHandler != null) {
7888
errorHandler.accept(error);

‎client/transport/rest/src/main/java/io/a2a/client/transport/rest/sse/RestSSEEventListener.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import io.a2a.grpc.StreamResponse;
1111
import io.a2a.grpc.utils.ProtoUtils;
1212
import io.a2a.spec.StreamingEventKind;
13+
import io.a2a.spec.Task;
14+
import io.a2a.spec.TaskState;
15+
import io.a2a.spec.TaskStatusUpdateEvent;
1316
import org.jspecify.annotations.Nullable;
1417

1518
public class RestSSEEventListener {
@@ -29,7 +32,7 @@ public void onMessage(String message, @Nullable Future<Void> completableFuture)
2932
log.fine("Streaming message received: " + message);
3033
io.a2a.grpc.StreamResponse.Builder builder = io.a2a.grpc.StreamResponse.newBuilder();
3134
JsonFormat.parser().merge(message, builder);
32-
handleMessage(builder.build());
35+
handleMessage(builder.build(), completableFuture);
3336
} catch (InvalidProtocolBufferException e) {
3437
errorHandler.accept(RestErrorMapper.mapRestError(message, 500));
3538
}
@@ -44,7 +47,7 @@ public void onError(Throwable throwable, @Nullable Future<Void> future) {
4447
}
4548
}
4649

47-
private void handleMessage(StreamResponse response) {
50+
private void handleMessage(StreamResponse response, @Nullable Future<Void> future) {
4851
StreamingEventKind event;
4952
switch (response.getPayloadCase()) {
5053
case MESSAGE ->
@@ -62,6 +65,23 @@ private void handleMessage(StreamResponse response) {
6265
}
6366
}
6467
eventHandler.accept(event);
68+
69+
// Client-side auto-close on final events to prevent connection leaks
70+
// Handles both TaskStatusUpdateEvent and Task objects with final states
71+
// This covers late subscriptions to completed tasks and ensures no connection leaks
72+
boolean shouldClose = false;
73+
if (event instanceof TaskStatusUpdateEvent tue && tue.isFinal()) {
74+
shouldClose = true;
75+
} else if (event instanceof Task task) {
76+
TaskState state = task.status().state();
77+
if (state.isFinal()) {
78+
shouldClose = true;
79+
}
80+
}
81+
82+
if (shouldClose && future != null) {
83+
future.cancel(true); // close SSE channel
84+
}
6585
}
6686

6787
}

0 commit comments

Comments
 (0)