agentscope-java
agentscope-java copied to clipboard
AG-UI MVC run is cancelled when the SSE emitter completes after a client disconnect
Description
AguiMvcController cancels the AG-UI run subscription whenever sending an event to an already completed SseEmitter fails. With agentscope.agui.interrupt-on-disconnect=false, the agent is supposed to keep running after the client disconnects, but its event stream is torn down instead, so a reconnecting client never receives events produced after the disconnect.
Steps to reproduce
- Run the AG-UI starter with
interrupt-on-disconnect: false. - Start a long run and disconnect the client, for example
curl --max-time 6 -N -X POST .../agui/run. - Reconnect with the same
threadId(a/connectroute, or a run with an emptymessagesarray when a snapshot store is enabled). - Only the events produced before the disconnect are replayed. The run never finishes and later reconnects return the same prefix.
Root cause
sendEventonly catchesIOException. After the container completes the emitter,send()throwsIllegalStateException; the exception escapesBaseSubscriber.hookOnNext, Reactor treats it as a stream error and terminates the subscription.hookOnErrorthen callssendErrorAndComplete, which sends to the same completed emitter and throws again.sendErrorAndCompletealso buildsMap.of("error", errorMessage), which throwsNullPointerExceptionwhen the failure has no message (for examplenew RuntimeException()).
Expected
- A failed send to a completed emitter must never cancel the upstream run.
- When the client is gone, the run continues and its events stay available for later consumers, unless
interrupt-on-disconnect=trueasks for cancellation. - A null error message must not throw while reporting an error.
Fix
- Catch all exceptions in
AguiMvcController.sendEventandsendErrorAndComplete; log at debug and keep the subscription alive. - Fall back to a non-null message when reporting errors.
- Regression tests: a completed emitter does not cancel the run, and a stream error with a null message still completes the emitter.