Repository navigation
fix(a2a): stop the remote stream and task when an A2aAgent call is cancelled - #4097
Merged
jujn merged 1 commit intoNov 12, 2026
Merged
Conversation
…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.
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
jujn
approved these changes
Nov 12, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 流;boundedElastic上发tasks/cancel,完成后再关闭 client,不阻塞发起取消的线程。tasks/cancel,和原来一样。ClientEventContext.task改为volatile:它由 HTTP 客户端线程写入,在取消线程上读。a2a.md补充取消调用的行为说明。已知限制:通过
clientTransports配置的传输使用调用方自己的 HTTP 客户端,它们的事件流无法关闭;取消远端任务、丢弃迟到事件这两点对它们照样生效。文档里写明了这一点。测试
A2aAgentTest:cancelTask;cancelTask失败(比如远端不支持取消)时,client 照样关闭;cancelTask(防回归,main 上同样通过)。cancelTask;钩子触发次数期望 1,实际 2),修复后通过。StreamTrackingHttpClientTest:cancelStreams()后服务端看到连接断开;A2aHttpEndToEndTest.cancellingCallCancelsRemoteTask:真实服务端上取消调用方的订阅,远端 runner 收到 stop、服务端执行结束。在 main 上先确认失败(远端 30 秒内没有停下),修复后通过。node scripts/check-docs.mjs(432 个页面)和node --test scripts/tests/*.test.mjs(13 条)通过;mint validate/broken-links本机没跑。mvn -B -T1C clean verify99 个模块全部通过(12,395 个测试,0 失败,247 跳过);之后补了「cancelTask失败」那条测试,客户端模块verify通过。补丁覆盖率:改动的可执行行 68/68,分支 14/14。Follow-up to #3663
Checklist
mvn spotless:applymvn -B -T1C clean verify)