Skip to content

Studio: build the OpenAPI schema off the event loop - #14239

Merged
danielhanchen merged 3 commits into
mainfrom
studio-openapi-off-loop
Oct 19, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-openapi-off-loop

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Fixes #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 #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 #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 #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.

@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-19T12:06:46.839910Z 9d8c80a New commits
🔒 Security Review ⚠️ Failed 2026-10-19T11:04:22.766930Z 9106a35 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 9106a358d9

ℹ️ 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 thread studio/backend/main.py
app.openapi = _openapi_once


@app.get(app.openapi_url, include_in_schema = False)

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 Preserve HEAD support on the OpenAPI route

Clients that probe /openapi.json with HEAD now receive 405 because @app.get registers only GET. FastAPI's built-in OpenAPI route uses Starlette's add_route, which automatically supports both GET and HEAD (confirmed against the pinned FastAPI 0.141.1), so this replacement should explicitly retain HEAD to avoid breaking metadata probes and HTTP clients that check the resource before fetching 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.

Studio already answers HEAD /openapi.json with 405 on main (TestClient(main.app).head("/openapi.json") at 37989cb returns 405, Allow: GET), and with this PR it is still 405, so nothing changes here. Plain FastAPI does answer HEAD on this route, but Studio's app did not before this PR either.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 9106a358d9

ℹ️ 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 a770de9 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the studio-openapi-off-loop branch October 19, 2026 13:04
danielhanchen added a commit that referenced this pull request Oct 19, 2026
## Summary

Main is over the startup bundle budget again, so `Frontend build +
bundle sanity` fails on every PR based on it:

```
over the startup budget: transfer 2012.0 KB > 2011.9 KB, raw 6681.8 KB > 6680.6 KB      (main at 26e246d)
```

#14247 (Developer mode opt-in for experimental CPU video generation)
added 1,777 B raw / 654 B transfer to the startup chunks. It merged
without raising the limits.

## Cause

`#14247` touches eager startup code. Its new strings land in
`i18n/locales/en.ts` (the English catalog ships in the startup chunks),
and the developer-mode flag is read from `__root.tsx`,
`app-sidebar.tsx`, `config/env.ts` and `config/hardware-verdict.ts`. The
settings section itself is lazy. This is small real growth with no lazy
home. The other commits since the last measurement (#14204, #14239,
#14249) change no frontend code.

## Change

- **`check-bundle-budget.ts`:** a BUDGET comment entry for #14247, and
both limits moved to the measured build plus a 160 B raw / 189 B
transfer margin:
  - `rawBytes`: 6,840,888 -> 6,842,343.
  - `transferBytes`: 2,060,219 -> 2,060,510.

## Testing

One machine and build, Node 24, exact byte totals from the budget
script. CI has matched this machine to the byte on earlier budget
changes.

| tree | raw B | transfer B |
|---|---|---|
| `88afa84b87` (before #14247) | 6,840,406 | 2,059,667 |
| main `26e246d407` | 6,842,183 | 2,060,321 |

- **Main against the old limits:** `over the startup budget: transfer
2012.0 KB > 2011.9 KB, raw 6681.8 KB > 6680.6 KB`.
- **With this change:** `within budget (0.2 KB transfer, 0.2 KB raw to
spare)`, 96 chunks.
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.

Studio backend stalls for 10-33s on macOS CI, serving nothing, then recovers

1 participant