Skip to content

fix(a2a): 取消任务后结束服务端执行,并释放服务端线程池 - #3578

Closed
guslegend0510 wants to merge 8 commits into
agentscope-ai:mainfrom
guslegend0510:fix/issue-3546-03-server-lifecycle
Closed

guslegend0510 wants to merge 8 commits into
agentscope-ai:mainfrom
guslegend0510:fix/issue-3546-03-server-lifecycle

Conversation

@guslegend0510

@guslegend0510 guslegend0510 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

Fixes #3574
Part of #3546

背景

客户端取消 A2A 任务后,服务端的那次执行一直不结束(#3574)。同一块代码里还有两个相关问题:服务端自己创建的线程池从来不关闭;Starter 声明的两个超时配置项从来没有生效。

改动

  • 取消后结束执行([Bug]: 取消 A2A 任务后,服务端执行一直阻塞不结束 #3574):AgentScopeAgentExecutor 原来取消的是 doOnSubscribe 里保存的上游订阅,下游的 doFinally/blockLast() 收不到终止信号,执行线程一直阻塞。现在每次执行从 execute() 开始就登记一个状态对象,并在事件处理之后、doFinally 之前用 takeUntilOther 接入结束信号:
    • 取消与完成/失败只有一个能生效。状态判断和它所保护的事件(初始 Task、WORKING、输出片段、终态、失败消息)在同一把锁里发布,取消生效后不再发布任何事件;stop() 和结束执行在锁外进行。
    • 执行仍在进行、但完成或失败已先发出时,取消会抛出 TaskNotCancelableError(与 SDK 对并发结束的任务的处理一致),抛出前先关闭 SDK 为这次取消建立的事件队列:SDK 只在取消正常返回时才消费并关闭它,不关的话这个任务的队列无法回收。
    • 同一任务同时到来多个取消请求时,只有抢到取消的那一个负责停止 Agent 并结束执行,其余的被拒绝,不会提前结束执行。
    • 即使 stop() 抛异常,执行也会结束;取消发生在 Agent 输出流订阅之前时,不会再发出初始 Task 或进入 WORKING。
    • 保持「先 stop() 再结束执行」的顺序,让 runner 仍能中断 Agent。
    • 阻塞请求出错时原有的两条错误消息保持不变。
  • 释放线程池:AgentScopeA2aServer 和 AgentScopeA2aRequestHandler 实现 AutoCloseable,close() 用 shutdownNow() 关闭它们自己创建的线程池(请求中断仍在执行的任务,不等待其结束);通过 Builder#executor(...) 传入的线程池由调用方管理。默认的 transport 线程池只属于本次 build() 创建的那台 server,同一个 Builder 多次构建互不影响;线程池在所有校验之后才创建,构建失败不会遗留线程池。Spring 会在上下文关闭时自动调用 close()。
  • 超时配置生效:Starter 的 agentscope.a2a.server.agent-completion-timeout-seconds 和 consumption-completion-timeout-seconds 现在会经 AgentExecuteProperties 传到请求处理器;不配置时仍为 60 秒和 10 秒。AgentScopeA2aRequestHandler.Builder 也新增了对应的两个设置方法。
  • 文档:中英文 a2a.md 的服务端示例补充了 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() 之后才结束。
    • 另新增:流式请求的初始 Task 入队失败时上报 FAILED。
  • AgentScopeA2aRequestHandlerTest:
    • 用 SDK 的请求处理器、TaskStore 和队列管理器跑一次流式请求,在完成已发出、存储尚未更新时取消:校验取消被拒绝、事件队列数不变、请求结束后任务的队列被回收。
    • 默认超时 60/10、自定义超时、close() 关闭线程池。
  • AgentScopeA2aServerTest:close() 关闭自建线程池、不关闭调用方的线程池、同一个 Builder 构建两台 server 各自拥有线程池、构建失败后重试、超时传到请求处理器。
  • AgentscopeA2aAutoConfigurationTest:两个配置项生效,上下文关闭后线程池被关闭。
  • 以下场景的回归用例都在修复前的代码上运行过并失败:取消后执行挂住(流式、阻塞)、stop() 让流完成或报错后又出现终态、订阅前取消后仍发出事件、完成后取消没有被拒绝、正在处理的事件在 CANCELED 之后发出、拒绝取消后队列不回收、第二个取消提前结束执行。
  • 测试中构建的 server/handler 都在用例结束后关闭,测试线程在用例结束后确认退出。
  • 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 跳过)。
  • 补丁覆盖率:改动的生产代码行 142/142、分支 24/24。

Checklist

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

- 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

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jujn

jujn commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

冲突了。另外如果这个不是必须的话,可以等升级之后再修

@guslegend0510

Copy link
Copy Markdown
Contributor Author

冲突了。另外如果这个不是必须的话,可以等升级之后再修

好的 我升级到1.0版本再去修复一下

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]: 取消 A2A 任务后,服务端执行一直阻塞不结束

3 participants