[GH-ISSUE #2740] [Issue]: Client disconnect not propagated to upstream LLM in game-mode & embedding paths — zombie generations burn tokens until internal timeout #1380

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The game-mode text-generation routes are buffered JSON endpoints whose options never carry a disconnect-aware AbortSignal, so runGameChatComplete can only abort on its internal GAME_GENERATION_TIMEOUT_MS (4 min; up to ~8 min for scene-wrap's streamed retry). The codebase already has the correct helper — createResponseAbortSignal(reply, timeoutMs, label) wires reply.raw.on('close') -> controller.abort — but it is used by exactly one site (/generate-assets). Separately, BaseLLMProvider.embed() has no signal parameter, so a Stop/disconnect during pre-generation memory-recall embedding cannot cancel the in-flight embeddings request (up to a 5-min embedding timeout). All of these are token/cost/connection leaks: the user navigates away and the expensive upstream call keeps running.

Sub-bug 1: /game/setup world-gen is a zombie on disconnect (HIGH, leak)

  • How it breaks: /setup builds setupOptions = gameGenOptions(...) with no signal (gameGenOptions only forwards overrides.signal, never passed), so inside runGameChatComplete parentSignal = options.signal is undefined and only the 4-min timer can abort. No reply.raw.on('close') listener exists in the handler. With bodyTimeout:0 on the agent, transport won't cancel either. On bad-JSON retry the run repeats for another 4 min.
  • Repro: Start game setup, close the tab during world generation; the upstream LLM keeps billing for up to 4 min (8 on retry).
  • Trace: game.routes.ts:3627 (handler), 3841-3862 (setupOptions, no signal), 3879 (call), 1738 (parentSignal undefined), 1748 (timeout), 1802-1830 (unused createResponseAbortSignal), 7812 (only usage).

Sub-bug 2: Other game LLM routes share the gap (HIGH, leak)

  • How it breaks: Same pattern at conclude, scene-wrap, party-turn, map/generate, campaign-progression, regenerate-conclusion — every runGameChatComplete/runGameChatStream call site builds options without a reply-bound signal and registers no close listener. The provider layer honors a signal end-to-end, so wiring one would propagate the abort.
  • Repro: Disconnect during any game text generation; upstream runs to completion/timeout.
  • Trace: call sites 4287/4491/5014/5290/5608/5970/6726/7054; 7045-7053/7065 (scene-wrap has a second full streamed run on empty content).

Sub-bug 3: embed() cannot be aborted (LOW, leak)

  • How it breaks: embed(texts, model) has no signal; the only abort wired into its fetch is AbortSignal.timeout(timeoutMs). The MemoryRecallEmbeddingSource.embed interface and all call sites omit a signal, so the generation's abortController.signal never reaches the embeddings request. On Stop/disconnect the embedding request runs until its own (default 5-min) timeout.
  • Repro: Stop during pre-generation memory recall against a slow embedding endpoint; the embeddings socket lingers.
  • Trace: base-provider.ts:634 (signature), 648 (only timeout signal); interface memory-recall.ts:64; calls memory-recall-embedding.ts:83/127, memory-recall.ts:77.

Suggested Fix

  • In each game LLM route, create const signal = createResponseAbortSignal(reply, GAME_GENERATION_TIMEOUT_MS, label) and thread it into the options ({ ...overrides, signal }) so runGameChatComplete's parentSignal aborts on reply.raw 'close' (this also folds in the existing timeout, matching /generate-assets).
  • Add an optional signal?: AbortSignal to embed() and to MemoryRecallEmbeddingSource.embed, combine it with the timeout via AbortSignal.any([AbortSignal.timeout(timeoutMs), callerSignal]), and thread the generation's abortController.signal from the call sites.

Root-cause throughline

Disconnect propagation was implemented once (createResponseAbortSignal for /generate-assets) but never applied to the buffered game text routes or the embedding path, so the generation's abort signal stops at the route boundary and every abandoned setup/conclude/scene/recall keeps an upstream LLM or embeddings request running to its internal timeout.

Verification checklist (leave all UNCHECKED)

  • Manually verify: closing the tab during /game/setup aborts the upstream LLM request promptly (check provider logs / network)
  • Manually verify: disconnect during scene-wrap/conclude/party-turn cancels the upstream call
  • Manually verify: Stop during memory-recall embedding cancels the embeddings request
  • Confirm the folded-in timeout still fires for genuinely hung upstreams
  • Run pnpm check

Affected files

  • packages/server/src/routes/game.routes.ts
  • packages/server/src/services/llm/base-provider.ts
  • packages/server/src/services/conversation/memory-recall.ts
  • packages/server/src/services/conversation/memory-recall-embedding.ts

Audit provenance

Found by a multi-agent static audit of the streaming/SSE subsystem (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged staging tree. Severity: HIGH.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2740 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The game-mode text-generation routes are buffered JSON endpoints whose options never carry a disconnect-aware `AbortSignal`, so `runGameChatComplete` can only abort on its internal `GAME_GENERATION_TIMEOUT_MS` (4 min; up to ~8 min for scene-wrap's streamed retry). The codebase already has the correct helper — `createResponseAbortSignal(reply, timeoutMs, label)` wires `reply.raw.on('close')` -> `controller.abort` — but it is used by exactly one site (`/generate-assets`). Separately, `BaseLLMProvider.embed()` has no `signal` parameter, so a Stop/disconnect during pre-generation memory-recall embedding cannot cancel the in-flight embeddings request (up to a 5-min embedding timeout). All of these are token/cost/connection leaks: the user navigates away and the expensive upstream call keeps running. ### Sub-bug 1: /game/setup world-gen is a zombie on disconnect (HIGH, leak) - **How it breaks:** `/setup` builds `setupOptions = gameGenOptions(...)` with no `signal` (`gameGenOptions` only forwards `overrides.signal`, never passed), so inside `runGameChatComplete` `parentSignal = options.signal` is undefined and only the 4-min timer can abort. No `reply.raw.on('close')` listener exists in the handler. With `bodyTimeout:0` on the agent, transport won't cancel either. On bad-JSON retry the run repeats for another 4 min. - **Repro:** Start game setup, close the tab during world generation; the upstream LLM keeps billing for up to 4 min (8 on retry). - **Trace:** `game.routes.ts:3627` (handler), `3841-3862` (setupOptions, no signal), `3879` (call), `1738` (`parentSignal` undefined), `1748` (timeout), `1802-1830` (unused `createResponseAbortSignal`), `7812` (only usage). ### Sub-bug 2: Other game LLM routes share the gap (HIGH, leak) - **How it breaks:** Same pattern at conclude, scene-wrap, party-turn, map/generate, campaign-progression, regenerate-conclusion — every `runGameChatComplete`/`runGameChatStream` call site builds options without a reply-bound signal and registers no close listener. The provider layer honors a signal end-to-end, so wiring one would propagate the abort. - **Repro:** Disconnect during any game text generation; upstream runs to completion/timeout. - **Trace:** call sites `4287/4491/5014/5290/5608/5970/6726/7054`; `7045-7053`/`7065` (scene-wrap has a second full streamed run on empty content). ### Sub-bug 3: embed() cannot be aborted (LOW, leak) - **How it breaks:** `embed(texts, model)` has no `signal`; the only abort wired into its fetch is `AbortSignal.timeout(timeoutMs)`. The `MemoryRecallEmbeddingSource.embed` interface and all call sites omit a signal, so the generation's `abortController.signal` never reaches the embeddings request. On Stop/disconnect the embedding request runs until its own (default 5-min) timeout. - **Repro:** Stop during pre-generation memory recall against a slow embedding endpoint; the embeddings socket lingers. - **Trace:** `base-provider.ts:634` (signature), `648` (only timeout signal); interface `memory-recall.ts:64`; calls `memory-recall-embedding.ts:83/127`, `memory-recall.ts:77`. ## Suggested Fix - In each game LLM route, create `const signal = createResponseAbortSignal(reply, GAME_GENERATION_TIMEOUT_MS, label)` and thread it into the options (`{ ...overrides, signal }`) so `runGameChatComplete`'s `parentSignal` aborts on `reply.raw` 'close' (this also folds in the existing timeout, matching `/generate-assets`). - Add an optional `signal?: AbortSignal` to `embed()` and to `MemoryRecallEmbeddingSource.embed`, combine it with the timeout via `AbortSignal.any([AbortSignal.timeout(timeoutMs), callerSignal])`, and thread the generation's `abortController.signal` from the call sites. ## Root-cause throughline Disconnect propagation was implemented once (`createResponseAbortSignal` for `/generate-assets`) but never applied to the buffered game text routes or the embedding path, so the generation's abort signal stops at the route boundary and every abandoned setup/conclude/scene/recall keeps an upstream LLM or embeddings request running to its internal timeout. ## Verification checklist (leave all UNCHECKED) - [ ] Manually verify: closing the tab during /game/setup aborts the upstream LLM request promptly (check provider logs / network) - [ ] Manually verify: disconnect during scene-wrap/conclude/party-turn cancels the upstream call - [ ] Manually verify: Stop during memory-recall embedding cancels the embeddings request - [ ] Confirm the folded-in timeout still fires for genuinely hung upstreams - [ ] Run `pnpm check` --- ### Affected files - `packages/server/src/routes/game.routes.ts` - `packages/server/src/services/llm/base-provider.ts` - `packages/server/src/services/conversation/memory-recall.ts` - `packages/server/src/services/conversation/memory-recall-embedding.ts` ### Audit provenance Found by a multi-agent static audit of the streaming/SSE subsystem (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged `staging` tree. Severity: **HIGH**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

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

Addressed on PR #2747. Game text-generation routes now thread a reply-bound abort signal into setup, session recap/conclusion/regeneration/progression, party recruit, map generation, party turn, scene-wrap (including streamed retry), and direct lorebook-keeper regeneration. Memory-recall and semantic lorebook embedding calls now accept an optional AbortSignal and generation-time calls pass the active generation abort controller through to configured/local embedding providers. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.

<!-- gh-comment-id:4763216968 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on PR #2747. Game text-generation routes now thread a reply-bound abort signal into setup, session recap/conclusion/regeneration/progression, party recruit, map generation, party turn, scene-wrap (including streamed retry), and direct lorebook-keeper regeneration. Memory-recall and semantic lorebook embedding calls now accept an optional AbortSignal and generation-time calls pass the active generation abort controller through to configured/local embedding providers. Deferred full pnpm validation until the end of the issue sweep, per maintainer request.
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#1380
No description provided.