Repository navigation
studio: dump the backend's own thread stacks while a stall is in progress - #9715
Conversation
…ai#9712) Co-authored-by: Cursor <[email protected]>
Co-authored-by: Cursor <[email protected]>
…down Co-authored-by: Cursor <[email protected]>
|
Read through the watchdog and the two capture paths, and the dead man's switch reasoning holds up for the GIL-held shape in #9712. Since this is diagnostic rather than product behaviour, could you gate the arming on the env the CI workflow sets so a normal desktop run does not carry an extra thread and a faulthandler timer? |
Co-authored-by: Cursor <[email protected]>
|
Done, it's opt-in now. UNSLOTH_STUDIO_STALL_WATCHDOG=1 arms it and the smoke workflow sets that for every phase, a plain desktop run starts nothing. |
Resolve the shutdown comment overlap while preserving the stall watchdog behavior.
|
Conflicts solved. |
|
Review record for #9715, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#444. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1: reviewed mirror
|
Fixes unslothai#9712 ## What was wrong FastAPI's built-in `/openapi.json` route is `async`, and it builds the schema inside that route, on the event loop. Studio has about 550 routes, so the first build per backend process takes seconds, and nothing else is served while it runs: `/api/liveness`, `/api/health`, chat, everything. Every session triggers it. Since unslothai#8078 the chat sidebar fetches `/openapi.json` to check whether the backend supports guarded title patches before it repairs legacy chat titles. ## How it was found The stall watchdog from unslothai#9715 is armed in `studio-mac-ui-smoke.yml` and logs `event loop answering again after Xs` whenever a no-op scheduled onto the loop takes more than 1s. I went through the server logs from the 167 runs whose artifacts are still retained (Oct 18-19): - 126 of 167 runs had at least one loop stall over 1s, 183 stalls in total, the longest 5.3s. - 182 of the 183 overlap a slow `/openapi.json`. The one left over is a single 2.6s probe during `/api/inference/validate`. - The first `/openapi.json` per backend took 2.26s median, 3.20s p90 and 5.94s max on the macOS runner (622 backends). Later calls hit FastAPI's cache: 17ms median. None of those runs had a 10s+ stall or a faulthandler dump, and the logs from the runs in the original report have expired. So I cannot prove the 10-33s windows in unslothai#9712 were this. The shape does match, though: a request accepted and then left sitting, nothing served, and recovery on its own. The same CPU-bound build on a starved 3 vCPU runner that is also running CPU-only llama-server could plausibly last that long. ## The change `FastAPI(openapi_url=None)` turns off the built-in route, and `/openapi.json` is registered on the same path. It builds the schema with `asyncio.to_thread`, under a lock so that two cold requests build it once, and later requests read FastAPI's existing cache. The `root_path` server entry is added the same way FastAPI's route adds it. `app.openapi_url` stays `/openapi.json`, so `/docs` and `/redoc` are unchanged, and `app.openapi()` still works for callers in the same process. The served schema is byte-identical. The build still holds the GIL while it runs, but the loop now gets a turn at every switch interval, so requests keep being served during the build. ## Verification Repro: an in-process ASGI client polls `/api/liveness` on the backend's own event loop while the first `/openapi.json` is in flight. | | first `/openapi.json` | worst liveness delay during it | schema | |---|---|---|---| | main | 3.44s | 3490ms (loop blocked) | 1,049,101 bytes | | this PR | 4.10s | 292ms | 1,049,101 bytes, identical | This host was heavily loaded (loadavg ~690), so read these as ratios, not absolute times. `tests/test_openapi_off_the_event_loop.py` slows the cold build by 3s so the result does not depend on machine speed: - main: 2 failed (event loop blocked 7.6s and 7.7s), 1 passed (the behaviour-parity test) - this PR: 3 passed Route, auth and docs suites (`test_middleware`, `test_desktop_auth`, `test_keyless_api_access*`, `test_lan_access_settings`, `test_quick_tunnel_streaming_routes`, `test_mcp_forward`, `test_chat_history_routes`, `test_account_installation_mutations`, `test_health_answers_within_probe_budget`, `test_stall_watchdog`) pass when run serially. Under `-n 8` on the loaded host, `test_desktop_auth::test_health_response_reports_desktop_capability_fields` fails the same way on main. End to end on macOS (`Chat UI, API, Update and GGUF inference`, staging run 39253072492, green): the first `/openapi.json` per backend still took 1.76-2.82s to build, but none of the 6 backend logs had a single `consecutive slow probes` line, so the event loop never went more than 1s without answering. On main, 126 of 167 runs had at least one.
The stalls in #9712 are over before anyone can attach py-spy, so the dump has to come from inside the process while the stall is still running.
A watchdog thread beats against the event loop. Each beat re-arms faulthandler.dump_traceback_later as a dead man's switch: faulthandler's timeout thread runs C code that never takes the GIL, so when a GIL holder freezes every Python thread, the watchdog included, the switch still fires mid-stall and writes the frame every thread is sitting in. The beat also times a no-op scheduled onto the loop and dumps from Python after three consecutive slow probes, which covers a loop that is stuck while the GIL is free. Thresholds are sized so both paths dump inside the shortest stall on record.
Dumps go to stderr, which the CI workflow and the desktop launcher already capture. The watchdog stands down from its first beat until the torch warm is over, because the switch cannot be disarmed once the warm's import holds the GIL. Both paths share one dump per cooldown window, and UNSLOTH_STUDIO_DISABLE_STALL_WATCHDOG=1 turns it off. Happy to gate it behind an env the CI workflow sets if a dump thread shouldn't ship in the product.
From the repo root:
9 passed in 8.0s on macOS, Python 3.12. The capture-path tests stall a real event loop: time.sleep scheduled onto it for the GIL-free path, and for the GIL-held path a busy loop under a stretched switch interval. Both assert the dump names the frame that held things up.