Skip to content

fix(a2a): stop the remote stream and task when an A2aAgent call is cancelled - #4097

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-client-cancel-stream
Nov 12, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/a2a-client-cancel-stream

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

#3663 让被取消的调用关闭它的 Client,但在默认的 JSON-RPC 传输下,这一步实际上什么也没关掉:

  • JSONRPCTransport.close() 在 SDK 1.5 里仍是空方法;流式请求的 future 只存在 sendMessageStreaming 的局部变量里,外面拿不到,也就没人去取消它;
  • 调用被取消时(上游 timeout、取消订阅、父 agent 放弃子任务),A2aAgent 不会通知远端。

结果是:取消之后 SSE 连接继续收事件,ReasoningChunk 等钩子在调用结束后照样触发,远端任务也一直跑到结束、继续消耗 token。

我在 JDK 17 和 21 上实测过:取消 postAsyncSSE 返回的 future 能断开 HTTP 连接(服务端写入失败,客户端不再收到事件)。只是 AgentScope 拿不到这个 future。

改动

  • 新增包内类 StreamTrackingHttpClient:包装 A2AHttpClient,记下 postAsyncSSE / getAsyncSSE 返回的 future,cancelStreams() 取消还没结束的流,其余请求原样转发。默认 JSON-RPC 传输改用 new JSONRPCTransportConfig(httpClient),httpClient 仍由 A2AHttpClientFactory.create() 创建,与 SDK 不传配置时一致。
  • 调用被取消时:
    • 之后到达的事件直接丢弃,不再触发钩子;
    • endCall 取消这次调用的 SSE 流;
    • 如果已经拿到 taskId,在 boundedElastic 上发 tasks/cancel,完成后再关闭 client,不阻塞发起取消的线程。
    • 正常完成或出错结束的调用不发 tasks/cancel,和原来一样。
  • ClientEventContext.task 改为 volatile:它由 HTTP 客户端线程写入,在取消线程上读。
  • 中英文 a2a.md 补充取消调用的行为说明。

已知限制:通过 clientTransports 配置的传输使用调用方自己的 HTTP 客户端,它们的事件流无法关闭;取消远端任务、丢弃迟到事件这两点对它们照样生效。文档里写明了这一点。

测试

  • A2aAgentTest:
    • 调用取消后,对已知 taskId 发 cancelTask;
    • 取消后再到的事件不触发钩子;
    • cancelTask 失败(比如远端不支持取消)时,client 照样关闭;
    • 正常完成的调用不发 cancelTask(防回归,main 上同样通过)。
    • 前两条在 main 上先确认失败(没有 cancelTask;钩子触发次数期望 1,实际 2),修复后通过。
  • StreamTrackingHttpClientTest:
    • 本地起一个持续推 SSE 的服务,cancelStreams() 后服务端看到连接断开;
    • 各 builder 方法原样转发给被包装的客户端,已结束的流不再被记录。
  • A2aHttpEndToEndTest.cancellingCallCancelsRemoteTask:真实服务端上取消调用方的订阅,远端 runner 收到 stop、服务端执行结束。在 main 上先确认失败(远端 30 秒内没有停下),修复后通过。
  • 取消相关的单测和端到端测试各连跑 3 次,结果稳定。
  • 文档:node scripts/check-docs.mjs(432 个页面)和 node --test scripts/tests/*.test.mjs(13 条)通过;mint validate / broken-links 本机没跑。
  • 本地(JDK 21,macOS;CI 用 JDK 17):mvn -B -T1C clean verify 99 个模块全部通过(12,395 个测试,0 失败,247 跳过);之后补了「cancelTask 失败」那条测试,客户端模块 verify 通过。补丁覆盖率:改动的可执行行 68/68,分支 14/14。

Follow-up to #3663

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn -B -T1C clean verify)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

…ncelled

The JSON-RPC transport keeps the future of a streaming request to itself
and its close() does nothing, so closing the client of a cancelled call
(agentscope-ai#3663) left the event stream open: events kept arriving and firing hooks,
and the remote task ran to its end.

- Wrap the HTTP client of the default JSON-RPC transport to keep the
  futures of the event streams, and cancel them when the call ends.
- Drop the events that arrive after the call was cancelled.
- When a cancelled call knows its task id, send tasks/cancel on
  boundedElastic and close the client after it.
- Make ClientEventContext.task volatile: it is written by the HTTP client
  thread and read on the cancelling thread.
@mintlify

mintlify Bot commented Nov 10, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit a2c4075.

Approve preview

@jujn jujn added the P1 紧急不重要 label Nov 10, 2026
@jujn
jujn merged commit 33c3dc5 into agentscope-ai:main Nov 12, 2026
7 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…ncelled (#4097)

Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit 33c3dc5)
Backport-review-patch: 2622c88e0e4e750a297486e81b51ce944fa535a3
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants