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>
This commit is contained in:
chrnx committed 2026-08-16 13:16:24 -05:00
1 parent 085eb9a67d
commit 586e14da72
6 files changed
+256 -7

No files matched your search

+32 -3
View File
@@ -3,6 +3,7 @@ import type { Channel } from './channel';
import type { App } from './app/types';
import type { WsSocket, WsRequest } from './http/ws-core';
import { decodeMessage, neutralSocket, toWsRequest, upgradeSafely } from './http/ws-adapter';
import { closeWithDeadline } from './http/shutdown';
// Minimal ambient for the Bun globals we use — avoids a @types/bun devDep.
// Mirrors the real Bun.serve / server.upgrade / ServerWebSocket signatures we call.
@@ -21,10 +22,25 @@ interface BunWebSocketHandler {
}
interface BunServer {
port: number;
stop(closeActiveConnections?: boolean): void;
// Returns a promise, verified on Bun 1.3 — the previous `void` here was simply wrong, and it
// hid the fact that this is awaitable at all.
stop(closeActiveConnections?: boolean): Promise<void>;
upgrade(request: Request, options?: { data?: unknown; headers?: Record<string, string> }): boolean;
requestIP(request: Request): { address: string; family: string; port: number } | null;
}
/**
* What {@link serveBun} hands back: Bun's own server, plus the bounded `close()` that
* `app.close()` cannot provide here. `app.close()` returns at its `if (!server)` guard on Bun,
* because a Bun app is served through `app.fetch` and never through `listen()`.
*/
export interface BunServeResult extends BunServer {
/**
* Drains in-flight requests, then force-closes whatever is left after `timeoutMs` (default:
* 10s). Same meaning as `app.close({ timeoutMs })` on Node.
*/
close(options?: { timeoutMs?: number }): Promise<void>;
}
interface BunServeOptions {
port?: number;
hostname?: string;
@@ -59,8 +75,8 @@ function bunSocket(ws: BunServerWebSocket): WsSocket {
* Bun.serve exposes no equivalent to Node's server.maxConnections; enforce connection caps at
* the deployment platform or reverse proxy.
*/
export function serveBun(app: App, options?: BunServeShortOptions): BunServer {
return Bun.serve({
export function serveBun(app: App, options?: BunServeShortOptions): BunServeResult {
const server = Bun.serve({
...options,
fetch(request, server) {
if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
@@ -97,4 +113,17 @@ export function serveBun(app: App, options?: BunServeShortOptions): BunServer {
},
},
});
// Object.assign rather than a fresh object: Bun.serve returns more than the ambient above
// declares (reload, ref, unref, …), and rebuilding would silently drop whatever we did not name.
return Object.assign(server, {
close: (closeOptions: { timeoutMs?: number } = {}): Promise<void> =>
closeWithDeadline(
() => server.stop(false),
() => void server.stop(true),
closeOptions.timeoutMs ?? 10_000,
(ms) =>
console.warn(`[green-tea] graceful shutdown timed out after ${ms}ms — forcing remaining connections closed`),
),
});
}