feat: shutdown teardown for plugins, providers and applications
Closing a connection meant writing process.on('SIGTERM') by hand: closeApp
drained the server and mesh links and knew nothing about the graph, and the
Bus had no shutdown event at all, so a plugin could not even observe that the
app was going away.
Implements docs/plans/2026-08-17-shutdown-teardown-design.md, D1-D8.
Three doors into one registry, with one order and one failure policy — a
@Provider may declare dispose(), a plugin may call api.onShutdown(fn), and an
application that wants neither passes createApp({ hooks: [{ onShutdown }] }).
Shipping only the plugin half would have frozen the public API around the less
common case: the thing that holds a database connection is a provider.
Callbacks take no arguments. Whatever needs closing is already in the closure
that registered it, so nothing crosses a scope and the registry needs no
container, app reference or transport. That is what keeps this a registry
rather than a second event bus — Bus.on is synchronous and emit swallows
failures on purpose, and a teardown needs the opposite of both.
Provider dispose() is registered as the provider boots rather than as it is
collected, so reverse-registration is reverse-topological order for free: a
cache that needs db closes before the db it is holding, with no second sort to
keep in step. It registers inside the try, so a provider that failed to boot is
never disposed — nothing it might close was ever opened.
Teardown runs after the drain, because an in-flight request may still be using
what a teardown is about to close, and inside the same timeoutMs, because that
number is a promise about when close() returns. teardownTimeoutMs reserves a
slice of that budget rather than extending it, and is rejected at boot rather
than clamped.
Node, Deno and Bun reach it through the close() each runtime actually uses.
app.close() returns at its no-server guard everywhere but Node, so the Deno and
Bun adapters chain it after their own drain — and app.close() now runs teardown
on that path too, since an app served through app.fetch still booted providers
that opened things. The registry drains once, so both entry points are safe.
Edge gets a doc comment and nothing else. workerd has no shutdown to intercept;
a best-effort call on an event that does not exist would be worse than saying so.
Recorded in mountPlugin's comment while widening it: "isolation is structural"
is a guarantee about the registration surface, not about the request context. A
plugin's step already receives the live context and can overwrite keys in it.
Pre-existing, unrelated, and now written where the next reader will look.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
16 files changed
+670
-25
No files matched your search
+21
-4
@@ -1,4 +1,5 @@
|
||||
import { Bus } from './bus';
|
||||
import type { TeardownFn } from './lifecycle';
|
||||
|
||||
/** A step or provider a plugin contributes to the graph. */
|
||||
export interface ScopeNode {
|
||||
@@ -13,18 +14,34 @@ export interface ScopeNode {
|
||||
export interface ScopeApi {
|
||||
add(node: ScopeNode): void;
|
||||
}
|
||||
/** The capabilities handed to a plugin: event subscription and node registration only. */
|
||||
/** The capabilities handed to a plugin: event subscription, node registration, and teardown. */
|
||||
export interface PluginApi {
|
||||
bus: { on: Bus['on'] };
|
||||
scope: ScopeApi;
|
||||
/**
|
||||
* Run something before the app closes — closing a pool, stopping a timer, flushing a buffer.
|
||||
*
|
||||
* Takes no arguments: whatever needs closing is already in the closure of the plugin that opened
|
||||
* it. It is awaited, unlike a `bus.on` listener, and a failure is logged rather than swallowed.
|
||||
*/
|
||||
onShutdown(fn: TeardownFn): void;
|
||||
}
|
||||
/** A plugin: a function that wires itself up through the provided PluginApi. */
|
||||
export type Plugin = (api: PluginApi) => void;
|
||||
|
||||
/** Runs a plugin against a restricted API, then emits `plugin:mounted`. */
|
||||
export function mountPlugin(plugin: Plugin, bus: Bus, scope: ScopeApi): void {
|
||||
// Hand the plugin ONLY on() and add(). No emit, no other scope. Isolation is structural.
|
||||
const api: PluginApi = { bus: { on: bus.on.bind(bus) }, scope };
|
||||
export function mountPlugin(plugin: Plugin, bus: Bus, scope: ScopeApi, onShutdown: (fn: TeardownFn) => void): void {
|
||||
// Hand the plugin ONLY on(), add() and onShutdown(). No emit, no other scope, no container.
|
||||
//
|
||||
// "Isolation is structural" means this list and nothing else — it is a guarantee about the
|
||||
// *registration surface*, not about the request context. A plugin's step receives the live
|
||||
// context like any other step and can overwrite keys in it (`src/pipeline.ts`, `Object.assign`),
|
||||
// which is a separate, pre-existing question this list has never covered.
|
||||
//
|
||||
// `onShutdown` is the first capability here the framework must *wait* for: nothing a plugin did
|
||||
// before could delay the process. It cannot delay it without bound — `close()`'s deadline still
|
||||
// caps the whole shutdown.
|
||||
const api: PluginApi = { bus: { on: bus.on.bind(bus) }, scope, onShutdown };
|
||||
plugin(api);
|
||||
bus.emit('plugin:mounted', { name: plugin.name || 'anonymous' });
|
||||
}
|
||||
Reference in new issue
Block a user