Skip to content

Studio /v1 images: pin the load's LoRA set so a reload cannot swap it mid-request - #18135

Merged
danielhanchen merged 2 commits into
mainfrom
studio-v1-images-load-pin
Nov 17, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-v1-images-load-pin

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

/v1/images/generations can render with a stale adapter set when the model is reloaded mid-request. With loras omitted, the route resolves the request to the load's adapters, but it pins only repo, base and family. Reloading the same model with other adapters keeps that identity, so a reload that lands between the status read and the generation slot is let through. There are two outcomes:

  • Plain load rebaked with a LoRA: the engine reads "no selection" as no LoRA, so the new bake renders at weight 0.
  • One baked LoRA swapped for another: the request gets a 400 for an adapter it never named.

#17993 fixed the same race on the native route. /v1 now passes the same expected_load_loras pin, on both single and batched calls. It does so only when loras is omitted and the engine reports its load's adapters. Batches are keyed on it.

Testing

  • New: tests/test_openai_images_load_swap.py, 6 tests in which a load thread commits a rebake between the status read and the slot. On main 4 fail and 2 pass; on this branch all 6 pass.
    • One test drives the real DiffusionBackend.generate slot check. On main it reaches the render with loras=None, which would zero the new bake.
  • Updated: the fake engine in test_openai_images_baked_loras.py takes expected_load_loras, as the real diffusers engine does.
  • Suites: /v1 and native route suites pass. Across 14 wider route files, main and this branch fail the same test IDs; they need zstd, sqlite_vec and the MiniMax download plan.

Not covered

No GPU run drove a real reload into a live /v1 request. The race is route-level, and the test exercises the real engine's slot check.

…esolved from

/v1/images/generations resolves an omitted loras to the load's adapters, then
pins only repo / base / family. A same-model reload with other adapters keeps
that identity, so a reload committing between the status read and the
generation slot was admitted with the old load's set:

- a plain load resolved to none, then rebaked with a LoRA: the engine reads
  the missing selection as none and renders the new bake with its weight at 0;
- a load baked with one LoRA, rebaked with another: the stale id is refused
  as a 400 although the request never named an adapter.

#17993 closed this on /images/generate with expected_load_loras. /v1 now
passes the same pin (only when loras is omitted and the engine reports its
load's adapters), on the single call and the batched call, and keys batches
on it, so a refused request retries once from fresh state with the new set.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T11:51:50.695724Z 8e3f108 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 8e3f108d7c

ℹ️ 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 be48889 into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-v1-images-load-pin branch November 17, 2026 12:36
danielhanchen added a commit that referenced this pull request Nov 17, 2026
…ine as benign (red since #18135) (#18145)

`tests/studio/test_backend_ci_parallel_isolation.py::test_every_tight_elapsed_bound_is_isolated`
fails on main since #18135 (workflow-trigger lint and the CPU `studio`
shard, seen on the staging run of #18139):

```
E   assert not {'test_openai_images_load_swap.py': ['test_openai_images_load_swap.py:199 one duration against another']}
```

## Cause

#18135 added `test_a_batch_waiting_through_a_reload_carries_the_pin`,
which waits for two image requests to join one batch:

```python
deadline = asyncio.get_running_loop().time() + 5
while sum(len(b.members) for b in batching._OPEN.values()) < 2:
    assert asyncio.get_running_loop().time() < deadline, "the batch never formed"
    await asyncio.sleep(0.01)
```

That is a poll deadline: descheduling only delays the poll and cannot
make the condition false. The lint flags every clock comparison in
backend tests run under `-n 4` until it is recorded, and this one was
not.

## Fix

Add `("test_openai_images_load_swap.py",
"test_a_batch_waiting_through_a_reload_carries_the_pin")` to
`BENIGN_TIMING` with a one-line reason. The lint keys on the outermost
enclosing function, so the test name covers the nested `run()`.

## Verification

- main: `test_every_tight_elapsed_bound_is_isolated` fails with the
message above.
- this branch: `tests/studio/test_backend_ci_parallel_isolation.py` 43
passed.
danielhanchen added a commit that referenced this pull request Nov 17, 2026
… with its output (rest-2 timeouts) (#18180)

`tests/test_ddp_find_unused_default.py::test_trainable_embeddings_train_under_ddp_reentrant_checkpointing`
hits pytest-timeout in the CPU `rest-2` shard on unrelated PRs (#18135,
#18165, #18171 today):

```
tests/test_ddp_find_unused_default.py:285: in test_trainable_embeddings_train_under_ddp_reentrant_checkpointing
    proc = subprocess.run(
E   Failed: Timeout (>330.0s) from pytest-timeout.
```

The test takes about 11 s locally and hung only on some workers, with no
child output in the log.

## Cause

The test copies `os.environ` into both children: the single-process
reference and the `torchrun` pair. The worker decides it is distributed
from `WORLD_SIZE`. Several tests write rank variables (`RANK`,
`WORLD_SIZE`, `MASTER_PORT`, ...) into the pytest process environment,
so on a worker that ran one of them first, the single-process reference
sees `WORLD_SIZE=2`, calls `init_process_group("gloo")` and waits for a
second rank that never comes. It never returns, and the child's `timeout
= 600` is longer than pytest-timeout's 330 s, so CI only showed a bare
timeout.

Reproduced on main by running the file with `WORLD_SIZE=2 RANK=0
MASTER_ADDR=127.0.0.1 MASTER_PORT=29555` in the environment: it hangs
until killed.

## Fix

- `_rank_env(tmp_path)` builds the child environment without `RANK`,
`WORLD_SIZE`, `LOCAL_RANK`, `LOCAL_WORLD_SIZE`, `GROUP_RANK`,
`ROLE_RANK`, `MASTER_ADDR`, `MASTER_PORT` (torchrun sets its own) and
the `UNSLOTH_DDP_FIND_UNUSED_PARAMETERS_AUTO` override the test already
dropped.
- `_run_child` runs each child under a timeout below pytest-timeout (150
s each for the two-step test, 300 s for the single torchrun in the VLM
test) and, if one still hangs, fails with the tail of its output instead
of a bare timeout.
- Both DDP tests in the file use them.

## Verification

- `tests/test_ddp_find_unused_default.py`: 63 passed.
- Same file with `WORLD_SIZE=2 RANK=0 MASTER_ADDR=127.0.0.1
MASTER_PORT=29555` exported: 63 passed (main: hangs).
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