Files
core/test/deno/shutdown.test.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

66 lines
2.9 KiB
TypeScript

// Real Deno integration test for the bounded close() serveDeno returns. app.close() cannot do this
// job on Deno — it returns at its `if (!server)` guard, because a Deno app is served through
// app.fetch and never through listen() — so the deadline lives on the transport that owns the
// handle. See issue #15.
//
// Run with: npm run test:deno
import 'reflect-metadata';
import { createApp, Route, Get, Module } from '../../src/index.ts';
import { serveDeno } from '../../src/deno.ts';
@Route('/')
class Ctl {
@Get('/hang')
hang() {
return new Promise(() => {}); // never resolves — a stuck handler
}
@Get('/slow')
async slow() {
await new Promise((r) => setTimeout(r, 200)); // finite, so draining has something real to wait out
return { ok: true };
}
}
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
const portOf = (server: { addr?: { port: number } }): number => server.addr!.port;
// On Deno the deadline bounds the wait, not the connections — see the comment on serveDeno's
// force callback. This test asserts exactly that: close() returns on time, and does so without
// the uncaught BadResource that aborting a draining server produces. The test runner fails on
// that error even when the assertions pass, so a green run here is the real assertion.
Deno.test('serveDeno close({ timeoutMs }) returns on a stuck handler without forcing', async () => {
const server = serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
fetch(`http://127.0.0.1:${portOf(server)}/hang`).catch(() => {});
await new Promise((r) => setTimeout(r, 50)); // let the request actually reach the handler
const start = Date.now();
await server.close({ timeoutMs: 300 });
const elapsed = Date.now() - start;
// The assertion that matters is the upper bound: without the deadline this never returns.
if (elapsed >= 2000) throw new Error(`close() took ${elapsed}ms — the deadline did not fire`);
if (elapsed < 250) throw new Error(`close() returned in ${elapsed}ms — it did not wait the deadline`);
});
Deno.test('serveDeno close() drains an in-flight request rather than cutting it', async () => {
const server = serveDeno(createApp({ modules: [M] }), { port: 0, onListen: () => {} });
const inFlight = fetch(`http://127.0.0.1:${portOf(server)}/slow`).then((r) => r.json());
await new Promise((r) => setTimeout(r, 50)); // must be *in* the handler before we close
await server.close({ timeoutMs: 5000 });
const body = await inFlight;
if (body.ok !== true) throw new Error(`in-flight request was cut: ${JSON.stringify(body)}`);
});
Deno.test('a caller-supplied signal still aborts the server', async () => {
const ac = new AbortController();
const server = serveDeno(createApp({ modules: [M] }), { port: 0, signal: ac.signal, onListen: () => {} });
ac.abort();
await server.finished; // hangs here if chaining the caller's signal into ours dropped it
});