feat(shutdown): opt into SIGINT/SIGTERM with createApp({ handleSignals })
The README promised shutdown was *"an extension point, not a SIGTERM handler you
write"*. Half of it was true: `dispose()`, `onShutdown` and `hooks` all deliver the
*content* of teardown, but nothing called `close()`, so every application still wrote
the handler — and wrote it three times, because the spelling is per runtime. Forget it
and the container is SIGKILLed after its grace period, every `dispose()` the registry
so carefully ordered never runs, and nothing reports it.
`createApp({ handleSignals: true })` registers SIGINT/SIGTERM to close and exit.
Off by default, and that is the design rather than caution: a library that installs
process-wide handlers behind your back is worse than one that installs none, because
when the process exits is the application's call. Both halves are now supported and
documented — keep the handler, or hand it over. What is not optional either way is
that *something* calls `close()`, and the README now says so instead of implying the
framework already had it covered.
Declared once on `createApp`, wired per runtime by whichever boot call runs:
`listen()`, `serveDeno()` and `serveBun()` each pass the closer that drains *their*
server to `app.handleSignals`, which returns a close that also unregisters. On Deno
that unregistering is load-bearing rather than tidy — a live `Deno.addSignalListener`
holds the process open, so an app that closed itself would otherwise hang.
Unregistering first also means a *second* signal falls through to the platform default
and ends the process at once. That is deliberate: Ctrl-C twice is the way out of a
teardown that is stuck, and swallowing it for the length of a 10s budget would read as
a hang. An orchestrator sends SIGTERM once and then SIGKILL, so it never gets there.
Tested against real processes, because a handler whose job is to exit its own process
cannot be fired by hand in the runner: Node covers SIGTERM, SIGINT, the double-signal
escape hatch and the default-off case; Deno covers `Deno.addSignalListener`/`Deno.exit`,
which is the API that actually differs. Bun shares Node's `process` path.
Closes #70.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
12 files changed
+445
-7
No files matched your search
@@ -0,0 +1,53 @@
|
||||
// `createApp({ handleSignals: true })` under the real Deno runtime.
|
||||
//
|
||||
// Deno is the runtime this option exists for. Node and Bun share `process.on`/`process.exit`; Deno
|
||||
// has `Deno.addSignalListener`/`Deno.exit`, and its listeners hold the process open until removed —
|
||||
// so an app that closed itself and forgot to unregister would hang here and nowhere else.
|
||||
//
|
||||
// Run with: npm run test:deno
|
||||
import { assert, assertEquals } from 'jsr:@std/assert';
|
||||
|
||||
const HARNESS = 'test/interop/_signal-app-deno.ts';
|
||||
|
||||
async function run(): Promise<{ code: number; lines: string[] }> {
|
||||
const child = new Deno.Command('deno', {
|
||||
args: ['run', '--allow-all', '--no-check', HARNESS],
|
||||
stdout: 'piped',
|
||||
stderr: 'inherit',
|
||||
}).spawn();
|
||||
|
||||
const lines: string[] = [];
|
||||
let buffered = '';
|
||||
let signalled = false;
|
||||
|
||||
const reader = child.stdout.pipeThrough(new TextDecoderStream()).getReader();
|
||||
const pump = (async () => {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
buffered += value;
|
||||
const parts = buffered.split('\n');
|
||||
buffered = parts.pop() ?? '';
|
||||
for (const line of parts.filter(Boolean)) {
|
||||
lines.push(line);
|
||||
if (!signalled && line.includes('"ready"')) {
|
||||
signalled = true;
|
||||
child.kill('SIGTERM');
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
const status = await child.status;
|
||||
await pump;
|
||||
return { code: status.code, lines };
|
||||
}
|
||||
|
||||
Deno.test('SIGTERM runs teardown and exits 0 through Deno.addSignalListener', async () => {
|
||||
const { code, lines } = await run();
|
||||
assert(
|
||||
lines.some((l) => l.includes('"disposed"')),
|
||||
`dispose() never ran — got: ${lines.join(' | ')}`,
|
||||
);
|
||||
assertEquals(code, 0);
|
||||
});
|
||||
Reference in new issue
Block a user