fix(app): listen() rejects when the port is taken (#138)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 18s
CI / runtimes (deno) (pull_request) Successful in 33s

`listen()` awaited `server.listen(port, cb)` without an `error` listener.
A bind failure (EADDRINUSE, EACCES) never calls the callback, so the
promise hung and the error went unhandled. That is the 30 s timeout #138
saw on CI: owner.test.ts reserved a port with `freePort()` (listen on 0,
close, reuse the number), another process on the shared runner took it,
and the teapot's `listen` hung instead of failing.

- core: `bind()` rejects on the `error` event. On failure `server` goes
  back to undefined, so `close()` resolves and `listen()` can retry.
- owner.test.ts: lifetime A listens on port 0 directly, since its teapot
  is up before the teacup needs the port. Lifetime B has to boot its
  teacup against a port nobody holds yet, so it keeps `freePort()` and
  reruns on a fresh port on EADDRINUSE, up to three times.

Tests: listen on a taken port rejects with EADDRINUSE, then the same app
listens on 0 and serves; close() after a failed listen resolves. Both
hung without the fix.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-24 14:12:50 -05:00
1 parent 4e02cdc351
commit e09d05028c
4 files changed
+86 -8

No files matched your search

+39
View File
@@ -697,3 +697,42 @@ describe('route table validation', () => {
expect(() => createApp({ modules: [MethodsModule] })).not.toThrow();
});
});
describe('listen() on a port already taken (#138)', () => {
@Route('/')
class TakenCtl {
@Get('/') get() {
return 'ok';
}
}
@Module({ mountpoint: '/', controllers: [TakenCtl] })
class TakenModule {}
it('rejects with EADDRINUSE, and the app can still close or listen elsewhere', async () => {
const { createServer } = await import('node:net');
const blocker = createServer();
await new Promise<void>((resolve) => blocker.listen(0, resolve));
const app = createApp({ modules: [TakenModule] });
try {
await expect(app.listen((blocker.address() as { port: number }).port)).rejects.toMatchObject({
code: 'EADDRINUSE',
});
const server = await app.listen(0);
const res = await fetch(`http://127.0.0.1:${(server.address() as { port: number }).port}/`);
expect(res.status).toBe(200);
} finally {
await app.close();
blocker.close();
}
});
it('close() after a failed listen resolves', async () => {
const { createServer } = await import('node:net');
const blocker = createServer();
await new Promise<void>((resolve) => blocker.listen(0, resolve));
const app = createApp({ modules: [TakenModule] });
await expect(app.listen((blocker.address() as { port: number }).port)).rejects.toThrow();
await app.close();
blocker.close();
});
});
+21 -7
View File
@@ -116,10 +116,9 @@ const routesChanged = (app: App) =>
async function meshLifetimes() {
const results = [];
const portA = await freePort();
// Port 0: A's teapot is up before its teacup needs the port, so nothing has to be reserved.
const teapotA = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
await teapotA.listen(portA);
const teacupA = teacupFor(portA);
const teacupA = teacupFor(((await teapotA.listen(0)).address() as AddressInfo).port);
try {
results.push(...(await probe(teacupA, 'A1 teapot up at boot')));
await teapotA.close();
@@ -128,21 +127,36 @@ async function meshLifetimes() {
await teacupA.close();
}
results.push(...(await lateTeapot()));
return results;
}
/**
* Lifetime B. Its teacup must know the port before the teapot exists, so the port is only free when
* checked; another process on a shared runner can take it before the teapot binds (#138). That is
* the host, not the router, so the whole lifetime reruns on a fresh port.
*/
async function lateTeapot(attempts = 3): Promise<Awaited<ReturnType<typeof probe>>> {
const portB = await freePort();
const teacupB = teacupFor(portB);
const teapotB = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
try {
results.push(...(await probe(teacupB, 'B1 teapot absent at boot')));
const results = await probe(teacupB, 'B1 teapot absent at boot');
const spliced = routesChanged(teacupB);
await teapotB.listen(portB);
try {
await teapotB.listen(portB);
} catch (err) {
spliced.catch(() => {});
if ((err as NodeJS.ErrnoException).code === 'EADDRINUSE' && attempts > 1) return await lateTeapot(attempts - 1);
throw err;
}
await spliced;
results.push(...(await probe(teacupB, 'B2 teapot up after boot')));
return results;
} finally {
await teacupB.close();
await teapotB.close();
}
return results;
}
// ── mesh: local and remote routes answering one path (CARD-07 indexes them as two trees) ─────────