#2885·ag-ui

[Bug]: a detached HttpAgent run keeps its HTTP response open and reads it to the end

Author: itsklimovCreated Oct 1, 2026Updated Oct 1, 2026

Describe the Bug

transformHttpEventStream (sdks/typescript/packages/client/src/transform/http.ts) subscribes to the request observable itself and returns eventSubject.asObservable(), so it keeps reading for later subscribers, as transform/__tests__/sse.test.ts expects. An unsubscribe from a run's pipeline never reaches the request, and nothing else ends it: the reader teardown in src/run/http-request.ts (reader.cancel()) never runs, and HttpAgent's abort signal is aborted only by abortRun().

AbstractAgent.detachActiveRun() ends a run through takeUntil(activeRunDetach$). With HttpAgent, the run settles, but the HTTP response stays open and the client keeps reading it for nobody until the server ends it. The same holds for any connect() built on transformHttpEventStream, for example CopilotKit's runtime agent (packages/core/src/agent.ts). CopilotKit's connectAgent calls detachActiveRun() expecting "the previous socket torn down before a new one can open" (packages/core/src/core/run-handler.ts); that socket stays open.

Steps to Reproduce

  1. Start a loopback HTTP server that answers with content-type: text/event-stream, writes one RUN_STARTED frame, and holds the response open.
  2. const agent = new HttpAgent({ url }), then agent.runAgent(...) and wait for onRunStartedEvent.
  3. await agent.detachActiveRun().
  4. Watch the server's response for close.

Expected Behavior

Detaching releases the read: the response closes within a moment. A later run on the same agent reads its own response to the end.

Actual: the first response never closes while the server holds it.

Environment

@ag-ui/client on main at 015baf5d (1.0.1), and 0.0.59. Node 22.23.1.

Logs & Errors

With the server held open, the response close does not fire within 2 s of detachActiveRun() resolving. When the server later closes the socket, the orphaned reader surfaces as an unhandled TypeError: terminated (UND_ERR_SOCKET, "other side closed").

Related

  • #2417: abortRun() settlement and source release (abort, not detach).
  • #2697: reader cancellation failures during teardown, in the same run/http-request.ts teardown this would reach.