Skip to content

comfyui-media: a dropped request interrupts its own GPU job - #63

Merged
data-angel merged 1 commit into
mainfrom
bridge-cancel-on-disconnect
Oct 15, 2026
Merged

data-angel merged 1 commit into
mainfrom
bridge-cancel-on-disconnect

Conversation

@data-angel

Copy link
Copy Markdown
Contributor

Why

Starlette does not cancel a plain FastAPI handler when its client disconnects. When a client gave up on a video request (the gateway aborts its upstream fetch), the bridge kept generating until the clip was done, then threw it away. Meanwhile, every new request got 429 busy.

The Media Studio Director now starts the next shot's render while it reviews the previous take, and cancels it when that review fails. Without this change, each of those cancellations wastes a full render of GPU time (~140 s for an 8 s H3 Turbo shot).

What

  • server.py: the video route runs the generation under _until_disconnect. It polls request.is_disconnected() once a second and cancels the generation when the client hangs up. The response is a 499 that nobody reads.
  • jobs.py: when a cancelled generation has already reached Comfy, it now schedules _interrupt_and_reap. That sends the existing targeted cancel_own_job (pending delete plus /interrupt for our own prompt id), then runs the ordinary reaper. The slot stays busy, not quarantined, until history proves the job ended. The no-second-GPU-job guarantee is unchanged.
  • The comfyui-media image pin is updated in all 16 ComfyUI recipes for the new bridge source.

Tests

  • scripts/test-comfyui-media.py (pinned requirements-test venv): 312 passed. New tests:
    • test_cancelled_request_interrupts_its_job
    • test_client_disconnect_cancels_the_generation (raw ASGI http.disconnect)
    • test_cancelled_request_keeps_slot_until_terminal now models an interrupt that lands at the next sampler step.
  • node test/comfyui-media.test.mjs passes.

Not yet deployed. ennspark03 needs the comfyui-media image rebuilt and the H3 runtime recreated.

🤖 Generated with Claude Code

Starlette does not cancel a plain handler when its client disconnects, so a
video request the client gave up on kept the GPU busy until the clip was done
and was then thrown away. The Media Studio Director now cancels a speculative
next-shot render when the shot before it fails review; without this it would
cost a full render of dead GPU time each time.

- The video route watches for http.disconnect and cancels the generation.
- A cancelled generation that reached Comfy now sends the targeted queue
  delete and /interrupt for its own prompt id, then holds the slot (busy, not
  quarantined) until history proves the job ended, as before.
- comfyui-media image pin updated for the new bridge source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@data-angel
data-angel merged commit 1a54886 into main Oct 15, 2026
2 checks passed
@data-angel
data-angel deleted the bridge-cancel-on-disconnect branch October 15, 2026 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant