Skip to content

Studio: queued image generations wait on their own pool, not the default executor - #17108

Merged
danielhanchen merged 2 commits into
mainfrom
studio-image-generate-own-executor
Nov 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-image-generate-own-executor

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

An image generation request runs backend.generate through asyncio.to_thread. Inside that worker thread, the engine then waits for its one-render slot (_generation_slot, polling _generate_lock.acquire(timeout = 0.1)).

Every queued render therefore holds a worker of the default executor for the whole time it waits. That executor is the one image status, gallery, chat streaming and most other routes use through asyncio.to_thread. With as many queued renders as the executor has workers (min(32, cpus + 4)), those calls stall until the active render ends.

Changes

  • The three image generate dispatches now run on a dedicated image-generate pool (32 workers) instead of the default executor:
    • the Studio image route
    • the OpenAI-compatible batched path
    • the OpenAI-compatible single path
  • The helper copies the caller's context exactly as asyncio.to_thread does, so account scope and progress context reach the render unchanged.
  • Results and exceptions pass through as before.
  • Admission, the render slot, teardown priority and cancellation are untouched. Only which thread pool hosts the waiting thread changes.
  • Kill switch: UNSLOTH_DIFFUSION_GENERATE_EXECUTOR=0 restores asyncio.to_thread.

Evidence (CPU)

tests/test_image_generate_executor.py uses a default executor of 4 workers, 4 queued renders behind a held slot, then one default-executor read (a status call):

Dispatch status read render context
asyncio.to_thread (main, and the kill switch) did not finish within 0.5 s: every worker waits on the slot account context kept
this PR < 0.25 s account context kept, threads named image-generate

The image route suites (test_diffusion_routes, test_openai_image_generation, test_openai_images_batching, test_openai_images_generations_route, test_openai_multi_image_route, test_generate_image_tool, test_liveness_reports_inference_active, test_diffusion_detailer) give 413 passed, 8 skipped on both main and this branch.

…ult executor

A generation request runs backend.generate in a worker thread that then waits
for the engine's one-render slot. On the default executor, a few queued renders
held every worker, so image status, gallery and chat streaming calls queued
behind the active render. Run the three image generate dispatches on a dedicated
pool with the same context copy as asyncio.to_thread.
UNSLOTH_DIFFUSION_GENERATE_EXECUTOR=0 restores asyncio.to_thread.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T04:49:34.389044Z 3d11acf Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 3d11acfc23

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 8f8b34c into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-image-generate-own-executor branch November 8, 2026 20:52
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Nov 9, 2026
…#17188)

Follow-up to unslothai#17108.

## What happened

unslothai#17108 moved queued image renders off the default asyncio executor onto
their own `image-generate` pool, because a render waits for the
one-render slot inside its thread and a few queued renders could hold
every default worker, stalling status reads and everything else that
uses `asyncio.to_thread`.

The Detailer pass in the same route was left on `asyncio.to_thread`.
`_run_detailer` hands `backend.generate` to the Detailer, and each crop
inpaint is a synchronous generate call that waits on the same render
slot. So a few Detailer requests queued behind a render still filled the
default executor. A reviewer reproduced it with the route's dispatch
expression: four Detailer workers blocked on a held slot exhausted a
four-worker default executor and a status read timed out after 0.2 s,
while the same work dispatched through `_image_generate_in_thread` left
the read responsive.

## Fix

Dispatch `_run_detailer` through `_image_generate_in_thread`, like the
primary render. The caller's context is still copied (account access),
and the existing `UNSLOTH_DIFFUSION_GENERATE_EXECUTOR=0` switch restores
`asyncio.to_thread` for both.

## Tests


`studio/backend/tests/test_image_generate_executor.py::test_detailer_inpaints_wait_on_the_image_pool`
reads how the route awaits `_run_detailer`: it fails on main
(`asyncio.to_thread`) and passes here. The existing saturation, context
and kill switch tests in that file still pass (5 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.

1 participant