Skip to content

Studio: wait out a published image load's tail instead of refusing generate with 409 - #18626

Merged
danielhanchen merged 2 commits into
mainfrom
studio-images-status-loaded-race
Nov 20, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-images-status-loaded-race

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

/api/inference/images/status reports loaded: true as soon as an image load publishes its pipeline. The load is not finished at that point: it still waits for the text encoder host pins (finish_encoder_pins_at_load, for example "pinned 8.7 GiB of Qwen3VLForConditionalGeneration host weights in the background ... the load waited 1.2 s for the encoder pins") and trims the host heap. Until it returns, load-progress reads finalizing, so /images/generate refuses every run with HTTP 409 "An image model is loading. Generate again once the load finishes." A client that waits for loaded, as the API docs suggest, could still be refused. An outside benchmark lost 11 cells on small simulated cards to this.

The Studio frontend is not affected, because it waits for load-progress to read ready before it reads status. The video route is not affected either: its generate admits a run once the state is committed and waits for the pins inside the render (finish_background_pins).

Change

  • DiffusionBackend._publish_state_locked is now the one place that assigns a loaded pipeline. Both commit sites (the general load and the MiniMax-H3-Image load) go through it, and it marks the in-flight _LoadingState as committed.
  • New DiffusionBackend.load_tail_pending(): True when the in-flight load has published its pipeline, has no error, and is only finishing its tail.
  • /images/generate: when a load is in flight and it is only that tail, the route waits for it, polling every 0.1 s with a 300 s cap, and then runs. A load or switch that has not published yet still gets the same 409 as before, and so does a tail that outlasts the cap. Engines without load_tail_pending (sd.cpp, MLX) behave exactly as before.

Status answers are unchanged, and so is every frontend path.

Tests

tests/test_images_generate_load_tail.py, CPU only, with the backend faked:

  • test_generate_in_a_published_loads_tail_waits_and_runs: status loaded, load-progress reads finalizing for three polls and then ready. The generate runs once, after ready. On main it gets the 409.
  • test_generate_before_the_pipeline_is_published_is_still_refused and test_a_tail_that_outlasts_the_cap_is_refused: both still get the 409.
  • test_engine_reports_the_tail_only_after_publishing, test_a_synchronous_load_without_a_loading_marker_publishes, test_every_image_commit_site_publishes_through_the_helper: the engine marker.

test_differential.py (base 2c21acdc79, head 1f1d690ba1): DISCRIMINATING, head_fail=0.

Regression run (test_images_generate_during_load.py, test_diffusion_routes.py, test_image_queue_routes.py, test_diffusion_backend.py): 887 passed and 1 failed on both base and head. The failing ID is the same on both, test_diffusion_backend.py::test_mirrored_base_still_trips_the_flux2_shape_guard, so it was already failing on main.

Not proven here

No GPU run. The tail timing (about 1.2 s for 8.7 GiB of encoder pins) is taken from the outside benchmark's server log and was not re-measured.

…nerate

/images/status reads loaded as soon as the load publishes its pipeline, but the
load then still waits for the encoder host pins and trims the heap, and
/images/generate refused every run in that window with the 409 "An image model
is loading". A client that waits for loaded could still be refused.

The engine now marks the in-flight load once it has published its pipeline
(load_tail_pending). Generate waits for that tail (capped at 300 s) and runs;
a load or switch that has not published yet is still refused as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T14:36:07.950839Z c6e27ae 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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1f1d690ba1

ℹ️ 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".

Comment on lines +53875 to +53879
while await asyncio.to_thread(_image_load_in_flight):
if time.monotonic() >= deadline or not await asyncio.to_thread(_image_load_tail_only):
return False
await asyncio.sleep(_IMAGE_LOAD_TAIL_POLL_S)
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor cancellation while waiting for the load tail

When an API client submits generation during a published load's tail and then calls /images/generate/cancel, this wait has not registered a generation ticket or reached the engine, so cancel_generate() cannot cancel it. The cancel route increments _direct_stops, but the generate route captures that counter only after this wait and never checks whether a Stop occurred while waiting. Consequently, the request starts rendering after the load finishes despite the intervening cancellation, potentially consuming GPU time after a wait of up to 300 seconds. Register a cancellable pending request before waiting and check its cancellation before starting generation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in the next commit. The generate route records the Stop counter before it waits, and the wait checks it on every poll and once more after the load ends. A Stop during the wait now ends the run with the usual 409 "Diffusion generation was cancelled." instead of rendering once the load finishes. New test test_a_stop_during_the_tail_wait_cancels_the_run bumps the counter the way /images/generate/cancel does while the run waits: before this change the run rendered with 200, now it is cancelled and never reaches the engine.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c6e27aee7f

ℹ️ 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 58f8867 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-images-status-loaded-race branch November 20, 2026 15:59
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…18657)

## Summary

`Frontend unit tests` fail on `main` (seen on #18522, reproduced on
current `main`):

```
not ok - the backend refuses an image generate while a load is in flight
  location: studio/frontend/tests/images-generate-during-switch.test.ts:77
  The input did not match the regular expression /if await asyncio\.to_thread\(_image_load_in_flight\):\s*raise HTTPException\(\s*status_code = 409/
```

## Cause

#18420 added this source check: a generate that arrives while a model
load is in flight was refused straight away with 409. Then #18626
deliberately changed that branch in `routes/inference.py`:

- a generate during a load whose pipeline is already published now waits
out the load's tail (encoder pins);
- a generate that cannot wait (pipeline not published yet, or the tail
outlasts the cap) is still refused with the in-flight 409.

#18626 covered the new behaviour with backend tests
(`test_images_generate_load_tail.py`), but this frontend check still
expected the 409 on the line right after the `if`. The product change is
right; the check is out of date.

## Change

`images-generate-during-switch.test.ts` now asserts the invariant both
PRs share, that no image run starts while a load is in flight. It finds
the in-flight branch before a run is admitted and requires both:

- that it waits out the tail (`await _wait_image_load_tail(`);
- that it still raises `HTTPException(status_code = 409, detail =
IMAGE_LOAD_IN_FLIGHT_MSG)` when it cannot.

## Testing

- On `main` the file fails 1 of 7. With this change: 7 passed.
- Sabotage on `routes/inference.py`, each of which fails the new check:
  - removing the in-flight 409;
  - replacing the tail wait.
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