Repository navigation
Open
Labels
Description
SSE 事件流卡在 1000 个事件问题 —— 根因分析与修复方案
1. 问题现象
- 测试脚本稳定复现:SSE 流式事件精确在第 1000 个事件处停止。
- 服务端最终报错:
TimeoutException: Did not observe any item or terminal signal within 600000ms。 - 该问题与事件计数强相关(精确卡在1000),而非偶发网络抖动,多次测试均可复现。
- 已排除的错误方向:
JsonRpcTransportWrapper的 8192 容量 backpressure buffer(onBackpressureBuffer+BufferOverflowStrategy.ERROR)—— 容量远大于1000,且策略是抛错而非阻塞,现象不匹配。
2. 根因确认
问题根源在第三方闭源依赖 io.github.a2asdk:a2a-java-sdk-server-common(当前项目通过 agentscope 2.0.0 正式版间接引入,确认版本号为 0.3.3.Final,与之前排查的 RC3 版本一致,因此结论对当前正式版同样适用)。
2.1 核心类:io.a2a.server.events.EventQueue
通过反编译字节码(javap -p -v)确认:
-
默认队列容量硬编码为 1000
- 主构造函数:
EventQueue(int queueSize),内部创建new Semaphore(queueSize, true)(fair 模式)。 - 子队列(父子关系)构造函数:
EventQueue(EventQueue parent)内部硬编码调用this(1000)。 - 字节码证据:
sipush 1000出现在该构造函数中。
- 主构造函数:
-
enqueueEvent()使用无超时的阻塞式acquire()27: aload_0 28: getfield #39 // semaphore 31: invokevirtual #86 // Semaphore.acquire:()V <-- 无超时阻塞! 34: goto 55 ... 55: aload_0 56: getfield #15 // queue: BlockingQueue 59: aload_1 60: invokeinterface #107 // BlockingQueue.add- 每次入队前必须先拿到一个
Semaphore许可;许可总数 = 队列容量(1000)。 - 只有消费者取出一个事件(
dequeueEventItem)才会release()一个许可。 - 该
acquire()调用没有超时参数,如果没有可用许可,会无限期阻塞当前线程。
- 每次入队前必须先拿到一个
2.2 触发链路
- Agent 执行过程中通过
AgentScopeAgentExecutor→TaskUpdater→EventQueue.enqueueEvent()持续产生事件(reasoning/tool/artifact 等)。 - 事件消费端是 SSE 输出(
JsonRpcTransportWrapper→ 客户端网络连接)。 - 一旦消费速度跟不上生产速度(例如客户端网络慢、下游处理阻塞等),未消费事件在队列中堆积。
- 堆到 1000 个后,第 1001 次
enqueueEvent()在semaphore.acquire()处永久阻塞——生产者线程被卡死,不再产生新事件。 - 表现为“事件数精确停在1000”,且后续什么都不再发生。
- 最终触发此前已加的 30 秒空闲超时兜底机制(
DynamicModelMiddleware中的resultFlux.timeout(Duration.ofSeconds(30))),报错看起来像“模型超时”,但真正原因是队列阻塞死锁,不是模型响应变慢。
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
zouyx commentedon Aug 1, 2026
我理解这个 1000 应该是相对合理,如果下游消费能力不够快,会造成队列堵塞。
至于是否要增加 timeout,我理解是因为这个事件是强一致性,不能丢失。
暂时我判断 a2a 这个框架实现是没什么问题。
我建议是优化下游消费能力比较合理。
Evader-lgb commentedon Nov 2, 2026
补充一个不依赖 SSE、模型或网络速度的队列级复现。针对 SDK 0.3.3.Final:
我最初在 #3941 尝试了应用侧 enqueue hook 出队回收,但复审发现 dequeueEventItem() 同时触发消费者启动信号,改变生命周期,因此已经撤回并关闭该 PR。
现在将修复放到了 SDK 的 0.3.x 维护分支: a2aproject/a2a-java#1256
新实现仅让默认 InMemoryQueueManager 使用分发模式,不再保留无人消费的 main 副本,也不做 bookkeeping dequeue。子队列的有界背压保留;直接创建的队列、自定义工厂和复制队列的自定义工厂保持原行为。
队列回归测试源码覆盖 10000 条事件、双订阅者、慢订阅者、启动信号、重新订阅及清理。未修改的 0.3.3.Final 在长流用例中超时;修复分支运行 mvn -B -ntp -pl server-common -am test,本地 411 项测试全部通过,其中 server-common 265 项。
目前仍是待维护者审查的上游 PR,并非已发布修复,也没有声称复现了某一条线上完整链路。CI 尚未全绿:TCK 在 Python 依赖安装阶段遇到 Python 3.15 / PyO3 最高支持 3.14 的兼容问题,尚未执行到 Java 服务。AgentScope 2.0.x 后续还需要接入上游发布版本,或由维护者确定 backport 方案。
Evader-lgb commentedon Nov 3, 2026
Root cause turned out to be upstream in the A2A Java SDK, and it's fixed and released now.
The 0.3.x
InMemoryQueueManagerkept an unread copy of every event in MainQueue and held a separate semaphore permit for it that consuming child queues never released, so SSE task event streams stall permanently once ~1000 events (the default capacity) have been emitted, even with fast consumers. Raising the capacity only moves the failure point.The fix is a2aproject/a2a-java#1276, released to Maven Central as
io.github.a2asdk:*:0.3.4(https://github.com/a2aproject/a2a-java/releases/tag/v0.3.4.Final). The default manager now dispatches to child queues without keeping the unused main copy; there's a 10000-event ordered-delivery regression test upstream that times out on unmodified 0.3.3.Final.If you're blocked on this today: bumping
io.github.a2asdkfrom0.3.3.Finalto0.3.4in your own build fixes the stall, no code changes needed on your side.For the 2.0.x line itself, I've opened #3957 bumping the SDK version in
agentscope-dependencies-bom, plus a small adaptation ofJsonRpcTransportWrapper(0.3.4 moved the SDK from Jackson to Gson), so the fix can ship with the 2.0.5 maintenance release. Happy to rework or drop it if you'd rather handle the bump yourself when cutting 2.0.5.