Skip to content

[Bug]:JSON-RPC streaming client reports "Request cancelled" after receiving final TaskStatusUpdateEvent #2927

Closed

Description

@skyXJJ

Description

When using the JSON-RPC streaming client with a2a-java-sdk version 0.3.3.Final, the error callback is invoked with:

Request cancelled

after the client has already received a valid terminal TaskStatusUpdateEvent with final=true.

The remote A2A task completes successfully, but the client reports the normal end of the stream as an error.

## Environment

- a2a-java-sdk: 0.3.3.Final
- Transport: JSON-RPC over SSE
- HTTP client: JdkA2AHttpClient
- Java: 21
- A2A method: message/stream
- Protocol version: A2A 0.3.x

Relevant dependencies:

<dependency>
    <groupId>io.github.a2asdk</groupId>
    <artifactId>a2a-java-sdk-client</artifactId>
    <version>0.3.3.Final</version>
</dependency>

<dependency>
    <groupId>io.github.a2asdk</groupId>
    <artifactId>a2a-java-sdk-client-transport-jsonrpc</artifactId>
    <version>0.3.3.Final</version>
</dependency>

<dependency>
    <groupId>io.github.a2asdk</groupId>
    <artifactId>a2a-java-sdk-http-client</artifactId>
    <version>0.3.3.Final</version>
</dependency>

## Steps to reproduce

Create an A2A client with JSON-RPC streaming enabled:

ClientConfig config = ClientConfig.builder()
        .setStreaming(true)
        .setPolling(false)
        .build();

Client client = Client.builder(agentCard)
        .withTransport(
                JSONRPCTransport.class,
                new JSONRPCTransportConfig(new JdkA2AHttpClient()))
        .clientConfig(config)
        .build();

Send a streaming message:

client.sendMessage(
        message,
        List.of((event, card) -> {
            System.out.println("EVENT: " + event);
        }),
        error -> {
            System.out.println("ERROR: " + error);
        },
        null);

The server returns normal A2A SSE events and eventually sends:

{
  "jsonrpc": "2.0",
  "id": "request-id",
  "result": {
    "taskId": "task-id",
    "status": {
      "state": "completed",
      "timestamp": "2026-09-01T06:19:29.2982548Z"
    },
    "contextId": "context-id",
    "final": true,
    "kind": "status-update"
  }
}

The server logs confirm that the task completed successfully:

Agent execution completed successfully
Task completed: signal=onComplete

However, immediately after the final event, the client error callback receives:

Request cancelled

## Actual behavior

The callback sequence is effectively:

TaskStatusUpdateEvent(final=true)
errorHandler(Request cancelled)

The application therefore sees a successfully completed A2A task as a failed streaming request.

The issue does not occur when the same endpoint is called with curl -N. curl receives the terminal event and exits normally.

## Expected behavior

After receiving:

{
  "kind": "status-update",
  "final": true,
  "status": {
    "state": "completed"
  }
}

the client should complete normally.

Expected callback behavior:

TaskStatusUpdateEvent(final=true)
completion callback

The error callback should not receive Request cancelled for an internally initiated stream shutdown after a valid final event.

## Suspected root cause

SSEEventListener.handleMessage() appears to cancel the HTTP future after delivering a final status event:

eventHandler.accept(streamingEvent);

if (streamingEvent instanceof TaskStatusUpdateEvent statusUpdate
        && statusUpdate.isFinal()) {
    future.cancel(true);
}

Cancelling the JDK HTTP client's future causes the HTTP layer to report:

Request cancelled

The resulting exception is then forwarded by SSEEventListener.onError():

public void onError(Throwable error, Future<Void> future) {
    if (errorHandler != null) {
        errorHandler.accept(error);
    }
    future.cancel(true);
}

The client therefore does not distinguish between:

1. an internal cancellation after receiving final=true;
2. a user-initiated cancellation;
3. an actual network or transport failure.

The completed flag also appears to be set only from onComplete(), not when a final TaskStatusUpdateEvent is received.

## Suggested fix

Mark the listener as successfully completed before cancelling the HTTP future, and suppress the cancellation exception caused by this internal
cleanup.

For example:

private volatile boolean completed;

private void handleMessage(JsonNode message, Future<Void> future) {
    StreamingEventKind event = parseEvent(message);

    eventHandler.accept(event);

    if (event instanceof TaskStatusUpdateEvent statusUpdate
            && statusUpdate.isFinal()) {
        completed = true;
        future.cancel(true);
        signalSuccessfulCompletion();
    }
}

public void onError(Throwable error, Future<Void> future) {
    if (completed && isCancellation(error)) {
        return;
    }

    if (errorHandler != null) {
        errorHandler.accept(error);
    }
}

Alternatively, the client could wait for the server to close the SSE connection naturally instead of cancelling the future after receiving
final=true.

## Workaround

At the application layer, we currently ignore Request cancelled only when:

- a final event has already been received; or
- valid response artifacts have already been received.

A cancellation received before any valid response is still treated as a real error.

This workaround prevents successful A2A streaming tasks from being reported as failures, but the behavior would be better fixed inside the SDK.

Activity

  1. added
    bug
    on Sep 1, 2026
  2. wmh123wmh1 commented on Sep 2, 2026

    @wmh123wmh1
    Contributor

    Hi, I’d like to investigate this issue.

    AgentScope Java currently uses a2a-java-sdk 0.3.3.Final. I’ll first reproduce the spurious CancellationException after a final TaskStatusUpdateEvent, then add a regression test and determine whether the fix should be made in AgentScope Java or upstream in the A2A SDK.

    If no one is currently working on it, I’d be happy to submit a PR.

  3. wmh123wmh1 commented on Sep 2, 2026

    @wmh123wmh1
    Contributor

    I traced the current AgentScope path further. Once a terminal A2A event is handled, ClientEventContext.complete(...) already completes the AgentScope call successfully; a later SDK cancellation therefore does not reach the caller as a second failure, although the current A2aAgent implementation still logs it as a cancellation warning.

    The reported error-callback behavior itself is in a2a-java-sdk 0.3.3.Final (JdkA2AHttpClient), and a corresponding fix has already landed upstream in a2a-java#951. I’m checking whether a compatible dependency update is possible before proposing an AgentScope change, so I’ll avoid opening a PR here that cannot meaningfully reproduce the reported callback behavior.

  4. skyXJJ commented on Sep 2, 2026

    @skyXJJ
    Author

    Thanks for the detailed investigation.

    One additional constraint on our side is that our current A2A integration is still based on A2A Protocol v0.3, so we need to remain compatible with the v0.3 protocol for now.

    I noticed that the fix in a2a-java#951 is available in a newer A2A Java SDK release, while AgentScope Java currently depends on a2a-java-sdk 0.3.3.Final.

    Given this constraint, would it be possible to either:

    upgrade to a newer SDK while still keeping compatibility with A2A Protocol v0.3, or
    backport the #951 fix to the v0.3-compatible SDK/AgentScope integration?

    Since we are still seeing the CancellationException with the current 0.3.3.Final dependency, we'd prefer a solution that does not require migrating our A2A protocol from v0.3 to v1.0 yet.

    Thanks again for looking into this.

  5. wmh123wmh1 commented on Sep 2, 2026

    @wmh123wmh1
    Contributor

    Thanks for the clarification.

    I opened PR #2940 with an AgentScope-side compatibility fix. It keeps the current a2a-java-sdk 0.3.3.Final and A2A Protocol v0.3, without migrating to Protocol v1.0.

    The fix wraps the default JSON-RPC HTTP client and suppresses only the internal CancellationException received after a terminal TaskStatusUpdateEvent. Cancellations before the final event and real transport errors are still propagated.

    Regression tests were added for these cases.

  6. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed

    Verified against main @ 39cd304a.

    Root cause: the "Request cancelled" text is not produced by this repo — it comes from the pinned upstream a2a-java-sdk 0.3.3.Final (agentscope-dependencies-bom/pom.xml:99-101), which fires the error callback with a CancellationException when it tears down the SSE stream right after a valid terminal TaskStatusUpdateEvent. Our wrapper does not guard that callback:

    • .../A2aAgent.java:218-239 — the err == null success branch is guarded by isTerminalDelivered() (:220), but the CancellationException branch (:232-234) and the generic error branch (:236-238) call completeExceptionally(err) unconditionally.
    • ClientEventContext.java:100-117 — complete() and completeExceptionally() contend on the same terminalDelivered.compareAndSet(false, true), so when the error callback wins the race against TaskUpdateEventHandler.java:119-121 (context.complete(msg) after post-reasoning hooks), the Mono terminates with an error even though the task finished successfully.
    • A2aAgentTest.java has no CancellationException / terminal-then-error case, so CI cannot catch the regression.

    Impact: every A2aAgent user with streaming (message/stream, JSON-RPC transport) gets a spurious failure on a successful remote task. Non-deterministic: the window widens with slow post-reasoning hooks or artifact conversion. Severity: medium (wrong result surfaced to the caller, no data loss).

    Suggested fix: apply the existing guard to both error branches — if (!eventContext.isTerminalDelivered()) { eventContext.completeExceptionally(err); } (and the same inside the CancellationException branch after the warn log) at A2aAgent.java:232-238, keeping the CAS in ClientEventContext as the second safety net. Add a regression test that delivers a terminal event and then invokes the error callback with CancellationException, asserting the Mono resolves with the successful result.

    No open PR touches A2aAgent.java, so this is not a duplicate. An automated fix proposal will be generated on request — reply /approve to proceed with PR generation.


    Automated evaluation by github-manager-bot

  7. oss-maintainer commented on Sep 13, 2026

    @oss-maintainer
    Collaborator

    Correction to the comment above — an open PR already covers this; no automated proposal will be generated

    The last line of the previous comment ("No open PR touches A2aAgent.java") was wrong: the file-level duplicate scan had not finished when it was posted. Corrected state against main @ 39cd304a:

    So the root-cause analysis above stands, but PR generation is skipped to avoid a competing duplicate. One input that would help the #2940 review rather than a new PR: the evaluation above locates the guard at A2aAgent.java:232-238 (isTerminalDelivered() short-circuit, reusing the pattern already applied on the err == null branch at :220), whereas #2940 resolves it one layer lower by wrapping the JSON-RPC HTTP client. Both close the race; the A2aAgent-side guard additionally covers error paths the client wrapper does not see, and the client-wrapper side needs no change to our callback ordering. Reviewers can pick the seam.

    Automated by github-manager-bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions