Skip to content

Commit 1666a0b

Browse files
committed
Complete tasks in background thread
1 parent a9d803e commit 1666a0b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

server-common/src/main/java/io/a2a/server/requesthandlers/DefaultRequestHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,8 @@ public EventKind onMessageSend(MessageSendParams params, ServerCallContext conte
217217
// Send push notification after initial return (for both blocking and non-blocking)
218218
pushNotificationCallback.run();
219219
} finally {
220-
if (interruptedOrNonBlocking) {
221-
trackBackgroundTask(cleanupProducer(taskId, queue, false));
222-
} else {
223-
// For blocking requests, wait for cleanup to complete to ensure agent is removed from map
224-
cleanupProducer(taskId, queue, false).join();
225-
}
220+
// Always track cleanup as background task to avoid blocking Vert.x threads
221+
trackBackgroundTask(cleanupProducer(taskId, queue, false));
226222
}
227223

228224
LOGGER.debug("Returning: {}", etai.eventType());
@@ -527,7 +523,13 @@ private void trackBackgroundTask(CompletableFuture<Void> task) {
527523
task.whenComplete((result, throwable) -> {
528524
try {
529525
if (throwable != null) {
530-
if (throwable instanceof java.util.concurrent.CancellationException) {
526+
// Unwrap CompletionException to check for CancellationException
527+
Throwable cause = throwable;
528+
if (throwable instanceof java.util.concurrent.CompletionException && throwable.getCause() != null) {
529+
cause = throwable.getCause();
530+
}
531+
532+
if (cause instanceof java.util.concurrent.CancellationException) {
531533
LOGGER.debug("Background task cancelled: {}", task);
532534
} else {
533535
LOGGER.error("Background task failed", throwable);

0 commit comments

Comments
 (0)