Skip to content

scheduler: never evict a tool that is loading, committing or running a job; refuse fast with 503 insufficient_memory - #159

Open
smfworks wants to merge 14 commits into
jayleaton:mainfrom
smfworks:upstream/scheduler-protect-loading
Open

smfworks wants to merge 14 commits into
jayleaton:mainfrom
smfworks:upstream/scheduler-protect-loading

Conversation

@smfworks

Copy link
Copy Markdown
Contributor

Stacked on #156 (ComfyUI worker). This branch is amd-split/4-comfyui-worker plus one commit of its own; until #156 merges, the diff here includes it. Review the last commit, "scheduler: do not evict a tool that is loading or committing". It builds on #156's job error (type and code) and its "a tool running a job is never a victim" test. It does not depend on #154, #155 or #157.

A chat request could unload a model that was still loading, or still committing the memory it had been admitted for (MiniMax H3 at priority 10, unloaded for Qwen3.8-Flash-Next chat). The load was lost, and the chat then sat there until the client timed out.

  • A tool that is loading, committing, or running a job is never chosen as a victim, even for a job that is only queued. Priority still only orders the victims. A request that would fit after unloading idle tools of any priority still gets in.
  • A request that cannot fit while a non-evictable job holds the memory is refused at once: 503 with type and code both insufficient_memory, plus retry_after in seconds, also sent as a Retry-After header. The same fields appear on the failed job object (/v1/jobs, /v1/videos, /v1/assets3d).
  • retry_after comes from the new config key retry_after_s (30 when unset or 0). If the blocking tool's last load took longer than that, the hint is raised to that load's time, capped at 600 s.
  • A request that will fit once a load's temporary overshoot settles (the admitted estimate is above what the load will hold) waits for that load and does not unload it.
  • HTTP, MCP, chat, responses, speech and decide callers all pass Retry-After through. docs/AGENTS.md and ARCHITECTURE.md describe the behavior.

Checks run locally (Zig 0.17.0, x86_64 Linux, no GPU):

  • zig build
  • zig build test --summary all: 594/597 passed, 3 skipped. The base branch gives 591/594, so this adds 3 scheduler tests:
    • loading/committing is never a victim, and a request that can't fit beside it is refused at once with the retry_after hint
    • a request that fits by unloading an idle model still does, and doesn't unload a loading tool
    • a request that fits once the overshoot settles waits instead of evicting
  • zig fmt --check on the touched Zig files
  • git diff --check

Hardware: deployed on AMD Strix Halo (gfx1151), 128 GB unified memory, alongside MiniMax H3 and Qwen3.8-Flash-Next.

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.
…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.
A chat request could unload a model that was still loading or committing
memory for a job (minimax-h3, priority 10, for qwen3.8-flash-next). A tool
in that state, or running a job, is not a victim. A request that fits by
unloading an idle model still does. One that cannot fit while a
non-evictable job holds the memory is refused at once with 503
insufficient_memory (type and code) and retry_after, also sent as
Retry-After, instead of sitting until the client times out. One that will
fit once a load's temporary overshoot settles waits for that load.

retry_after_s in the config (30 when unset) is the hint; a blocker whose
last load took longer raises it.

@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. One blocker:

Blocking: src/sched/scheduler.zig:1273. The eviction precheck can't recover when current usage is over budget. To reproduce, set a 100 MiB budget and load a worker estimated at 60 MiB that actually reports 120 MiB. A following 10 MiB load then returns 503, even though the first worker is idle and could be evicted. On #156 (the parent), the same scenario replaces it fine. Fix: work out the memory after subtracting the victims from current usage, counting reclaimable host memory, instead of only clamping the new request's size. Please add this as a regression test.

…age recovers

roomIfEvicted took the victims' resident bytes off the request
(fits(extra -| freeable)). That clamps at 0 and still asks the current
usage to fit, so once a worker reported more than its estimate and usage
went over the budget, every later load was refused with 503 though the
worker was idle and could be evicted. The check now takes the victims'
bytes off what is held and adds them back to the host's available memory
(fitsFreeing), the state the eviction loop would reach.

Regression test: a 100 MiB budget, a worker estimated at 60 MiB that loads
at 120 MiB, then a 10 MiB load evicts it and loads.
ci.yml: main's openai_audio step, then this branch's ComfyUI worker step.
@smfworks

Copy link
Copy Markdown
Contributor Author

Fixed in 7839dda: the room check now takes the evicted tools' bytes off what is held and gives them back to host available, so a tool that loaded over its estimate no longer blocks every later load. Regression test included (fails without the fix).

@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.

Round 2: the original repro passes now. With the 100 MiB budget, the 10 MiB load returns 200 and evicts the idle worker that reports 120 MiB. One regression:

Blocking: src/sched/scheduler.zig:1424. With a 20 MiB reserve and no MemAvailable reading, a 10 MiB load returns 503 and also evicts a healthy 60 MiB worker. Before this change it returned 503 and kept the worker loaded. Fix: when the host memory reading is unavailable, refuse admission before counting reclaimable bytes. Please add a test for that case.

…s' bytes

fitsFreeing added the victims' resident bytes to MemAvailable, read as 0
when unreadable. With a 20 MiB reserve and no reading, an idle 60 MiB
worker alone cleared the reserve, so the room check passed, the eviction
loop unloaded the worker, and the 10 MiB load was still refused with 503.
Before the over-budget fix the same load was refused and the worker stayed.

When the reading is unavailable and the check would count freed bytes, it
now refuses first. With nothing freed the check is unchanged.

memory.test_available lets a test stand in for the host's reading.
Regression test: "no MemAvailable reading: a load is refused and no
healthy worker is evicted for it" (fails without the fix).
smfworks added a commit to smfworks/localrouter that referenced this pull request Nov 19, 2026
@smfworks

Copy link
Copy Markdown
Contributor Author

Round 2 fix in 48b4e34. When there's no MemAvailable reading, the room check now refuses before it counts the victims' bytes, so the 10 MiB load gets a 503 and the 60 MiB worker stays loaded. With nothing to free, the check works as before. New test: "no MemAvailable reading: a load is refused and no healthy worker is evicted for it" (fails without the fix). I added memory.test_available so a test can stand in for the host reading. The over-budget repro from round 1 still passes.

@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.

Round 3: the missing-MemAvailable fix is verified. The 10 MiB load returns 503 and the healthy 60 MiB worker stays ready. One blocker:

Blocking: src/api/routes.zig:773 and src/sched/scheduler.zig:2317. #156 (now on main) overlaps this PR's job error response, scheduler tests and API docs. Please rebase onto main, keeping #156's typed errors and regression tests alongside this PR's retry hints and admission changes.

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