feat(runtime)!: serveDeno and serveBun boot before they bind
CI / dco (pull_request) Successful in 6s
CI / build (pull_request) Successful in 30s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 13s

Both were synchronous and left the boot to the first request, with the fix
documented as something the caller should remember: `await app.boot()` before
`Deno.serve` / `Bun.serve`. That made the correct use of a core helper depend
on reading a README, in a framework whose whole argument is that order should
not be something you have to get right.

The failure it leaves is the bad kind. `app.fetch` boots lazily and the boot
memo keeps the *rejection*, so a missing signing key does not fail the deploy:
the port binds, every request is answered 500 by the runtime, and none of them
reach `onError`. Anything that health-checks by asking whether the process is
listening sees a healthy server.

`listen()` has always awaited `bootApp()` before binding its socket, with a
comment saying it exists so Node and Deno/Bun serve an identical graph. They
did not. Now the three helpers agree, and the two adapters return promises.

`edgeHandler` is deliberately unchanged: on workerd there is no startup outside
a request, so there is no earlier moment to move the failure to.

Both new tests were confirmed to fail with the gate commented out — without it
`serveBun` resolves instead of rejecting, and serveDeno binds a port that 500s.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-19 12:38:50 -05:00
1 parent cbeab73626
commit 9634ec73e8
18 files changed
+147 -38

No files matched your search

+6 -5
View File
@@ -62,11 +62,12 @@ export interface App {
/**
* Boots the providers now, instead of on the first request.
*
* `listen()` already does this before it accepts a connection. Every other path — `app.fetch`,
* `app.upgrade`, `serveDeno`, `serveBun`, `edgeHandler` — boots lazily on the first request, and a
* provider that throws there fails *every* request, answered by the runtime with a 500 that never
* reaches `onError`. Calling this first turns that into a startup failure, which is where a bad
* key or an unreachable database belongs.
* The three serving helpers call it for you: `listen()` before it accepts a connection,
* `serveDeno()` and `serveBun()` before they bind. Calling it by hand is for the paths that have
* no startup of their own — `app.fetch` and `app.upgrade` driven by your own server, where a
* provider that throws fails *every* request, answered by the runtime with a 500 that never
* reaches `onError`, because the boot memo keeps the rejection. Calling this first turns that into
* a startup failure, which is where a bad key or an unreachable database belongs.
*
* Idempotent, and shares its memo with `listen()` and `fetch()`: calling both boots once. Unlike
* `ready()`, it does run provider factories.
+13 -1
View File
@@ -74,8 +74,20 @@ function bunSocket(ws: BunServerWebSocket): WsSocket {
* (inbound channel, abort, WsRequest) is stashed on ws.data during the upgrade.
* Bun.serve exposes no equivalent to Node's server.maxConnections; enforce connection caps at
* the deployment platform or reverse proxy.
*
* **Boots before it binds**, which is why it is async — see the `await app.boot()` below.
*/
export function serveBun(app: App, options?: BunServeShortOptions): BunServeResult {
export async function serveBun(app: App, options?: BunServeShortOptions): Promise<BunServeResult> {
// The same gate `listen()` runs before it binds a socket, and for the same reason: resolve the
// graph and run the provider factories here, so a missing key or an unreachable pool is a startup
// failure rather than a 500 on every request.
//
// Leaving it to `app.fetch` is not the lazier equivalent. The boot memo keeps a *rejection*, so
// the first request would fix the failure in place and the runtime would answer from it forever,
// without ever reaching `onError`. This was documented as something the caller should remember to
// do, which made the correct use of a core helper depend on reading a README.
await app.boot();
const server = Bun.serve({
...options,
fetch(request, server) {
+13 -1
View File
@@ -51,8 +51,20 @@ declare const Deno: {
* WebSocket messages flow through app.upgrade over the shared neutral core.
* Deno.serve exposes no equivalent to Node's server.maxConnections; enforce connection caps at
* the deployment platform or reverse proxy.
*
* **Boots before it binds**, which is why it is async — see the `await app.boot()` below.
*/
export function serveDeno(app: App, options?: DenoServeOptions): DenoServer {
export async function serveDeno(app: App, options?: DenoServeOptions): Promise<DenoServer> {
// The same gate `listen()` runs before it binds a socket, and for the same reason: resolve the
// graph and run the provider factories here, so a missing key or an unreachable pool is a startup
// failure rather than a 500 on every request.
//
// Leaving it to `app.fetch` is not the lazier equivalent. The boot memo keeps a *rejection*, so
// the first request would fix the failure in place and the runtime would answer from it forever,
// without ever reaching `onError`. This was documented as something the caller should remember to
// do, which made the correct use of a core helper depend on reading a README.
await app.boot();
// Deno's only force-close lever is the abort signal it was served with, so the adapter has to
// own one to offer a deadline at all. A caller-supplied signal still works and still aborts the
// server — it is chained into ours rather than replaced, so neither party loses its control.