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

+4 -4
View File
@@ -48,9 +48,9 @@ function freePort(): number {
}
/** Boot a teapot on a fixed port; the handle kills it and leaves the port free again. */
function startTeapot(port: number) {
async function startTeapot(port: number) {
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const server = serveBun(app, { port });
const server = await serveBun(app, { port });
return {
stop: async () => {
@@ -64,7 +64,7 @@ const settle = (ms: number) => new Promise((r) => setTimeout(r, ms));
test('mesh on Bun: a teacup reconnects to a teapot that came back', async () => {
const port = freePort();
let teapot = startTeapot(port);
let teapot = await startTeapot(port);
const teacup = createApp({
modules: [TeacupModule],
@@ -88,7 +88,7 @@ test('mesh on Bun: a teacup reconnects to a teapot that came back', async () =>
const down = await teacup.fetch(new Request('http://x/api/local/who'));
expect(down.status).toBe(503);
teapot = startTeapot(port);
teapot = await startTeapot(port);
await settle(400);
const after = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'xyz' } }));
+3 -3
View File
@@ -42,14 +42,14 @@ const controlUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
test('mesh on Bun: teacup resolves a Bun teapot through the graph', async () => {
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const tServer = serveBun(teapot, { port: 0 });
const tServer = await serveBun(teapot, { port: 0 });
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url: controlUrl(tServer.port), secret: SECRET }] },
});
const cServer = serveBun(teacup, { port: 0 });
const cServer = await serveBun(teacup, { port: 0 });
try {
const res = await fetch(`http://127.0.0.1:${cServer.port}/api/local/who`, { headers: { 'x-token': 'abc' } });
@@ -67,7 +67,7 @@ test('mesh on Bun: teacup resolves a Bun teapot through the graph', async () =>
test('mesh on Bun: a teapot refuses a bad secret over its control channel', async () => {
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
const tServer = serveBun(teapot, { port: 0 });
const tServer = await serveBun(teapot, { port: 0 });
const teacup = createApp({
modules: [TeacupModule],
+29 -4
View File
@@ -6,7 +6,7 @@
// Run with: npm run test:bun (or: bun test test/bun/)
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Route, Get, Module } from '../../src/index.ts';
import { createApp, Route, Get, Module, Provider, needs } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
@Route('/')
@@ -25,7 +25,7 @@ class Ctl {
class M {}
test('serveBun close({ timeoutMs }) gives up on a stuck handler and resolves', async () => {
const server = serveBun(createApp({ modules: [M] }), { port: 0 });
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
fetch(`http://127.0.0.1:${server.port}/hang`).catch(() => {});
await Bun.sleep(50); // let the request actually reach the handler
@@ -40,7 +40,7 @@ test('serveBun close({ timeoutMs }) gives up on a stuck handler and resolves', a
});
test('serveBun close() drains an in-flight request rather than cutting it', async () => {
const server = serveBun(createApp({ modules: [M] }), { port: 0 });
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
const inFlight = fetch(`http://127.0.0.1:${server.port}/slow`).then((r) => r.json());
await Bun.sleep(50); // the request must be *in* the handler before we close, or it never got in
@@ -65,10 +65,35 @@ test('serveBun close() runs registered teardown', async () => {
}),
}],
});
const server = serveBun(app, { port: 0 });
const server = await serveBun(app, { port: 0 });
await server.close({ timeoutMs: 2000 });
// Reverse registration order: the plugin registered after the hook, so it tears down first.
expect(closed).toEqual(['plugin', 'hook']);
});
// The boot gate, which is why serveBun is async. Before it, a provider that threw was memoized as a
// rejection and answered 500 to every request from the runtime, never reaching onError — so the
// failure looked like a runtime fault at 3am instead of a deploy that refused to start.
@Provider({ provides: 'key' })
class BadKey {
provide(): never {
throw new Error('no such key file');
}
}
@Route('/')
class NeedsKey {
@Get('/signed')
signed(@needs('key') key: string) {
return { key };
}
}
@Module({ mountpoint: '/', providers: [BadKey], controllers: [NeedsKey] })
class KeyModule {}
test('serveBun rejects when a provider fails, instead of binding a port that 500s', async () => {
const app = createApp({ modules: [KeyModule] });
await expect(serveBun(app, { port: 0 })).rejects.toThrow(/no such key file/);
});
+1 -1
View File
@@ -26,7 +26,7 @@ class M {}
test('serveBun: HTTP + WebSocket round-trip', async () => {
const app = createApp({ modules: [M] });
const server = serveBun(app, { port: 0 });
const server = await serveBun(app, { port: 0 });
const port = server.port;
try {
const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`);