Skip to content

scheduler: retry_after estimates when the blocking tool will free its memory - #163

Draft
smfworks wants to merge 17 commits into
jayleaton:mainfrom
smfworks:upstream/retry-after-estimate
Draft

smfworks wants to merge 17 commits into
jayleaton:mainfrom
smfworks:upstream/retry-after-estimate

Conversation

@smfworks

Copy link
Copy Markdown
Contributor

Stacked on #159 (it changes the retry_after that #159 adds). Draft: review #159 first.

Problem

A 503 insufficient_memory tells the client to retry after retry_after_s (30 s), or after the blocker's last load time if that is longer. A video render keeps its memory for its whole job: a 10 s clip at 1024x576 took 16 min on a Strix Halo workstation. An agent waiting on its chat model was told "30 s" each time, so it asked about thirty times and could run out of retries before the render ended.

Change

  • Each slot records last_job_ms (its last successful generate) and job_started_ns (its running generate).
  • retryAfter estimates when each tool that cannot be evicted will be free:
    • running a job: its last job time less the time this job has run;
    • loading or committing: the rest of its last load (from admission), then its last job time.
  • It uses the longest of those, at least the config's retry_after_s, at most 600 s. With no history it uses the floor, so there is no change before the first job finishes.
  • The wait for a load's overshoot to settle (admission's .wait) still counts only the load (settleWait), as before.
  • docs/AGENTS.md covers the estimate and that it is a hint, not a promise.

Tests

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.
…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.
The 503 insufficient_memory hint was the configured retry_after_s (30 s)
or a blocker's last load time, whichever was longer. That can be far too
short for a video render (10 s at 1024x576 took 16 min), so an agent asks
again dozens of times and its retries can run out before the render ends.

Each tool records its last successful generate time. The hint estimates
when it will be free: that time less the time its current job has run; if
loading, the rest of its last load then the job. The longest of those, at
least the config's retry_after_s and at most 600 s. Without history it
uses the floor. The wait for a load's overshoot still counts the load
only. A hint, not a promise: a longer request or another job can keep the
memory busy.
ci.yml: main's openai_audio step, then this branch's ComfyUI worker step.
…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).
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