Repository navigation
Studio: run the LoRA scan off the event loop - #8392
Conversation
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.
There was a problem hiding this comment.
💡 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".
| stop = True | ||
| beat.cancel() |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Merged current main in ( The code is already on main
The test asserted nothing, and I can prove itIt 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 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: Second problem: it used 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. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
GET /api/models/loraswalks 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:
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_syncand the route awaits it withasyncio.to_thread, which is the pattern already used inroutes/llama.pyandroutes/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.