[GH-ISSUE #5097] Media generation has no global concurrency cap — batches can stampede local and paid providers #2820

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

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

Originally assigned to: @kolacheee on GitHub.

What happens

The media-generation queue serialises per connection, and only for callers that opt in: the very first branch of runMediaGenerationRequest is if (!args.queue) { … return args.task(); } (packages/server/src/services/image/image-generation-queue.ts:43). A batch that fans out across connections — or any caller that doesn't pass queue: true — has no ceiling at all.

Generating a batch of images (a zone's worth of tiles and sprites, a storyboard fan-out, a gallery bulk action) can therefore stampede a local ComfyUI/SwarmUI instance (GPU OOM) or fan out unbounded concurrent requests against a paid provider.

Try it yourself

Point image generation at a local ComfyUI and trigger several generations quickly across different surfaces: nothing above the per-connection FIFO (and only for opted-in callers) limits how many requests are in flight at once.

Suggested fix

A process-global concurrency semaphore applied in addition to the existing per-connection FIFO, with a conservative configurable default, admitting non-queued callers too (that is the point — but it is a behavior change for them and should be called out). Two invariants from the existing code must hold: acquire the global permit only AFTER waiting for the per-connection turn (never before, or the two queues can deadlock), and release in finally on every path including abort — a leaked permit hangs all generation app-wide with no error, which is the worst available failure mode. Deserves an abort-during-wait and task-throws regression plus a manual soak (storyboard + gallery batch concurrently).

From the pixel-RPG exploration roadmap (PR-D3); pre-existing exposure that batch tile generation would make easy to hit. Verified against current staging.

Originally created by @kolacheee on GitHub (Aug 15, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5097 Originally assigned to: @kolacheee on GitHub. ### What happens The media-generation queue serialises per connection, and only for callers that opt in: the very first branch of `runMediaGenerationRequest` is `if (!args.queue) { … return args.task(); }` (`packages/server/src/services/image/image-generation-queue.ts:43`). A batch that fans out across connections — or any caller that doesn't pass `queue: true` — has no ceiling at all. Generating a batch of images (a zone's worth of tiles and sprites, a storyboard fan-out, a gallery bulk action) can therefore stampede a local ComfyUI/SwarmUI instance (GPU OOM) or fan out unbounded concurrent requests against a paid provider. ### Try it yourself Point image generation at a local ComfyUI and trigger several generations quickly across different surfaces: nothing above the per-connection FIFO (and only for opted-in callers) limits how many requests are in flight at once. ### Suggested fix A process-global concurrency semaphore applied *in addition* to the existing per-connection FIFO, with a conservative configurable default, admitting non-queued callers too (that is the point — but it is a behavior change for them and should be called out). Two invariants from the existing code must hold: acquire the global permit only AFTER waiting for the per-connection turn (never before, or the two queues can deadlock), and release in `finally` on every path including abort — a leaked permit hangs all generation app-wide with no error, which is the worst available failure mode. Deserves an abort-during-wait and task-throws regression plus a manual soak (storyboard + gallery batch concurrently). From the pixel-RPG exploration roadmap (PR-D3); pre-existing exposure that batch tile generation would make easy to hit. Verified against current `staging`.
bsakaguchi 2026-10-21 05:56:13 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 15, 2026):

Taking this one (agent working from kolacheee's fork) — draft PR is up. Worth reading the PR body's review section: the pre-submission adversarial pass found that the naive semaphore would DEADLOCK on the video-fallback path (it re-enters the queue while holding a permit), so the design gained an AsyncLocalStorage re-entrancy guard — which in turn made it safe to enforce the cap inside generateImage itself, closing the gap where game assets/sprites/avatars/storyboards were never routed through the queue at all.

<!-- gh-comment-id:5304424629 --> @kolacheee commented on GitHub (Aug 15, 2026): Taking this one (agent working from kolacheee's fork) — draft PR is up. Worth reading the PR body's review section: the pre-submission adversarial pass found that the naive semaphore would DEADLOCK on the video-fallback path (it re-enters the queue while holding a permit), so the design gained an AsyncLocalStorage re-entrancy guard — which in turn made it safe to enforce the cap inside generateImage itself, closing the gap where game assets/sprites/avatars/storyboards were never routed through the queue at all.
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#2820
No description provided.