3 Commits
Author SHA1 Message Date
chrnx bca6fd09eb fix(app): listen() rejects when the port is taken (#138)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
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>
2026-10-24 14:13:39 -05:00
chrnx 1611ec0bfe Merge pull request 'docs(changelog): static revalidation is a breaking change (CARD-15, D12)' (#153) from docs/changelog-1c-breaking into develop
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 20s
CI / build (push) Successful in 29s
CI / runtimes (edge) (push) Successful in 19s
Promote → GitHub / push-github (push) Successful in 6s
CI / runtimes (deno) (push) Successful in 33s
2026-10-24 19:13:23 +00:00
chrnx b23b4e4161 docs(changelog): static revalidation is a breaking change (CARD-15, D12)
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 19s
CI / runtimes (deno) (pull_request) Successful in 33s
Every static file now goes out with `Cache-Control: no-cache`, where
before it sent no Cache-Control at all. A browser or proxy that reused
static files for a lifetime guessed from `Last-Modified` now revalidates
each one, and keeping the old behaviour needs a config change (`static:
{ cacheControl }`). That is a breaking change, so the entry moves from
`### Changed` to `### Breaking`, word for word.

D12 had filed it under `### Changed`. CARD-22's gate table already said
a breaking entry was due if the default turned on, and it did. The
pre-freeze review flagged the mismatch.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 14:10:55 -05:00
4 changed files with 92 additions and 13 deletions

No files matched your search

+13 -6
View File
@@ -99,6 +99,13 @@ npm treats versions as semver and semver forbids leading zeros.
`app.upgrade` already did. An app with no `@Ws`/`@Stream` routes and no mesh attaches no upgrade `app.upgrade` already did. An app with no `@Ws`/`@Stream` routes and no mesh attaches no upgrade
listener on Node, as before, so there the request still reaches the HTTP route table. listener on Node, as before, so there the request still reaches the HTTP route table.
- **Static files are revalidated, not re-downloaded** (#114, D12). Every static hit carries a weak
`ETag` (`W/"<size>-<mtime>"`), `Last-Modified` and `Cache-Control: no-cache`. A matching
`If-None-Match`, or `If-Modified-Since` when there is no `If-None-Match`, answers `304` from the
file's `stat` without reading it. A `HEAD` no longer reads the file either. `no-cache` stops
browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old
behaviour, set `static: { cacheControl }` to whatever your proxy expects.
### Added ### Added
- **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__` - **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__`
@@ -177,12 +184,6 @@ npm treats versions as semver and semver forbids leading zeros.
### Changed ### Changed
- **Static files are revalidated, not re-downloaded** (#114, D12). Every static hit carries a weak
`ETag` (`W/"<size>-<mtime>"`), `Last-Modified` and `Cache-Control: no-cache`. A matching
`If-None-Match`, or `If-Modified-Since` when there is no `If-None-Match`, answers `304` from the
file's `stat` without reading it. A `HEAD` no longer reads the file either. `no-cache` stops
browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old
behaviour, set `static: { cacheControl }` to whatever your proxy expects.
- **Every `304` is bodiless** (#114). A `304` has no body and no `content-*` headers on any adapter, - **Every `304` is bodiless** (#114). A `304` has no body and no `content-*` headers on any adapter,
including one from `throw new NotModified()`, which used to go out with the default JSON including one from `throw new NotModified()`, which used to go out with the default JSON
envelope's `content-type`. envelope's `content-type`.
@@ -232,6 +233,12 @@ npm treats versions as semver and semver forbids leading zeros.
### Fixed ### Fixed
- **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or
`EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise
never settled, and the error surfaced as an unhandled one. It now rejects with that error. The
app counts as never having listened, so `close()` resolves and a second `listen()` can try
another port.
- **WebSocket outbound respects backpressure** (#41). A `@Ws` handler's async iterator was pulled - **WebSocket outbound respects backpressure** (#41). A `@Ws` handler's async iterator was pulled
as fast as it could yield, whatever the peer read, so a fast producer and a slow client grew the as fast as it could yield, whatever the peer read, so a fast producer and a slow client grew the
send buffer without limit: 10,000 frames of 64 KiB put 640 MiB in memory at once. The pump now send buffer without limit: 10,000 frames of 64 KiB put 640 MiB in memory at once. The pump now
+20 -1
View File
@@ -455,7 +455,11 @@ export function createApp(opts: {
etag: opts.etag, etag: opts.etag,
}); });
server.on('close', () => closeLinks(meshLinks)); server.on('close', () => closeLinks(meshLinks));
await new Promise<void>((resolve) => server!.listen(port, resolve)); await bind(server, port).catch((err: unknown) => {
// Never listened: `close()` treats the app as one that never served, and `listen()` can retry.
server = undefined;
throw err;
});
// After the socket is up, so a boot that throws never leaves a handler behind pointing at an // After the socket is up, so a boot that throws never leaves a handler behind pointing at an
// app that never served. // app that never served.
handleSignals(close); handleSignals(close);
@@ -1996,6 +2000,21 @@ function runTeardown(teardown: TeardownRegistry, logger: Logger, budgetMs: numbe
}); });
} }
/**
* `server.listen`, settled either way. A bind failure (`EADDRINUSE`, `EACCES`) arrives as an
* `error` event, never the listening callback, so without the listener `listen()` would hang
* and the error would surface as an unhandled one (#138).
*/
function bind(server: http.Server, port: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
server.once('error', reject);
server.listen(port, () => {
server.off('error', reject);
resolve();
});
});
}
function closeApp( function closeApp(
server: http.Server | undefined, server: http.Server | undefined,
meshLinks: Closable[], meshLinks: Closable[],
+39
View File
@@ -697,3 +697,42 @@ describe('route table validation', () => {
expect(() => createApp({ modules: [MethodsModule] })).not.toThrow(); 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();
});
});
+20 -6
View File
@@ -116,10 +116,9 @@ const routesChanged = (app: App) =>
async function meshLifetimes() { async function meshLifetimes() {
const results = []; 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 } }); const teapotA = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
await teapotA.listen(portA); const teacupA = teacupFor(((await teapotA.listen(0)).address() as AddressInfo).port);
const teacupA = teacupFor(portA);
try { try {
results.push(...(await probe(teacupA, 'A1 teapot up at boot'))); results.push(...(await probe(teacupA, 'A1 teapot up at boot')));
await teapotA.close(); await teapotA.close();
@@ -128,21 +127,36 @@ async function meshLifetimes() {
await teacupA.close(); 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 portB = await freePort();
const teacupB = teacupFor(portB); const teacupB = teacupFor(portB);
const teapotB = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } }); const teapotB = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
try { 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); const spliced = routesChanged(teacupB);
try {
await teapotB.listen(portB); 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; await spliced;
results.push(...(await probe(teacupB, 'B2 teapot up after boot'))); results.push(...(await probe(teacupB, 'B2 teapot up after boot')));
return results;
} finally { } finally {
await teacupB.close(); await teacupB.close();
await teapotB.close(); await teapotB.close();
} }
return results;
} }
// ── mesh: local and remote routes answering one path (CARD-07 indexes them as two trees) ───────── // ── mesh: local and remote routes answering one path (CARD-07 indexes them as two trees) ─────────