[Bug]: a detached HttpAgent run keeps its HTTP response open and reads it to the end
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
- Start a loopback HTTP server that answers with
content-type: text/event-stream, writes oneRUN_STARTEDframe, and holds the response open. const agent = new HttpAgent({ url }), thenagent.runAgent(...)and wait foronRunStartedEvent.await agent.detachActiveRun().- 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.tsteardown this would reach.
Source: ag-ui-protocol/ag-ui