Public: anyone can read this; the crew can change it.

/signin answers without a stall on the serving thread #17835

opened this

Merged 4h 6m ago, live in production

The first /signin after each boot no longer stalls the serving thread. The sign-in door is now part of the warm-up that runs once the server has been idle after its first answer (#14662). Cause, with before counts.

Notes

opened this

The first /signin after each boot no longer stalls the serving thread. The sign-in door is now part of the warm-up that runs once the server has been idle after its first answer (#14662).

Cause, with before counts. Password hashing was not the problem: Bun.password.verify already runs off the serving thread. Five checks at the production cost (argon2id, 64 MiB, two passes) took about 900 ms in total, and the serving thread never paused for more than 1.8 ms. The stall came from loading the accounts route module, which only loads on its first request (#12521). The first GET or POST /signin after a boot loaded that module and drew the form for the first time on the serving thread. That held it for 120–225 ms (app;dur=207 while the named phases added up to 3 ms). Once loaded, the page answers in 1–2 ms (production showed app;dur=1.6). After that first load, a POST paused the thread for no more than 4 ms; the rest of its time is the off-thread argon2 check. My guess is that the [loop] log names /signin because that 180–250 ms request is still open across other stalls. I couldn't check this: production returns 404 for /metrics.

Change. src/http/warmup.ts adds a sign-in entry to WARM that requests GET /signin. That GET loads the module the POST shares and does the first render while nobody is waiting. The hash cost, the decoy hash used for unknown names, the throttle windows and the concurrent-hash cap are all unchanged.

Tests.

  • test/signin-loop-free.test.ts stores a real-cost argon2id hash, then checks sign-ins with a wrong password, an unknown name and the right password. It expects 422 for the first two with the same words, 303 for the right password, and the serving thread never pausing for more than 50 ms. With the warm-up removed, it fails at 127 ms.
  • test/warmup.test.ts now expects /signin first in the warm list.

tsc, the gate, and nine test files that cover warm-up, lazy loading, sign-in and handover all pass. scripts/fast.ts said the change reaches too much of the suite to name its tests, so I picked these myself.

Not done. Route modules stay loaded on first use, as #12521 chose. Only the sign-in door's module is now loaded ahead of time.

Try it. Open /signin on the preview right after a deploy. The first answer's Server-Timing app;dur should be in single-digit milliseconds instead of about 200.

[{"go":"/signin","look":"the Server-Timing response header on the first load after the preview deploys","expect":"app;dur in single-digit ms, no render or app phase near 200 ms"},{"go":"/signin","look":"submit a wrong password for any name","expect":"422 with \"That username and password don't match.\""}]

🤖 Generated with Claude Code

Closes #17822

2 earlier comments
commented

The first /signin after each boot no longer stalls the serving thread. The sign-in door is now part of the warm-up that runs once the server has been idle after its first answer (#14662).

Cause, with before counts. Password hashing was not the problem: Bun.password.verify already runs off the serving thread. Five checks at the production cost (argon2id, 64 MiB, two passes) took about 900 ms in total, and the serving thread never paused for more than 1.8 ms. The stall came from loading the accounts route module, which only loads on its first request (#12521). The first GET or POST /signin after a boot loaded that module and drew the form for the first time on the serving thread. That held it for 120–225 ms (app;dur=207 while the named phases added up to 3 ms). Once loaded, the page answers in 1–2 ms (production showed app;dur=1.6). After that first load, a POST paused the thread for no more than 4 ms; the rest of its time is the off-thread argon2 check. My guess is that the [loop] log names /signin because that 180–250 ms request is still open across other stalls. I couldn't check this: production returns 404 for /metrics.

Change. src/http/warmup.ts adds a sign-in entry to WARM that requests GET /signin. That GET loads the module the POST shares and does the first render while nobody is waiting. The hash cost, the decoy hash used for unknown names, the throttle windows and the concurrent-hash cap are all unchanged.

Tests.

  • test/signin-loop-free.test.ts stores a real-cost argon2id hash, then checks sign-ins with a wrong password, an unknown name and the right password. It expects 422 for the first two with the same words, 303 for the right password, and the serving thread never pausing for more than 50 ms. With the warm-up removed, it fails at 127 ms.
  • test/warmup.test.ts now expects /signin first in the warm list.

tsc, the gate, and nine test files that cover warm-up, lazy loading, sign-in and handover all pass. scripts/fast.ts said the change reaches too much of the suite to name its tests, so I picked these myself.

Not done. Route modules stay loaded on first use, as #12521 chose. Only the sign-in door's module is now loaded ahead of time.

Try it. Open /signin on the preview right after a deploy. The first answer's Server-Timing app;dur should be in single-digit milliseconds instead of about 200.

[{"go":"/signin","look":"the Server-Timing response header on the first load after the preview deploys","expect":"app;dur in single-digit ms, no render or app phase near 200 ms"},{"go":"/signin","look":"submit a wrong password for any name","expect":"422 with \"That username and password don't match.\""}]

🤖 Generated with Claude Code

commented

The first /signin after a boot no longer loads the accounts module on the serving thread (#17822). The booting process now loads the accounts module and draws /signin once before Bun.serve opens the port. Before this, the warm-up only ran after the first answer plus an idle wait, so an early visitor still paid for the load.

  • src/http/lazy_routes.ts: new loadFamilies(table, names) runs a family's cached loader before any request. A family that fails, or isn't on the table, is left out, and its first request tries again as before.
  • src/http/warmup.ts: new BOOT = { families: ["accounts"], pages: ["/signin"] }. The sign-in entry is gone from WARM, because boot now covers it.
  • src/http/server.ts: serve loads BOOT before listening, only when it's given warm (only main.ts passes it). It draws /signin the same way the hourly probe does. Servers.booted lists the families it loaded.

Measured on a fresh boot with no request first, using serve(app, { warm: {} }) then GET /signin (two runs each):

First GET /signin (server-timing app;dur)serve time
Before531–559 ms25–39 ms
After11–22 ms518–637 ms

So the boot is about half a second longer: the import cost moves from the first visitor to the boot. The first POST /signin with a wrong password still takes about 300 ms either way. That time is the argon2 check, which runs off the serving thread, not the import.

Test (test/signin-loop-free.test.ts): it no longer times anything, so it can't flake. It boots with the real WARM list and checks servers.booted is ["accounts"] before any request. Then a wrong-password POST /signin at full argon2 cost is the very first request, and it's refused. A forge served without warm loads nothing at boot. A unit test checks loadFamilies loads a family once and that the request then reuses it. It also checks that a family that fails or is missing is left out and loads on a later try. Without this change the booted check fails.

What I left out: the post-boot warm-up of the MCP and openapi pages is unchanged. test/warmup.test.ts ends up the same as on main.

Checks run: bun x tsc --noEmit passes, and the gate shows no failures. I chose 14 test files by hand because scripts/fast.ts named 2,806; all 65 tests pass, including boot-lazy, boot-no-stall, unused-exports, warmup, the sign-in tests and the probe tests.

🤖 Generated with Claude Code

commented

Before this change, the first /signin after a boot loaded the accounts module (120–485 ms) and drew its form for the first time (about 25 ms) on the serving thread, so every other request waited behind it. Now the booting process loads the accounts module and draws /signin once before the listener opens, so no request ever pays that cost.

  • src/http/lazy_routes.ts: loadFamilies(table, names) loads named lazy route families ahead of their first request. It keeps the existing rule that a load which fails is not kept, so that family's first request tries again.
  • src/http/warmup.ts: BOOT names what is loaded and drawn before the port opens (accounts and /signin).
  • src/http/server.ts: serve runs BOOT only when the server warms up, which means only in main.ts. If the page fails to draw, it logs a warning and the first request draws it. Servers.booted reports which families loaded.
  • test/signin-loop-free.test.ts: uses real argon2 at production cost. The very first request after a boot is a wrong password, and it is refused; a name with no account gets the same words; the right password signs in. It also covers that a server with no warm-up loads nothing at boot, and that loadFamilies loads once, skips what fails or is not on the table, and retries later.

The earlier check run failed on "the pane's frame > draws a public app" and "a cold /map answers within a few passes over the runs". Neither runs the new boot step, because both serve with no warm-up. The cause was this pull's new test: it served with the full warm-up list. The check runs test files with --no-isolate, so files that share a worker share one process. That warm-up kept loading public pages after its first answer and reached into whichever file ran next in that worker. The test now warms with an empty family list, so it still runs the boot step and starts nothing afterwards. Neither of the failing tests was changed. With --no-isolate, the new test, the /map budget test and the frame test pass together locally; the frame tests are skipped because no Chromium starts in this sandbox. bun x tsc --noEmit and the gate (32 rules) pass.

Left out: the boot step runs only in the booting process, so the code's other serve callers (tests and tools) don't load anything early.

To try it, boot the app (bun src/main.ts). Then make a POST to /signin as the first request and check that / answers at once while it is in flight.

[{"go":"/signin","look":"the sign-in form","expect":"it draws at once on the first visit after a deploy, with no pause before the page appears"},{"go":"/signin","look":"submit a wrong password","expect":"\"That username and password don't match.\" comes back, and other tabs on the forge keep answering while it is checked"}]

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs