Repository navigation
fix(a2a): 取消任务后结束服务端执行,并释放服务端线程池 - #3578
Closed
guslegend0510 wants to merge 8 commits into
Closed
guslegend0510 wants to merge 8 commits into
guslegend0510 wants to merge 8 commits into
Conversation
- AgentScopeAgentExecutor: cancelling a task now completes its execution through a per-task signal applied with takeUntilOther, so doFinally runs and blockLast() returns. It used to cancel the upstream subscription, which left the execution blocked forever. - AgentScopeA2aServer and AgentScopeA2aRequestHandler implement AutoCloseable and shut down the thread pools they create; an executor supplied by the caller is left alone. Spring calls close() when the context closes. - The starter's agent-completion-timeout-seconds and consumption-completion-timeout-seconds properties are now applied (defaults unchanged: 60 and 10 seconds). Fixes agentscope-ai#3574 Part of agentscope-ai#3546
Address review findings on the cancellation fix: - Track each execution in AgentScopeAgentExecutor from the start of execute(). Cancel, completion and failure race to publish the task's terminal state and only the first one does, so a cancelled task no longer also reports COMPLETED, FAILED or a result, and output arriving after cancel is dropped. - End the execution in a finally block, so it also ends when stopping the agent throws, and a cancel that arrives before the agent stream is subscribed no longer starts the task. - Keep the default transport executor local to each build(), so servers built from the same builder each own their thread pool, and create the pools only after all checks so a failed build leaves nothing behind. - Close the servers and handlers built in tests, and wait for the test execution threads to stop. Fixes agentscope-ai#3574
Address the second review round on the cancellation fix: - A cancel that arrives after the task completed or failed now throws TaskNotCancelableError, as the SDK does for tasks that ended concurrently, instead of returning without an event and leaving the SDK's cancel request waiting. - Each execution keeps one state under a lock. State checks and the events they guard (WORKING, artifacts, the terminal state, the failure message) are published under that lock, so nothing is published after the terminal state. stop() and ending the execution stay outside it. - Rewrite the remaining cancel test without fixed sleeps, and make test thread pools fail the test if their threads do not stop. Fixes agentscope-ai#3574
- A cancel rejected after completion closes the queue the SDK tapped for it. The SDK only drains and closes that queue when cancel returns, so the task's main queue was never released and a later resubscription waited forever. - Only the cancel that won ends the execution. A concurrent cancel that is rejected no longer ends it before the winner stopped the agent. - The initial Task of a streaming execution is published only while the execution is running, so nothing follows CANCELED.
Cancelling now ends the server-side execution, so the remote task is cancelled and the execution terminates.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Collaborator
|
冲突了。另外如果这个不是必须的话,可以等升级之后再修 |
Contributor
Author
好的 我升级到1.0版本再去修复一下 |
5 tasks done
This was referenced Oct 20, 2026
Merged
5 tasks done
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
Fixes #3574
Part of #3546
背景
客户端取消 A2A 任务后,服务端的那次执行一直不结束(#3574)。同一块代码里还有两个相关问题:服务端自己创建的线程池从来不关闭;Starter 声明的两个超时配置项从来没有生效。
改动
AgentScopeAgentExecutor原来取消的是doOnSubscribe里保存的上游订阅,下游的doFinally/blockLast()收不到终止信号,执行线程一直阻塞。现在每次执行从execute()开始就登记一个状态对象,并在事件处理之后、doFinally之前用takeUntilOther接入结束信号:stop()和结束执行在锁外进行。TaskNotCancelableError(与 SDK 对并发结束的任务的处理一致),抛出前先关闭 SDK 为这次取消建立的事件队列:SDK 只在取消正常返回时才消费并关闭它,不关的话这个任务的队列无法回收。stop()抛异常,执行也会结束;取消发生在 Agent 输出流订阅之前时,不会再发出初始 Task 或进入 WORKING。stop()再结束执行」的顺序,让 runner 仍能中断 Agent。AgentScopeA2aServer和AgentScopeA2aRequestHandler实现AutoCloseable,close()用shutdownNow()关闭它们自己创建的线程池(请求中断仍在执行的任务,不等待其结束);通过Builder#executor(...)传入的线程池由调用方管理。默认的 transport 线程池只属于本次build()创建的那台 server,同一个 Builder 多次构建互不影响;线程池在所有校验之后才创建,构建失败不会遗留线程池。Spring 会在上下文关闭时自动调用close()。agentscope.a2a.server.agent-completion-timeout-seconds和consumption-completion-timeout-seconds现在会经AgentExecuteProperties传到请求处理器;不配置时仍为 60 秒和 10 秒。AgentScopeA2aRequestHandler.Builder也新增了对应的两个设置方法。server.close()及其语义。已知限制(未在本 PR 处理)
execute()已经返回、执行登记已移除,但 SDK 还没把 COMPLETED 写入 TaskStore 时,如果恰好到来一个取消请求,它能通过 SDK 的终态预检;执行器已找不到这次执行,仍会发出 CANCELED,最终状态可能被覆盖为 CANCELED。这是 SDK 0.3 在事件入队之后才持久化造成的既有竞争,原实现同样存在,执行器无法看到存储何时更新。SDK 1.4.0.Final 改由后台的MainEventBusProcessor先更新 TaskStore 再把事件分发给各个订阅队列,迁移到 1.x(#3546)后再重新验证这个窗口。测试
AgentScopeAgentExecutorTest,取消相关共 15 个用例(原有 3 个,其中testCancelTaskSuccessfully改为不依赖固定等待;新增 12 个):stop()抛异常、stop()让 Agent 流完成或报错、取消后的迟到输出、正在处理的事件遇到取消、取消后的失败、订阅前取消:校验execute()能返回,恰好一个 CANCELED 且它是最后一个事件,没有其他终态或结果消息。TaskNotCancelableError)、不发出 CANCELED,并关闭这次取消的事件队列。这两个用例在执行器层面用同一线程重入来构造竞争。stop()之后才结束。AgentScopeA2aRequestHandlerTest:close()关闭线程池。AgentScopeA2aServerTest:close()关闭自建线程池、不关闭调用方的线程池、同一个 Builder 构建两台 server 各自拥有线程池、构建失败后重试、超时传到请求处理器。AgentscopeA2aAutoConfigurationTest:两个配置项生效,上下文关闭后线程池被关闭。stop()让流完成或报错后又出现终态、订阅前取消后仍发出事件、完成后取消没有被拒绝、正在处理的事件在 CANCELED 之后发出、拒绝取消后队列不回收、第二个取消提前结束执行。A2aHttpEndToEndTest(test(a2a): 为 A2A Starter 增加走真实 HTTP 的端到端测试 #3573 已合入):本 PR 去掉了interruptCancelsRemoteTask的@Disabled,经真实 HTTP 取消远端任务,连续 3 次通过,服务端每次执行都有对应的结束日志。本地结果(JDK 17,macOS):
mvn -B -T1 clean verify(已合入最新 main):97 个模块全部通过(10,780 个测试,0 失败,0 错误,198 跳过)。Checklist
mvn spotless:applymvn test)