Skip to content

comfyui: a cmd worker for image and video tools, and engine size options - #156

Merged
jayleaton merged 10 commits into
jayleaton:mainfrom
smfworks:amd-split/4-comfyui-worker
Nov 19, 2026
Merged

jayleaton merged 10 commits into
jayleaton:mainfrom
smfworks:amd-split/4-comfyui-worker

Conversation

@smfworks

@smfworks smfworks commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part 4 of 5 (split from #152).

Independent of Parts 1–3. A generic cmd worker that serves image and video tools through a ComfyUI server: text to image, edits with input images and an optional mask, image to video, and progress over /ws. Also the narrow Zig size-options change (engine defaults keep precedence; the fallback has a unit test).

  • The /ws client moved to tools/comfyui/comfy_events.py, so worker.py (596 lines) stays under the 600-line convention.
  • CI runs tools/comfyui/test_worker.py (29 checks against a fake ComfyUI).

Checks run locally: zig build test, zig build test-unit (26/26), comfy 29/29, syntax, git diff --check.

tools/comfyui/worker.py starts its own ComfyUI (options.comfy_dir/python/args/env,
loopback, a private output directory) or uses options.url, fills an API-format
workflow from the request through explicit bindings (node.input), queues it,
reports progress from /ws, waits on /history and fetches 0.png.. or video.mp4
through /view. Unbound fields (negative prompt, steps, guidance), edits and
image-to-video are refused, never ignored; sizes, frame grids (frame_multiple /
frame_offset) and fps are checked; audio off removes a bound input. Cancel is
SIGUSR1 (/queue delete + /interrupt), and the worker stays loaded. Standard
library only; test_worker.py runs it against a fake ComfyUI (16 checks).

engine.fill: a video tool's options may name default_width/default_height for
requests without a size (a cmd model's own size); else 1344x768 as before.
options.modes.image_edit / image_to_video give requests with input images
their own workflow and bindings, merged over the tool's. references, mask
and first_frame are uploaded to ComfyUI (/upload/image, into the worker's
private input directory, cleaned per job) and wired in through added
LoadImage nodes (IMAGE, or MASK for a mask). Requests a tool cannot honour
are still refused. Tests: 24 checks against a fake ComfyUI.
image_edit may set defaults and an unmasked overlay (workflow, bindings,
defaults) used when a request has input images but no mask; request fields
still override. Masked edits keep the tool's fast defaults. Tests cover the
auto-select and the overrides.
worker.py had grown to 662 lines with edits and image to video, past the
600-line convention; the RFC 6455 client moves to comfy_events.py (76 lines) and
worker.py is 596. No behaviour change: the 29 worker checks pass.

@jayleaton jayleaton left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explicit bindings, the refusals for unsupported options and the fake-ComfyUI coverage. The 29 worker checks pass, and the Zig size options keep the existing defaults.

Blocking: tools/comfyui/worker.py:224. ComfyUI starts in its own session, outside the worker group that the daemon SIGKILLs on abort or worker timeout. It can therefore outlive an unloaded worker and keep holding GPU memory. Please make a hard worker kill take ComfyUI down too, and add a fake-server test for that path. The graceful-exit and SIGUSR1 tests don't cover it. This is the same issue as #154.

…ts code

A job, video or 3D asset object's error now has type (the value an HTTP error
for the same failure carries, e.g. insufficient_memory), code (the same value,
as before) and message. A client matching the HTTP type in a job's message
missed jobs refused for room (the message reads "insufficient memory: ...")
and gave up instead of retrying. docs/AGENTS.md: match type or code, never
the message. Test: the error object's JSON.
A video job holds its model while it runs; a chat request (or an API load)
that needs the room is refused at once with 503 insufficient_memory, and loads
once the job is done. The scheduler already does this (in_use); the test pins
it, and fails if admit may take a slot in use.
ComfyUI no longer gets a session of its own (start_new_session): it stays in
the worker's process group, so the daemon's SIGKILL of that group on abort or
timeout takes it down too, and PR_SET_PDEATHSIG (SIGTERM) covers the worker
dying alone. Before, a hard kill left ComfyUI holding the GPU. An unload now
signals ComfyUI and every process below it instead of its group; SIGUSR1
(cancel) still goes to the worker alone.

Test: SIGKILL of the worker's group mid-render, the fake ComfyUI is gone
(fails on the old worker). The worker tests start the worker in its own group,
as the daemon does.
@smfworks

Copy link
Copy Markdown
Contributor Author

Review fixes pushed (new commits, no force-push):

  • comfyui worker.py:224 detached session (blocking): fixed in 4009078. ComfyUI is no longer started in its own session. It stays in the worker's group, has a PR_SET_PDEATHSIG preexec, and stop() signals the process tree. New test: SIGKILL the group mid-render, then assert the fake ComfyUI is gone. The old worker fails this test. Also checked on Strix Halo hardware: after a real group SIGKILL mid-render, ComfyUI exited within about 1 s and its GPU memory was released.
  • Also in this PR:
    • 6cd7bd6: a failed job's error now carries the same type as HTTP errors, as well as code (insufficient_memory). AGENTS.md says to match those fields, never the message.
    • cb3ab70: a scheduler test that a tool running a job is never chosen as an eviction victim.

— SMF Works

@jayleaton jayleaton left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Randy's round-2 review found nothing left to fix. ComfyUI dies with the worker in both kill cases, the job-error type field is additive, and the scheduler test passes. Thanks!

@jayleaton

Copy link
Copy Markdown
Owner

Approved: Randy's round-2 review found nothing left to fix. I merged #154 first (squashed as f902361), and now GitHub shows this PR as conflicting with main. Could you merge main in again, with no force-push needed? I'll merge it as soon as it's green. #159 and #160 build on this branch, so they'll need the same.

ci.yml: main's openai_audio step, then this branch's ComfyUI worker step.

@jayleaton jayleaton left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after the main merge. The PR diff against main is identical to the head Randy cleared (90e97f8), so the only new commit is the #154 merge. Thanks!

@jayleaton
jayleaton merged commit a3c28f6 into jayleaton:main Nov 19, 2026
4 checks passed
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.

2 participants