Skip to content

studio: dump the backend's own thread stacks while a stall is in progress - #9715

Merged
shimmyshimmer merged 8 commits into
unslothai:mainfrom
hellopahe:studio-stall-watchdog
Oct 5, 2026
Merged

shimmyshimmer merged 8 commits into
unslothai:mainfrom
hellopahe:studio-stall-watchdog

Conversation

@hellopahe

Copy link
Copy Markdown
Contributor

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:

python3 -m venv .venv
.venv/bin/pip install pytest structlog huggingface_hub
cd studio/backend
../../.venv/bin/python -m pytest tests/test_stall_watchdog.py -v

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.

@danielhanchen

Copy link
Copy Markdown
Member

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?

@hellopahe

Copy link
Copy Markdown
Contributor Author

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.
@hellopahe

Copy link
Copy Markdown
Contributor Author

Conflicts solved.
btw, a recent main run also logged a 10s stall, backend recovered and CI passed with a warning, not sure if it has the same cause as #9712

@danielhanchen

Copy link
Copy Markdown
Member

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 b2bccf651 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: b2bccf6512

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror b2bccf651 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@shimmyshimmer
shimmyshimmer merged commit cc5a85b into unslothai:main Oct 5, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 19, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants