Repository navigation
Studio: keep users signed in when auth.db is busy - #14151
Conversation
Under heavy disk IO (a large GGUF download on Colab), POST /api/auth/refresh waited out the 5 s busy timeout on the event loop, returned 500 "database is locked", and the frontend then cleared a refresh token that was still valid. - auth.db uses synchronous=NORMAL when it is in WAL and keeps one WAL keeper connection for the server's lifetime, matching studio.db (unslothai#9945, unslothai#9996). - /api/auth/refresh runs off the event loop and rotates the refresh token in one BEGIN IMMEDIATE transaction. A busy database answers 503 with Retry-After (exposed via CORS) and leaves the old token valid. - An API-key request whose last_used_at touch hits a busy auth.db is still accepted instead of failing with 500. - The frontend clears tokens only on a 401 from refresh, retries 503/429 a bounded number of times, and keeps the session (and route guards) on any other failure.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…nds auth.db busy The change-password form and the desktop app's managed-account startup still read refreshSession()'s false as a rejected token and cleared both tokens, so a busy auth.db could still sign the user out there. Both now use the refresh outcome: a rejected refresh clears the session as before, while an unanswered one keeps it. The form asks the user to try again, and the forced desktop startup probe releases the startup screen without opening the login page.
|
Update after the latest run:
|
## Summary `(Python 3.13, l-r)` fails on main at `6069a27a`, and on PRs based on it such as #14151: ``` studio/backend/tests/test_log_budget.py::test_every_classified_path_is_in_a_scenario AssertionError: these paths are classified in loggers/handlers.py but no scenario in tests/log_budget/session.py polls them, so their log volume is unmeasured: /api/inference/images/prompt-upsampler/status ``` #14137 (FLUX.3 prompt upsampling) added `/api/inference/images/prompt-upsampler/status` to `_QUIET_SUCCESS_PATHS` in `loggers/handlers.py`, with the note "Polled by the Images page while the FLUX.3 prompt upsampler downloads". It did not add the path to either log-budget scenario. The guard is right to fail: every classified poll needs a cadence so the idle and busy envelopes stay honest. The product is fine. ## Change `studio/backend/tests/log_budget/session.py` adds the path to `BUSY_POLLS` at `(1.0, "declared")`. The cadence comes from `usePromptUpsamplerStatus` in `studio/frontend/src/features/images/prompt-enhance-panel.tsx`: - It reads the status once when the panel becomes active. - After that it reads every 1000 ms only while `download.downloading` is true. It is therefore busy-only, at the same 1 s as the other download-progress polls. `tests/studio/playwright_declared_polls.py` reads the same `ALL_POLLS`, so the browser check knows the path too. ## Testing - `studio/backend/tests/test_log_budget.py`: 8 passed. On main without this change, `test_every_classified_path_is_in_a_scenario` fails with the error above. - `test_log_signal_floor.py` and `test_research_runs_hardening.py`, which also import the scenario module: 328 passed.
…upsampler tests (#14169) ## Summary `(Python 3.13, a-k)` failed three of #14137's new FLUX.3 prompt upsampler tests on #14151's run (base `def3451e`), on two different xdist workers: ``` tests/test_flux3_prompt_upsampler.py::test_generated_only_penalties_follow_the_server[True] tests/test_flux3_prompt_upsampler.py::test_generated_only_penalties_follow_the_server[False] tests/test_flux3_prompt_upsampler.py::test_stop_during_server_start_cancels_the_start core.inference.flux3_prompt_upsampler.UpsamplerUnavailableError: The image model no longer holds the GPU. ``` On their own the tests pass on main (93 passed, serially and with `-n 4`). They fail only after another test has run in the same process. #14151 does not touch either file, so any PR whose worker draws this order will hit it. ## Cause `FluxPromptUpsampler.generate` starts the sidecar only while `_image_model_owns_gpu()`, which reads `gpu_arbiter.current_owner()` (image model or nobody). That arbiter is process-wide, and the upsampler tests never pinned it. I ran each `tests/test_[a-k]*` file that touches the arbiter in front of `test_flux3_prompt_upsampler.py` in one process. Only `test_gpu_selection.py` reproduces the failures, and it gives the same three IDs. Tracing `gpu_arbiter._owner` around each test points at one test: `TestRouteErrors::test_inference_route_returns_400_for_invalid_gpu_ids` (`None -> 'chat'`). It drives the real `/load` route, which claims the GPU for CHAT through `acquire_for_request` before the stubbed backend refuses `gpu_ids = [99]`. Nothing puts the arbiter back afterwards. So the tests are what is wrong here: one leaks global state, the other depends on it. A failed chat load leaving CHAT claimed with nothing loaded is a separate product question, which this PR does not change. I could not find a failure path in `routes/inference.py` that calls `release_chat_gpu_claim()`. It may be worth a look on its own. ## Change Test only: - **`test_gpu_selection.py`:** `TestRouteErrors.setUp` saves the arbiter's module state (`_owner`, `_owner_epoch`, `_owner_account`, `_prior_account`) and restores it with `addCleanup`. The route tests therefore leave the arbiter as they found it. - **`test_flux3_prompt_upsampler.py`:** the autouse `_clean` fixture pins `gpu_arbiter._owner` to `None`, the fresh-start state those tests assume. The tests about a different owner already set it themselves and still do. ## Testing `tests/test_gpu_selection.py tests/test_flux3_prompt_upsampler.py` in one process, in that order: | change | result | |---|---| | neither (main) | 3 failed, 194 passed: the three IDs above | | only the upsampler pin | 197 passed | | only the `TestRouteErrors` restore | 197 passed | | both (this PR) | 197 passed | `test_gpu_selection.py`, `test_flux3_prompt_upsampler.py` and `test_gpu_arbiter.py` together with `-n 4` give 218 passed.
|
@codex review |
|
@codex security review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
Summary
On Google Colab during a large GGUF download,
POST /api/auth/refreshreturned 500sqlite3.OperationalError: database is lockedafter about 5 s (raised atBEGIN IMMEDIATEinconsume_refresh_token), and Unsloth Studio signed the user out even though the refresh token was still valid.There were four causes. auth.db kept SQLite's default
synchronous=FULLunder WAL, so every commit fsyncs while holding the write lock. It also had no long-lived connection, so the last close ran a checkpoint inside the process-widesqlite_gatelock./api/auth/refreshwasasync defand ran the blocking SQLite busy wait on the event loop, which stalled the whole server. Finally, the frontend cleared both tokens on any non-OK refresh response. studio.db already hadsynchronous=NORMALand a WAL keeper (#9945, #9996); auth.db now gets the same treatment, and a busy auth.db no longer ends the session.Changes
synchronous=NORMALwhen it is in WAL mode. The lifespan holds one WAL keeper connection for the server's lifetime.is_busy_errorandapply_wal_synchronousmove fromstudio_db.pytostorage/sqlite_gate.pyso both databases share them, and the old names instudio_dbremain as aliases./api/auth/refreshis now a sync route, so it runs off the event loop. The newrotate_refresh_tokendeletes the old token and inserts the new one in a singleBEGIN IMMEDIATEtransaction. A busy auth.db rolls back and returns 503 withRetry-After: 2, and the old token stays valid.Retry-Afteris added to the CORS exposed headers.validate_api_key_accountfinds auth.db busy, it skips thelast_used_atupdate and validates read-only. The request no longer fails with 500.Retry-After. Any other failure keeps the session, and the route guard counts a surviving refresh token as signed in.refreshSessionOutcome()returnsok,rejectedorunavailable, andrefreshSession()still returns a boolean.Behavior notes:
test_config_mutations_on_event_loop.pynow lists/refreshamong the off-loop routes, because the rotation is atomic in storage and no longer depends on the event loop for serialization.synchronous=NORMALcan lose the last commits on a power loss or OS crash, but not on an application crash. This is the same trade-off as Stop one slow SQLite writer from becoming a stream of Studio log errors #9996.Testing
Fault-injection A/B on a local Unsloth Studio, main
475a02ac18vs this branch:fdatasynconauth.db-walis delayed 7 s viaLD_PRELOAD./api/healthup to 4871 msRetry-After: 2after ~5 s,/api/healthmax 230 ms; the same refresh token returns 200 after release; a reused token still returns 401/api/healthstalled 5258 mslast_used_atupdate skipped)PRAGMA synchronouson auth.dbA cross-origin (
tauri://localhost) 503 exposesRetry-Aftervia CORS.Automated checks:
studio/backend/tests/test_auth_db_lock_contention.pyplustest_config_mutations_on_event_loop.pyandtest_studio_db_write_lock_contention.py: 98 passed (pytest -n 2).avmodule and sqlite-vec extension).tests/auth-refresh-transient.test.ts14/14. All 91 frontend test files that touch auth: 1073/1073.npm run typecheck(tsc) is clean.cd studio/backend python -m pytest -n 2 tests/test_auth_db_lock_contention.py tests/test_config_mutations_on_event_loop.py tests/test_studio_db_write_lock_contention.pyBefore / after
The app loaded with an expired access token while another connection held the auth.db write lock for 16 s (merge base
475a02ac1vs headae419121f, each on its own fresh Studio install)./api/auth/refreshanswered 500, both tokens were cleared and the page went to the login screen./api/auth/refreshanswered 503 twice (retried within the budget), the refresh token was kept and the page stayed in the app.