[GH-ISSUE #2688] [Issue]: Agent error/abort/timeout gaps — error path leaks in-flight agent calls; no per-agent timeout can hang the turn #1334

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Two agent resource-handling gaps can leak or hang the generation turn: (1) when the main generation throws after parallel agents were launched, the route never calls abortController.abort(), so detached in-flight agent LLM streams keep running and charging tokens; (2) agent provider calls have no per-call timeout, so a stalled mid-stream agent response (first byte received, then hangs) can block the whole turn indefinitely.

Expected behavior

  • When the turn tears down on error, any in-flight parallel/post agent provider calls should be cancelled via the shared abort signal.
  • A single stalled agent provider call should not be able to hang the entire turn forever; there should be an upper time bound.

Actual behavior

(1) Phase-2 parallel agents are launched fire-and-forget and awaited later. The whole turn runs inside a try; its catch sends an SSE error and the finally calls releaseActiveGeneration() and reply.raw.end() but never calls abortController.abort(). agentContext.signal is abortController.signal, so the only way to cancel in-flight agent streams is to abort that controller. The /abort route and the client-disconnect (onClose) path do call abort(); the thrown-error teardown path is the gap. Because the agent phase uses Promise.allSettled it never rejects, so this is a silent token/resource leak rather than a crash.

(2) Agent provider calls pass no per-call timeout. The shared undici agent sets a 5-minute headers timeout (time-to-first-byte) but bodyTimeout:0 (no body timeout). Once an agent stream starts, no timeout fires if it stalls mid-body; agents rely entirely on context.signal, which is only aborted on explicit /abort or active-mode disconnect.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • (1) packages/server/src/routes/generate.routes.ts — parallel agents launched fire-and-forget (:6599), awaited later (:6767); turn try at :1306; catch at ~:10328 and finally at ~:10336-10344 call releaseActiveGeneration()/reply.raw.end() but not abortController.abort(). agentContext.signal = abortController.signal (:4092). abort() is called in onClose (:1286) and the /abort route (~:10367).
  • (2) Agent chatComplete calls pass only { model, temperature, maxTokens, stream, signal } (agent-executor.ts:333-344,:467-474,:696-707); shared undici bodyTimeout:0 (base-provider.ts). A hung post-processing agent blocks the await at generate.routes.ts:~7050 and holds the generation lock; a hung parallel agent blocks the await at ~:6767.

Steps to reproduce

  • (1) Configure a parallel-phase agent on a slow connection; trigger a generation where the main model connection errors mid-turn (e.g. invalid main key, valid agent key). The route catch fires and the client gets an error, but the parallel agent keeps streaming on the agent connection until it finishes on its own.
  • (2) Point an agent at a backend that sends one token then stalls without closing. The headers timeout already passed, bodyTimeout is 0, and there's no agent-level timeout, so the agent chatComplete never resolves and the turn cannot complete.

Suggested fix

  • (1) In the turn finally (or catch), call abortController.abort() before releaseActiveGeneration() so detached parallel/post agent calls are cancelled on error — mirroring the abort() already done in onClose and /abort.
  • (2) Wrap agent provider calls in a per-call timeout (e.g. AbortSignal.timeout(...) combined with context.signal via AbortSignal.any([...])) so a single stalled agent can't stall the whole turn.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the agent subsystem. (1) is a silent token/resource leak on the error path; (2) is an unbounded-hang risk. Part of a set of agent-pipeline issues filed together.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2688 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Two agent resource-handling gaps can leak or hang the generation turn: **(1)** when the main generation throws after parallel agents were launched, the route never calls `abortController.abort()`, so detached in-flight agent LLM streams keep running and charging tokens; **(2)** agent provider calls have no per-call timeout, so a stalled mid-stream agent response (first byte received, then hangs) can block the whole turn indefinitely. ## Expected behavior - When the turn tears down on error, any in-flight parallel/post agent provider calls should be cancelled via the shared abort signal. - A single stalled agent provider call should not be able to hang the entire turn forever; there should be an upper time bound. ## Actual behavior **(1)** Phase-2 parallel agents are launched fire-and-forget and awaited later. The whole turn runs inside a `try`; its `catch` sends an SSE error and the `finally` calls `releaseActiveGeneration()` and `reply.raw.end()` but **never** calls `abortController.abort()`. `agentContext.signal` is `abortController.signal`, so the only way to cancel in-flight agent streams is to abort that controller. The `/abort` route and the client-disconnect (`onClose`) path *do* call `abort()`; the thrown-error teardown path is the gap. Because the agent phase uses `Promise.allSettled` it never rejects, so this is a silent token/resource leak rather than a crash. **(2)** Agent provider calls pass no per-call timeout. The shared undici agent sets a 5-minute *headers* timeout (time-to-first-byte) but `bodyTimeout:0` (no body timeout). Once an agent stream starts, no timeout fires if it stalls mid-body; agents rely entirely on `context.signal`, which is only aborted on explicit `/abort` or active-mode disconnect. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - (1) `packages/server/src/routes/generate.routes.ts` — parallel agents launched fire-and-forget (~`:6599`), awaited later (~`:6767`); turn `try` at `:1306`; `catch` at `~:10328` and `finally` at `~:10336-10344` call `releaseActiveGeneration()`/`reply.raw.end()` but not `abortController.abort()`. `agentContext.signal = abortController.signal` (~`:4092`). `abort()` *is* called in `onClose` (~`:1286`) and the `/abort` route (~`:10367`). - (2) Agent `chatComplete` calls pass only `{ model, temperature, maxTokens, stream, signal }` (`agent-executor.ts:333-344`,`:467-474`,`:696-707`); shared undici `bodyTimeout:0` (`base-provider.ts`). A hung post-processing agent blocks the await at `generate.routes.ts:~7050` and holds the generation lock; a hung parallel agent blocks the await at `~:6767`. ## Steps to reproduce - (1) Configure a parallel-phase agent on a slow connection; trigger a generation where the **main** model connection errors mid-turn (e.g. invalid main key, valid agent key). The route catch fires and the client gets an error, but the parallel agent keeps streaming on the agent connection until it finishes on its own. - (2) Point an agent at a backend that sends one token then stalls without closing. The headers timeout already passed, `bodyTimeout` is 0, and there's no agent-level timeout, so the agent `chatComplete` never resolves and the turn cannot complete. ## Suggested fix - (1) In the turn `finally` (or `catch`), call `abortController.abort()` before `releaseActiveGeneration()` so detached parallel/post agent calls are cancelled on error — mirroring the `abort()` already done in `onClose` and `/abort`. - (2) Wrap agent provider calls in a per-call timeout (e.g. `AbortSignal.timeout(...)` combined with `context.signal` via `AbortSignal.any([...])`) so a single stalled agent can't stall the whole turn. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the agent subsystem. (1) is a silent token/resource leak on the error path; (2) is an unbounded-hang risk. Part of a set of agent-pipeline issues filed together. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Fixed in branch codex/spicy-issue-sweep-2026-06-21.

What changed:

  • Aborts the shared generation controller from the route error path so launched agent calls do not continue after the turn has already failed.
  • Wraps every agent provider request with a fresh per-call timeout signal while preserving the existing generation/client abort signal.

Final repo checks will run after the full issue sweep.

<!-- gh-comment-id:4761500626 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed in branch `codex/spicy-issue-sweep-2026-06-21`. What changed: - Aborts the shared generation controller from the route error path so launched agent calls do not continue after the turn has already failed. - Wraps every agent provider request with a fresh per-call timeout signal while preserving the existing generation/client abort signal. Final repo checks will run after the full issue sweep.
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#1334
No description provided.