Files
core/src/http/shutdown.ts
T
chrnx 586e14da72 feat(runtimes): a bounded close() on the Deno and Bun adapters
Closes #15, taking the second of the two answers it framed: app.close() stays
Node-only, and each transport that owns a handle offers the same close({
timeoutMs }). app.close() returns at its `if (!server)` guard on Deno and Bun
because they serve through app.fetch and never through listen(), and only the
transport can drain what it accepted. The warning there now names the
replacement instead of pointing at this issue.

The runtimes do not agree, and the code says so rather than averaging them:

Bun escalates. stop(false) drains, stop(true) forces, and the deadline moves
between them like Node's closeAllConnections().

Deno cannot. Its two levers are mutually exclusive — aborting the serve signal
while shutdown() is pending throws BadResource from Deno's own listener,
uncaught and fatal under deno test. Measured on 2.9: abort alone is clean,
abort after shutdown is not. So on Deno the deadline bounds how long close()
waits, not when connections die, and both the type and the timeout warning say
that outright. Silently accepting an option that does less is the failure mode
AGENTS.md rules out.

closeWithDeadline is shared so the two cannot drift into different meanings of
the same timeoutMs, and it resolves on its own timer rather than awaiting the
force call — Bun's stop(true) was measured waiting out a handler with a pending
timer, which is exactly the unbounded wait the deadline exists to prevent.

serveDeno now owns the AbortController it serves with, chaining any caller
signal into it so neither party loses control; a test covers that. The Bun
ambient said stop() returns void, which was simply wrong.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-08-16 13:16:24 -05:00

51 lines
2.1 KiB
TypeScript

/**
* Bounds a graceful shutdown by a deadline, forcing whatever is left.
*
* Shared by the Deno and Bun adapters so the two cannot drift into different meanings of the same
* `timeoutMs`, and shaped like the Node path in `closeApp`: try to drain, and if the deadline
* passes first, force the remainder and resolve anyway.
*
* The race is the point, and it is not paranoia. Both runtimes' force calls were measured against
* a handler that never resolves and returned immediately, but `Bun.stop(true)` was also measured
* waiting out a handler with a pending timer — same call, ~2s, because it awaits pending async
* work rather than only open connections. Awaiting the force call would therefore inherit exactly
* the unbounded wait the deadline exists to prevent. Resolving on our own timer means `close()`
* returns within `timeoutMs` whatever either runtime decides to wait for.
*
* @param graceful Drain in flight work. Unbounded — that is what the deadline is for.
* @param force Stop waiting and drop what is left. Fire-and-forget; its promise is not awaited.
*/
export function closeWithDeadline(
graceful: () => Promise<void>,
force: () => void,
timeoutMs: number,
onTimeout?: (timeoutMs: number) => void,
): Promise<void> {
return new Promise<void>((resolve) => {
let settled = false;
// Armed before `finish` exists, and deliberately: setTimeout cannot invoke its callback
// synchronously, so `finish` is always assigned by the time this can reach it. The reverse
// ordering would rest on when the runtime's own promise settles. Mirrors `closeApp`.
const timer = setTimeout(() => {
if (settled) return;
onTimeout?.(timeoutMs);
force();
finish();
}, timeoutMs);
timer.unref?.();
const finish = (): void => {
if (settled) return;
settled = true;
clearTimeout(timer);
resolve();
};
// A graceful path that rejects still has to end the shutdown: the deadline covers a slow
// close, not a broken one, and leaving close() pending forever is the worse failure.
void graceful().then(finish, finish);
});
}