Skip to content

Measure the schema bring-up, and record why it can't just be backgrounded - #303

Merged
Sbussiso merged 1 commit into
masterfrom
ops/ddl-boot-budget
Sep 14, 2026
Merged

Sbussiso merged 1 commit into
masterfrom
ops/ddl-boot-budget

Conversation

@Sbussiso

Copy link
Copy Markdown
Contributor

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() and sync_schema() run synchronously at module import. Nothing
is listening on the port while they run, Fly's grace_period for the app
group is 30s, and the single-machine immediate deploy strategy means
exceeding 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:

[ 0.03s] lifespan startup BEGIN (sleeping 6s)
   t≈2s  TCP: REFUSED   HTTP /health -> 000
[ 6.04s] lifespan startup END
          TCP: ACCEPTED  HTTP /health -> 200

Module import is earlier still, so both positions are equally invisible to a
health check. What actually makes sync_indexes safe isn't its location — it's
asyncio.create_task(asyncio.to_thread(...)), fire-and-forget, so startup
returns 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:

call backgrounded consequence
create_all a fresh database serves its first request against missing tables
sync_schema every query touching a newly added column errors until it lands

That 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 WARNING above 10s of the 30s budget
that names the consequence — because "machine never became healthy" looks
nothing like "a migration got slow".

INFO  Schema bring-up 0.02s (create_all 0.00s, sync_schema 0.02s, changes: none)

WARN  Schema bring-up took 12.4s (...). Nothing is listening on the port until
      this finishes and Fly's health grace is 30s — a slower migration than
      this will fail the deploy rather than run late.

Both branches verified to fire (the WARNING by temporarily lowering the
threshold).

Verification

  • 874 backend tests pass, ruff clean
  • uvicorn bind ordering measured, not assumed

🤖 Generated with Claude Code

…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>
@Sbussiso
Sbussiso merged commit 072b28c into master Sep 14, 2026
12 checks passed
@Sbussiso
Sbussiso deleted the ops/ddl-boot-budget branch September 14, 2026 04:33
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.

1 participant