[GH-ISSUE #2614] [Issue]: /retry-agents SSE route never aborts upstream agent LLM calls on client disconnect (leak) #1287

Closed
opened 2026-10-21 05:30:44 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Romuromylus on GitHub (Jun 18, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2614

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — leak. The /retry-agents SSE route builds no AbortController, registers no client-disconnect listener, and threads no abort signal into the agent LLM calls. If the client disconnects mid-retry, every in-flight tracker/agent provider request continues to completion, burning tokens/time with no cancellation. It also emits SSE via the unguarded sendSseEvent, which can throw on a destroyed socket.

Expected behavior

Mirror the main POST /api/generate handler: on client disconnect, abort the upstream provider calls and stop writing to the closed socket.

Actual behavior

The retry handler starts SSE and runs agent batches with no abort wiring:

// packages/server/src/routes/generate/retry-agents-route.ts (~2572-2935)
startSseReply(reply);                       // ~2588
// ... executeRetryBatches (~2757), executeLorebookKeeperRetries (~2768-2782)
// NO  const abortController = new AbortController()
// NO  reply.raw.on("close", () => abortController.abort())
// agentContext built by buildRetryAgentContext (~384-752) never sets .signal
} finally {
  reply.raw.end();                          // ~2932-2933 — only cleanup
}

Abort is plumbed exclusively via context.signal → provider.chatComplete({ signal }) (agent-executor.ts:343,473,552,706; base-provider.ts:114). Because the retry route leaves context.signal undefined, every retried agent LLM call runs to upstream completion after a disconnect; executeRetryBatches runs job groups in parallel (Promise.allSettled), so multiple in-flight requests leak at once. By contrast generate.routes.ts builds abortController (~812), monkey-patches reply.raw.write to no-op after disconnect (~979-987), registers onClose -> abortController.abort() (~989-1007), threads signal into its agent context and provider calls, and off()s the listener in finally. The retry route also uses the unguarded sendSseEvent (sse.ts:14-16, a bare reply.raw.write with no try/catch) for all post-start events, while trySendSseEvent (sse.ts:18-24) exists precisely to swallow writes-after-disconnect.

Steps to reproduce

  1. On a chat with multiple agents, start a tracker/agent retry (HUD "retry agents" / re-run trackers).
  2. Close the tab or drop the network before the run finishes.
  3. Observe the upstream provider requests continue to completion (token/time spend) with no cancellation.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch. Mirror the main generate route: create const abortController = new AbortController(), register reply.raw.on("close", () => abortController.abort()), set agentContext.signal = abortController.signal (and on any pre-generation agent context), switch the post-start sendSseEvent calls to trySendSseEvent (or guard reply.raw.write), and off() the listener in finally. The reference implementation is the in-repo generate.routes.ts abort wiring.

Regression notes. Not a regression — main's retry-agents-route.ts has the same shape (no AbortController/close-listener/context.signal/trySendSseEvent). The retry route never had abort wiring; staging neither broke nor dropped it.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2614 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — leak.** The `/retry-agents` SSE route builds no `AbortController`, registers no client-disconnect listener, and threads no abort signal into the agent LLM calls. If the client disconnects mid-retry, every in-flight tracker/agent provider request continues to completion, burning tokens/time with no cancellation. It also emits SSE via the unguarded `sendSseEvent`, which can throw on a destroyed socket. ## Expected behavior Mirror the main `POST /api/generate` handler: on client disconnect, abort the upstream provider calls and stop writing to the closed socket. ## Actual behavior The retry handler starts SSE and runs agent batches with no abort wiring: ```ts // packages/server/src/routes/generate/retry-agents-route.ts (~2572-2935) startSseReply(reply); // ~2588 // ... executeRetryBatches (~2757), executeLorebookKeeperRetries (~2768-2782) // NO const abortController = new AbortController() // NO reply.raw.on("close", () => abortController.abort()) // agentContext built by buildRetryAgentContext (~384-752) never sets .signal } finally { reply.raw.end(); // ~2932-2933 — only cleanup } ``` Abort is plumbed exclusively via `context.signal` → `provider.chatComplete({ signal })` (`agent-executor.ts:343,473,552,706`; `base-provider.ts:114`). Because the retry route leaves `context.signal` undefined, every retried agent LLM call runs to upstream completion after a disconnect; `executeRetryBatches` runs job groups in parallel (`Promise.allSettled`), so multiple in-flight requests leak at once. By contrast `generate.routes.ts` builds `abortController` (~812), monkey-patches `reply.raw.write` to no-op after disconnect (~979-987), registers `onClose -> abortController.abort()` (~989-1007), threads `signal` into its agent context and provider calls, and `off()`s the listener in finally. The retry route also uses the unguarded `sendSseEvent` (`sse.ts:14-16`, a bare `reply.raw.write` with no try/catch) for all post-start events, while `trySendSseEvent` (`sse.ts:18-24`) exists precisely to swallow writes-after-disconnect. ## Steps to reproduce 1. On a chat with multiple agents, start a tracker/agent retry (HUD "retry agents" / re-run trackers). 2. Close the tab or drop the network before the run finishes. 3. Observe the upstream provider requests continue to completion (token/time spend) with no cancellation. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context **Fix sketch.** Mirror the main generate route: create `const abortController = new AbortController()`, register `reply.raw.on("close", () => abortController.abort())`, set `agentContext.signal = abortController.signal` (and on any pre-generation agent context), switch the post-start `sendSseEvent` calls to `trySendSseEvent` (or guard `reply.raw.write`), and `off()` the listener in `finally`. The reference implementation is the in-repo `generate.routes.ts` abort wiring. **Regression notes.** Not a regression — `main`'s `retry-agents-route.ts` has the same shape (no AbortController/close-listener/`context.signal`/`trySendSseEvent`). The retry route never had abort wiring; staging neither broke nor dropped it. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:44 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742294956 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1287
No description provided.