From 3dcf784159ebb12779c84c572711e2bb722b1ef0 Mon Sep 17 00:00:00 2001 From: ServeurGoat Date: Fri, 7 Aug 2026 20:07:35 -0400 Subject: [PATCH 1/3] fix: add timeout to app.close() Signed-off-by: YxnnXriel --- src/app/index.ts | 68 +++++++++++++++++++++++++++++++++++++++--------- src/app/types.ts | 2 +- test/app.test.ts | 21 +++++++++++++++ 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/app/index.ts b/src/app/index.ts index 0096d01..8bbdd2a 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -260,19 +260,7 @@ export function createApp(opts: { return server; }; - const close = (): Promise => - new Promise((resolve) => { - // before the server check: a mesh app booted through app.fetch (Deno/Bun, where listen() - // never runs) has links but no server, and would otherwise leak every teapot connection. - // Closing a link rejects its in-flight RPCs via the socket's abort. - closeLinks(meshLinks); - - if (!server) return resolve(); - - server.close(() => resolve()); // waits for in-flight buffered to drain - for (const closeStream of streams) closeStream(); // force-close long-lived SSE/WS so close() resolves - server.closeIdleConnections(); // Node >=18.2: drop idle keep-alive - }); + const close = (options: { timeoutMs?: number } = {}): Promise => closeApp(server, meshLinks, streams, options); return { listen, @@ -957,6 +945,60 @@ function buildWsRoutes(routePlans: RoutePlan[], deps: PipelineDeps): WsRouteDef[ })); } +function closeApp( + server: http.Server | undefined, + meshLinks: Link[], + streams: Set<() => void>, + options: { timeoutMs?: number }, +): Promise { + return new Promise((resolve) => { + // A mesh app booted through `app.fetch` has links but no server, and would leak every + // teapot connection otherwise — so links close before we even check for a server. + closeLinks(meshLinks); + + if (!server) { + if (options.timeoutMs !== undefined) { + console.warn( + '[green-tea] close({ timeoutMs }) has no effect here — this app has no ' + + 'listen()ed server (Bun/Deno run through app.fetch). See #15.', + ); + } + + return resolve(); + } + + const timeoutMs = options.timeoutMs ?? 10_000; + let settled = false; + + const finish = () => { + if (settled) return; + settled = true; + clearTimeout(timer); + resolve(); + }; + + server.close(() => finish()); + + for (const closeStream of streams) closeStream(); + server.closeIdleConnections(); + + const timer = setTimeout(() => { + if (settled) return; + + console.warn( + `[green-tea] graceful shutdown timed out after ${timeoutMs}ms — ` + 'forcing remaining HTTP connections closed', + ); + + // closeAllConnections() requires Node >=18.2, same floor as closeIdleConnections() + // above — `engines` in package.json only guarantees >=18, so this matters. + server.closeAllConnections(); + finish(); + }, timeoutMs); + + timer.unref?.(); + }); +} + /** Best-effort close of every mesh link, ignoring individual failures. */ function closeLinks(links: Link[]): void { for (const link of links) { diff --git a/src/app/types.ts b/src/app/types.ts index 86090d3..d13c3b5 100644 --- a/src/app/types.ts +++ b/src/app/types.ts @@ -35,7 +35,7 @@ export interface Explain { /** A running application instance with lifecycle, introspection and graph-export methods. */ export interface App { listen(port: number): Promise; - close(): Promise; + close(options?: { timeoutMs?: number }): Promise; /** * Resolves the dependency graph, then returns. For a mesh app that means connecting to its * teapots and splicing their scopes in — a mesh graph is not knowable without asking. For every diff --git a/test/app.test.ts b/test/app.test.ts index 01ff2ff..bb97afe 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -466,6 +466,27 @@ describe('graceful shutdown', () => { }); }); +it('close({ timeoutMs }) forces shutdown when a handler never responds', async () => { + @Route('/') + class Ctl { + @Get('/hang') hang() { + return new Promise(() => {}); // never resolves — simulates a stuck handler + } + } + @Module({ mountpoint: '/', controllers: [Ctl] }) + class M {} + const app = createApp({ modules: [M] }); + const server = await app.listen(0); + const port = (server.address() as any).port; + + fetch(`http://127.0.0.1:${port}/hang`).catch(() => {}); + await new Promise((r) => setTimeout(r, 50)); + + const start = Date.now(); + await app.close({ timeoutMs: 300 }); + expect(Date.now() - start).toBeLessThan(600); +}); + describe('overrides', () => { const portOf = (s: any) => (s.address() as any).port; it('overrides a provider token with a plain value (wrapped as a merge object)', async () => { From 9d95260706cc948f1e0a709df0e1d25f5b9b72ee Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 13:33:57 -0500 Subject: [PATCH 2/3] chore(test): nest the close({ timeoutMs }) case inside its describe block The test landed one line below the closing brace of describe('graceful shutdown'), so it ran at the top level instead of under the block it belongs to. Placement only; the assertions are untouched. Signed-off-by: Diego Resendez --- test/app.test.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/app.test.ts b/test/app.test.ts index bb97afe..02ff257 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -464,27 +464,27 @@ describe('graceful shutdown', () => { await expect(fetch(`http://127.0.0.1:${port}/ticks`).then((r) => r.text())).rejects.toThrow(); reader.cancel().catch(() => {}); }); -}); -it('close({ timeoutMs }) forces shutdown when a handler never responds', async () => { - @Route('/') - class Ctl { - @Get('/hang') hang() { - return new Promise(() => {}); // never resolves — simulates a stuck handler + it('close({ timeoutMs }) forces shutdown when a handler never responds', async () => { + @Route('/') + class Ctl { + @Get('/hang') hang() { + return new Promise(() => {}); // never resolves — simulates a stuck handler + } } - } - @Module({ mountpoint: '/', controllers: [Ctl] }) - class M {} - const app = createApp({ modules: [M] }); - const server = await app.listen(0); - const port = (server.address() as any).port; + @Module({ mountpoint: '/', controllers: [Ctl] }) + class M {} + const app = createApp({ modules: [M] }); + const server = await app.listen(0); + const port = (server.address() as any).port; - fetch(`http://127.0.0.1:${port}/hang`).catch(() => {}); - await new Promise((r) => setTimeout(r, 50)); + fetch(`http://127.0.0.1:${port}/hang`).catch(() => {}); + await new Promise((r) => setTimeout(r, 50)); - const start = Date.now(); - await app.close({ timeoutMs: 300 }); - expect(Date.now() - start).toBeLessThan(600); + const start = Date.now(); + await app.close({ timeoutMs: 300 }); + expect(Date.now() - start).toBeLessThan(600); + }); }); describe('overrides', () => { From 4aae9c3e71e3fba92b54feda35d4677616f52d36 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Wed, 12 Aug 2026 14:09:22 -0500 Subject: [PATCH 3/3] chore(deps): bump nanoid to 3.3.18 in both lockfiles Clears Dependabot alert 26. The advisory is real but our exposure is not: nanoid arrives through postcss, under tsup at the root and under astro in website/, so it never ships. Production dependencies of the package are reflect-metadata and nothing else, and `npm audit --omit=dev` was already green. Reaching the bug needs control of the size argument passed to nanoid, which postcss calls with a fixed length. Fixing it anyway because the alert costs more to keep explaining than to close, and an open badge trains us to skim Dependabot. The next advisory should be the only one sitting there. Dependabot files it under website/ as runtime scope because website/package.json lists astro under dependencies and has no devDependencies at all. Moving them would stop that mislabelling, but that is a separate change. Signed-off-by: Diego Resendez --- package-lock.json | 6 +++--- website/package-lock.json | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index f62ff56..9072f3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5670,9 +5670,9 @@ } }, "node_modules/nanoid": { - "version": "3.3.16", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.16.tgz", - "integrity": "sha512-bzlKTyNJ7+LdGIIwy8ijFpIqEQIvafahV7eYykJ8Cvh42EdJeODoJ6gUJXpQJvej1BddH8OqTXZNE/KfbWAu8Q==", + "version": "3.3.18", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.18.tgz", + "integrity": "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w==", "dev": true, "funding": [ { diff --git a/website/package-lock.json b/website/package-lock.json index dd061ab..5113208 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -5263,9 +5263,9 @@ "license": "MIT" }, "node_modules/nanoid": { - "version": "3.3.16", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.16.tgz", - "integrity": "sha512-bzlKTyNJ7+LdGIIwy8ijFpIqEQIvafahV7eYykJ8Cvh42EdJeODoJ6gUJXpQJvej1BddH8OqTXZNE/KfbWAu8Q==", + "version": "3.3.18", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.18.tgz", + "integrity": "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w==", "funding": [ { "type": "github",