Merge pull request 'Merge contrib: app.close() timeout from an outside contributor' (#38) from contrib into develop
This commit was merged in pull request #38.
This commit is contained in:
commit
2e4712788b
3 files changed
+77
-14
No files matched your search
+55
-13
@@ -260,19 +260,7 @@ export function createApp(opts: {
|
||||
return server;
|
||||
};
|
||||
|
||||
const close = (): Promise<void> =>
|
||||
new Promise<void>((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<void> => 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<void> {
|
||||
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) {
|
||||
|
||||
+1
-1
@@ -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<http.Server>;
|
||||
close(): Promise<void>;
|
||||
close(options?: { timeoutMs?: number }): Promise<void>;
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -464,6 +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
|
||||
}
|
||||
}
|
||||
@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', () => {
|
||||
|
||||
Reference in new issue
Block a user