Measure the schema bring-up, and record why it can't just be backgrounded - #303
Merged
Merged
Conversation
…nded Review pass two flagged that create_all() and sync_schema() run synchronously at module import while sync_indexes was deliberately moved out of that path, and recommended moving both behind the same background step. Having now measured it, that recommendation was wrong twice over, and this commit records why so nobody spends the afternoon I just did. FIRST: relocating to lifespan would change nothing. uvicorn runs lifespan startup BEFORE it binds the socket — measured 2026-09-13 with a 6s artificial startup delay, TCP connections were refused for the whole of it and accepted only after. Module import is earlier still, so both positions are equally invisible to a health check. What actually makes sync_indexes safe is not its location: it is asyncio.create_task(asyncio.to_thread(...)), fire-and-forget, so startup returns immediately and the port binds while indexes build. SECOND: backgrounding these two is not safe. Indexes only make queries faster, so building them late is invisible. create_all and sync_schema make queries *possible* — background the first and a fresh database serves its first request against missing tables; background the second and every query touching a newly added column errors until it lands. That trades a noisy deploy failure for a silent data-error storm. So the original finding was right about the hazard (nothing is listening while this runs, Fly's grace is 30s, and the single-machine `immediate` strategy means failing it takes out the only machine) and wrong about the remedy. The lever actually available is visibility. Both calls are now timed and logged, with a WARNING above 10s of the 30s budget that names the consequence, because "machine never became healthy" looks nothing like "a migration got slow". Verified both branches fire: INFO at 0.02s on a normal boot, WARNING with the threshold lowered. 874 backend tests pass, ruff clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes out finding F from review pass two — which flagged a real hazard and
then recommended the wrong fix. Having measured it, this records both.
The hazard is real
create_all()andsync_schema()run synchronously at module import. Nothingis listening on the port while they run, Fly's
grace_periodfor theappgroup is 30s, and the single-machine
immediatedeploy strategy meansexceeding it fails the only machine with nothing serving.
But my recommended fix was wrong, twice
1. Relocating to lifespan changes nothing. uvicorn runs lifespan startup
before it binds the socket. Measured with a 6s artificial startup delay:
Module import is earlier still, so both positions are equally invisible to a
health check. What actually makes
sync_indexessafe isn't its location — it'sasyncio.create_task(asyncio.to_thread(...)), fire-and-forget, so startupreturns immediately and the port binds while indexes build.
2. Backgrounding these two isn't safe anyway. Indexes only make queries
faster, so building them late is invisible. These make queries possible:
create_allsync_schemaThat trades a noisy deploy failure for a silent data-error storm.
So: visibility, which is the lever that exists
Both calls are timed and logged, with a
WARNINGabove 10s of the 30s budgetthat names the consequence — because "machine never became healthy" looks
nothing like "a migration got slow".
Both branches verified to fire (the WARNING by temporarily lowering the
threshold).
Verification
ruffclean🤖 Generated with Claude Code