agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

AG-UI MVC run is cancelled when the SSE emitter completes after a client disconnect

Open chcodex opened this issue 1 month ago • 0 comments

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

  1. Run the AG-UI starter with interrupt-on-disconnect: false.
  2. Start a long run and disconnect the client, for example curl --max-time 6 -N -X POST .../agui/run.
  3. Reconnect with the same threadId (a /connect route, or a run with an empty messages array when a snapshot store is enabled).
  4. Only the events produced before the disconnect are replayed. The run never finishes and later reconnects return the same prefix.

Root cause

  • sendEvent only catches IOException. After the container completes the emitter, send() throws IllegalStateException; the exception escapes BaseSubscriber.hookOnNext, Reactor treats it as a stream error and terminates the subscription.
  • hookOnError then calls sendErrorAndComplete, which sends to the same completed emitter and throws again.
  • sendErrorAndComplete also builds Map.of("error", errorMessage), which throws NullPointerException when the failure has no message (for example new 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=true asks for cancellation.
  • A null error message must not throw while reporting an error.

Fix

  • Catch all exceptions in AguiMvcController.sendEvent and sendErrorAndComplete; 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.

chcodex avatar Oct 23 '26 12:10 chcodex