Skip to content

Studio: run the LoRA scan off the event loop - #8392

Merged
danielhanchen merged 4 commits into
mainfrom
studio-scan-loras-off-the-event-loop
Aug 14, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-scan-loras-off-the-event-loop

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

GET /api/models/loras walks the outputs and exports directories and reads a config per checkpoint, and it did that directly in the coroutine. For the whole duration the server cannot serve anything else, streamed chat tokens included. The model picker polls this route, so the stall repeats.

The cost

Measured on this box, purely the blocking part of the scan:

checkpoints event loop blocked
50 4.0 ms
200 15.5 ms
500 37.8 ms

Roughly 0.076 ms per checkpoint, so it is the users with the most trained models who pay the most, and they are the ones most likely to have the picker open.

The change

The filesystem half moves into _scan_loras_sync and the route awaits it with asyncio.to_thread, which is the pattern already used in routes/llama.py and routes/whisper.py. No behaviour change: same rows, same order, same error handling.

Test

The test drives the route with a deliberately slow scan while a heartbeat coroutine ticks every 10 ms, then asserts the longest gap between ticks stays well under the scan duration. It fails on the unfixed route, where the heartbeat gets no turn at all until the scan returns, rather than passing for the wrong reason.

Backend: 422 passed across the LoRA, model route and scan suites.

Note

PR #7984 adds a per-checkpoint audio probe to this same route, which makes the blocking materially worse, and it currently carries this same fix. If this lands first that branch should drop its copy on the next merge from main.

danielhanchen and others added 2 commits August 11, 2026 06:28
GET /api/models/loras walks the outputs and exports directories and reads a config
per checkpoint, and it did that directly in the coroutine. For the whole duration
the server could not serve anything else, streamed chat tokens included, and the
model picker polls this route so the stall repeated.

Measured on this box, purely the blocking part:

| checkpoints | event loop blocked |
|---|---|
| 50 | 4.0 ms |
| 200 | 15.5 ms |
| 500 | 37.8 ms |

Moved the filesystem half into _scan_loras_sync and awaited it with asyncio.to_thread,
which is the pattern already used in routes/llama.py and routes/whisper.py. No
behaviour change: same rows, same order, same errors.

The test drives the route with a deliberately slow scan while a heartbeat coroutine
ticks, and asserts the longest gap between ticks stays well under the scan. It fails
on the unfixed route, where the heartbeat gets no turn at all until the scan returns.

@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: 549aaaea83

ℹ️ 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 +53 to +54
stop = True
beat.cancel()

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 Record a heartbeat after the scan finishes

If the route regresses to calling _scan_loras_sync directly on the event loop, this coroutine resumes immediately after the blocking call and stops/cancels the heartbeat before it can record a post-scan timestamp. Consequently, longest_gap contains only the normal pre-scan intervals and the test still passes despite a 300 ms stall; allow the heartbeat another turn after scan_loras returns before stopping it.

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.

Right, and I confirmed it: with the route reverted to the direct call, the old longest_gap form still passed (longest_gap 0.0102s against a 0.15s threshold) despite the full 300ms stall, because the last tick lands before the blocking call and stop/cancel runs before the heartbeat is scheduled again. The test at head no longer measures a gap at all: it counts the ticks recorded strictly between the call and its return. Blocking route: heartbeat ran 0 times during a 0.3s scan -> fail. to_thread route: pass.

One conflict, studio/backend/routes/models.py, in four hunks that are all the
same disagreement.

This branch wanted GET /api/models/loras off the event loop: lift the directory
walk into _scan_loras_sync and await it through asyncio.to_thread, so a poll of
the LoRA page stops stalling every other request including streamed tokens.

main wants the same and already has it, via #7984, with the same function name
and the same to_thread call. main's version additionally threads hf_token
through to _scan_loras_sync and fills LoRAInfo.audio_type per row from
_audio_type_of_checkpoint, which is what the audio page filters on.

So the resolution is main's side of models.py whole. A blend is not available
here: this branch's two-argument _scan_loras_sync and its LoRAInfo(...) without
audio_type are simply the same function before the audio probe was added, and
taking either would drop the modality off every scan row.

That leaves tests/test_scan_loras_off_event_loop.py as the only thing this
branch still carries, and main has no coverage for the threading at all. Two
things had to change for it to be worth keeping:

  - It was written with @pytest.mark.asyncio. pytest-asyncio is not a dependency
    of this backend and is in none of its requirements files; every other async
    test here drives its coroutine with asyncio.run. As written it errored with
    "async functions are not natively supported" plus an unknown-mark warning.

  - The assertion did not test anything. It measured the largest gap between
    heartbeat ticks, but the last tick lands before the blocking call and the
    stop flag is set the instant the call returns, so the heartbeat is never
    scheduled again and the stall leaves no gap in the list. Restoring the
    pre-fix synchronous call and rerunning it passed. It now counts the ticks
    recorded strictly between the call and its return: about 30 on the merged
    code, exactly 0 on the pre-fix form, asserted against a floor of 3 so a
    loaded runner does not turn it into a flake.

Verified both directions locally: fails on the synchronous form, passes on the
merged one.
@danielhanchen

Copy link
Copy Markdown
Member Author

Merged current main in (2a05b759e). This PR is now test-only, and the test it carries had to be rewritten before it was worth keeping.

The code is already on main

_scan_loras_sync plus await asyncio.to_thread(...) landed via d20db3f1f (#7984). Main's version additionally threads hf_token into _scan_loras_sync and fills LoRAInfo.audio_type per row from _audio_type_of_checkpoint; this branch's two-argument form is the same function from before the audio probe existed. So all four conflict hunks resolve to main's side, and a blend would have dropped modality off every scan row.

The test asserted nothing, and I can prove it

It measured the largest gap between heartbeat ticks. But the last tick lands before the blocking call, and the stop flag is set the instant the call returns, so the heartbeat is never rescheduled and a stall leaves no gap in the list at all. Reverting models.py to the pre-fix synchronous call, the test still passed: nticks 5 maxgap 0.010.

It now counts ticks recorded strictly between the call and its return: roughly 30 on the merged code, exactly 0 on the pre-fix form, asserted against a floor of 3 so a loaded runner cannot flake it. Driven both directions:

### against the PRE-FIX blocking form (must FAIL)
tests/test_scan_loras_off_event_loop.py:77: AssertionError
1 failed in 5.89s

### against the FIXED to_thread form (must PASS)
1 passed in 5.91s

Second problem: it used @pytest.mark.asyncio, and pytest-asyncio is in none of the backend's requirements files and is not installed. It was the only file in studio/backend/tests using that mark, against 95 that use asyncio.run(...), and as written it errored with "async functions are not natively supported". That was almost certainly among this PR's original CI failures.

tests/test_scan_loras_off_event_loop.py + 3 related files   27 passed
ruff                                                        All checks passed

So: worth landing as a regression guard, since main has no coverage for the threading at all. But it is only that, and you should merge it knowing the fix itself is already in.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d782f965de

ℹ️ 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 71ad2b3 into main Aug 14, 2026
38 of 42 checks passed
@danielhanchen
danielhanchen deleted the studio-scan-loras-off-the-event-loop branch August 14, 2026 11:49
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