Skip to content

fix(a2a): suppress cancellation after final event - #2940

Closed
wmh123wmh1 wants to merge 2 commits into
agentscope-ai:mainfrom
wmh123wmh1:fix/a2a-streaming-cancellation-2927
Closed

wmh123wmh1 wants to merge 2 commits into
agentscope-ai:mainfrom
wmh123wmh1:fix/a2a-streaming-cancellation-2927

Conversation

@wmh123wmh1

Copy link
Copy Markdown
Contributor

Summary

  • Keep a2a-java-sdk 0.3.3.Final and A2A Protocol v0.3 compatibility.
  • Wrap the default JSON-RPC HTTP client to suppress the internal CancellationException emitted after a final status update.
  • Continue forwarding cancellations received before a final event and real transport errors.
  • Add regression tests for the callback behavior.

Testing

  • mvn -pl agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-a2a/agentscope-extensions-a2a-client -am test

Fixes #2927

@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@dailingtao dailingtao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@oss-maintainer oss-maintainer 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.

Summary

Package-private A2AHttpClient decorator that suppresses the spurious CancellationException the a2a-java-sdk 0.3.3 emits after a final status-update, wired into A2aAgent's default JSON-RPC transport. Direction looks right and the callback matrix is well tested (final vs non-final, malformed JSON, non-cancellation errors, builder delegation, forward of post()).

The three substantive findings below are all the same gap: the decorator filters errorConsumer, but not the two other ways a stream terminates — the returned CompletableFuture and completeRunnable. If the SDK signals the cancellation through either of those instead, a caller either still sees the error or sees a stream that never terminates. CI is green and the CLA is signed, so nothing here blocks the change — I'd just like confirmation on the completion-signal question.


Automated review by github-manager-bot

messageConsumer.accept(message);
},
error -> {
if (!completed.get() || !isCancellation(error)) {

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.

[Warning] Suppressing the error here drops the only terminal signal the consumer gets: when the SDK reports the post-final cancellation through errorConsumer, completeRunnable is not invoked either, so a caller that closes its stream / releases resources in onComplete will see a stream that never terminates (it just goes quiet). Please confirm the SDK's behaviour for this path and, if it holds, run completeRunnable (or otherwise signal completion) when the error is suppressed, e.g.

error -> {
    if (completed.get() && isCancellation(error)) {
        completeRunnable.run();   // terminal signal, exactly once
    } else {
        errorConsumer.accept(error);
    }
}

If postAsyncSSE can call both callbacks, guard with an AtomicBoolean compare-and-set so the consumer still sees exactly one terminal event.

Runnable completeRunnable)
throws IOException, InterruptedException {
completed.set(false);
return delegate.postAsyncSSE(

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.

[Warning] The decorator only filters the three callbacks; the CompletableFuture returned by the delegate is passed through untouched. If JdkA2AHttpClient completes that future exceptionally with the same CancellationException (rather than / in addition to invoking errorConsumer), any caller that joins or composes on the returned future will still observe the spurious failure, i.e. the fix would be incomplete for those call sites. Could you verify how the 0.3.3 SDK surfaces this cancellation and, if the future is also affected, wrap the return value as well (.exceptionally(t -> completed.get() && isCancellation(t) ? null : rethrow(t)))? A test that inspects the returned future's state in the suppressed case would pin this down — all current tests drive the callbacks directly and use the mock's never-completed future, so this path is untested.


private static boolean isFinalStatusUpdate(String message) {
try {
JsonNode root = Utils.OBJECT_MAPPER.readTree(message);

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.

[Warning] This runs a full Jackson tree parse for every SSE chunk, on top of the parse the SDK itself already does, so a long message/stream response pays double deserialization on the hot path (artifact/token chunks can be many and large). A cheap pre-filter keeps the behaviour identical while making the non-matching case nearly free:

if (message == null || message.indexOf('"') < 0
        || !message.contains("status-update")) {
    return false;
}

Alternative: only try detection while completed is still false, since once a final status update has been seen there is nothing left to record.

}

@Override
public GetBuilder createGet() {

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.

[Info] createGet() is delegated unwrapped, so only the POST streaming path gets the fix. If the same SDK bug can surface on the GET-based SSE path (e.g. tasks/resubscribe / polling a running task), that path is still exposed. Worth a one-line comment stating the intended scope ("only message/stream over JSON-RPC POST is affected") so the next reader does not treat the missing GET wrapper as an oversight — or wrap GetBuilder symmetrically if it can also stream.

return result != null
&& "status-update".equals(result.path("kind").asText())
&& result.path("final").asBoolean(false);
} catch (IOException | RuntimeException ignored) {

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.

[Info] The parse failure is swallowed silently, which is the right default for non-JSON keep-alives, but it also means a genuinely final event that does not match the expected shape (different envelope, or final nested differently) silently disables the suppression and the user keeps seeing the confusing CancellationException. A one-line debug/trace log here (and one when an error is actually suppressed at line 145) would make field diagnosis of "why is this workaround not working" much cheaper.

builder.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfig());
builder.withTransport(
JSONRPCTransport.class,
new JSONRPCTransportConfig(new CancellationSuppressingA2aHttpClient()));

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.

[Info] This changes the default client for every A2aAgent built without explicit transports, with no opt-out, and the suppression is a behaviour change (errors disappear) rather than a pure fix. Two small things would make it easier to live with: (1) link the upstream SDK issue/PR in the Javadoc of CancellationSuppressingA2aHttpClient together with the version that fixes it, so the workaround can be deleted mechanically later; (2) consider gating it behind an A2aAgentConfig flag (default on) so users who want to observe cancellations are not stuck.

@jujn

jujn commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Fixed in: #3590

@jujn jujn closed this Oct 19, 2026
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]:JSON-RPC streaming client reports "Request cancelled" after receiving final TaskStatusUpdateEvent

4 participants