@zukhruf/shutdown 0.8.48 → 0.8.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,6 +4,12 @@ A graceful shutdown for a Node.js program. When the user presses Ctrl-C, or Dock
|
|
|
4
4
|
|
|
5
5
|
The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
|
|
6
6
|
|
|
7
|
+
## Use it
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @zukhruf/shutdown
|
|
11
|
+
```
|
|
12
|
+
|
|
7
13
|
## Stop the work at a termination signal
|
|
8
14
|
|
|
9
15
|
Make one `Shutdown` at the start of the entry point, before the first `await`. Then a termination signal during the start also stops the work. Give the signal to the work:
|
|
@@ -26,7 +32,7 @@ The user presses Ctrl-C while the pipeline runs. Then:
|
|
|
26
32
|
|
|
27
33
|
SIGINT, SIGTERM and SIGHUP start a shutdown. SIGQUIT does not: Ctrl-\ keeps its default action, which ends the process and writes a core dump.
|
|
28
34
|
|
|
29
|
-
##
|
|
35
|
+
## Read what the parent sees
|
|
30
36
|
|
|
31
37
|
| Cause of the shutdown | Exit status |
|
|
32
38
|
| ---------------------------------------------- | ---------------------------------------------------- |
|
|
@@ -37,7 +43,7 @@ SIGINT, SIGTERM and SIGHUP start a shutdown. SIGQUIT does not: Ctrl-\ keeps its
|
|
|
37
43
|
|
|
38
44
|
An exit code of 130 is not the same as a death by SIGINT. A Bash script stops its loop only when the child died by SIGINT. In a terminal, `node app.js && next` runs `next` after Ctrl-C when `app.js` exits with 0. When `app.js` dies by SIGINT, zsh stops the whole line. A parent that calls `waitpid()` sees the difference too: Node.js gives `signal: 'SIGINT'`, not `code: 130`.
|
|
39
45
|
|
|
40
|
-
##
|
|
46
|
+
## Handle a second stop request
|
|
41
47
|
|
|
42
48
|
The first stop request removes the termination signal listeners of the package. Thus a second termination signal gets the default action, and the process ends at once. Use this when the cleanup does not end. For example, a read from a pipe that no program writes waits in the thread pool of Node.js, and no signal can stop it. The second Ctrl-C ends the process.
|
|
43
49
|
|
|
@@ -51,7 +57,7 @@ The package has no timer. The supervisor owns the deadline of a shutdown:
|
|
|
51
57
|
| systemd | 90 s, then SIGKILL (`TimeoutStopSec`) |
|
|
52
58
|
| Nx | 5 s, then SIGKILL (`NX_PROCESS_KILL_GRACE_PERIOD`) |
|
|
53
59
|
|
|
54
|
-
##
|
|
60
|
+
## Handle a crash
|
|
55
61
|
|
|
56
62
|
A crash is an exception that no code catches, or a rejection that no code handles. Without the package, Node.js writes the error and ends the process at once. With the package, a crash is a stop request:
|
|
57
63
|
|
|
@@ -91,17 +97,21 @@ With `init: true`, tini is PID 1. The program dies by the signal, and tini gives
|
|
|
91
97
|
The package does not stop a server. Node.js does most of it. An `http.Server` is `AsyncDisposable`, and `server.close()` stops new connections and closes the idle ones:
|
|
92
98
|
|
|
93
99
|
```ts
|
|
94
|
-
import {
|
|
100
|
+
import { once } from 'node:events';
|
|
95
101
|
|
|
96
102
|
import { Shutdown } from '@zukhruf/shutdown';
|
|
97
103
|
|
|
98
104
|
using shutdown = new Shutdown();
|
|
99
105
|
await using server = serve({ fetch: app.fetch, port });
|
|
100
|
-
|
|
106
|
+
if (!shutdown.signal.aborted) {
|
|
107
|
+
await once(shutdown.signal, 'abort');
|
|
108
|
+
}
|
|
101
109
|
setTimeout(() => server.closeAllConnections(), 1_000).unref();
|
|
102
110
|
```
|
|
103
111
|
|
|
104
|
-
`
|
|
112
|
+
Check `signal.aborted` before the wait. A signal that aborted before the call emits no `'abort'` event again, so `events.once(signal, 'abort')` alone waits forever.
|
|
113
|
+
|
|
114
|
+
Do not wait with `util.aborted(signal, resource)` on a resource that nothing else holds. `util.aborted()` holds its resource weakly: when a garbage collection takes the resource before the signal aborts, the promise never settles ([Node.js docs](https://nodejs.org/api/util.html#utilabortedsignal-resource)). In #3929, the websets app of context-compiler waited with `util.aborted(ends, collections)`, and after a garbage collection took the array, the process stayed alive until a second SIGINT. The fix waits on the signal itself (context-compiler commit `2d8c016`). The recipe [Stop an HTTP server at a termination signal](./docs/recipes/stop-an-http-server-at-a-termination-signal.md) shows a full program.
|
|
105
115
|
|
|
106
116
|
A probe on Node.js 26.10.0 compared this with `stoppable`, which `@godaddy/terminus` uses:
|
|
107
117
|
|
|
@@ -124,6 +134,21 @@ Node.js keeps a connection that becomes idle after the call to `close()` until `
|
|
|
124
134
|
- A choice of termination signals. A program that must live on after its terminal closes runs under a supervisor or in tmux, not under `nohup`: a listener for SIGHUP replaces the ignore of `nohup`.
|
|
125
135
|
- Windows. Windows has no POSIX signals.
|
|
126
136
|
|
|
137
|
+
## Who uses it
|
|
138
|
+
|
|
139
|
+
Each consumer below is an entry point that makes one `Shutdown`. The package itself is backlog #3579.
|
|
140
|
+
|
|
141
|
+
| Consumer | How | Record |
|
|
142
|
+
| -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
|
|
143
|
+
| context-compiler: logful (`apps/logful/src/main.ts`) | `pipeline.run({ signal: shutdown.signal })`. It replaced an `AbortController` for the termination signals that exited with code 0. | #3580, context-compiler commit `9ea2c89` |
|
|
144
|
+
| context-compiler: startup-credits | `AbortSignal.any([shutdown.signal, failed.signal])` is the signal of its loops. It throws the failure when only `failed` aborted. It replaced the same `AbortController`. | #3580, context-compiler commit `9ea2c89` |
|
|
145
|
+
| context-compiler: what-is-going-on | `await aborted(shutdown.signal, server)`, then `server.close()`. It replaced the same `AbortController`. | #3580, context-compiler commit `9ea2c89` |
|
|
146
|
+
| context-compiler: the MCP server of the Codex plugin | `new Shutdown()` for the life of the process, with no dispose. A listener for `'abort'` on `shutdown.signal` calls `mcpServer.close()`. It replaced the same `AbortController`. | #3580, context-compiler commit `9ea2c89` |
|
|
147
|
+
| context-compiler: websets | `AbortSignal.any([shutdown.signal, failed.signal])` is the signal of its queue jobs and its loops. `await aborted(watching, server)`, then `server.close()`. | context-compiler commit `e9350c6`, #3929 |
|
|
148
|
+
| context-compiler: ambient (`apps/ambient/src/server.ts`) | `Promise.race([aborted(shutdown.signal, server), observer.failure])`, then `server.close()` in a `finally` block. | context-compiler commit `673c1ae` |
|
|
149
|
+
| context-compiler: ipay | `pipeline.watch({ signal: shutdown.signal })`. | context-compiler commit `e30e754` |
|
|
150
|
+
| elt: the proactive app | `pipeline.run({ signal: shutdown.signal })` and `automations.run(shutdown.signal)`. Its `await using` stack closes the Postgres clients before the process dies by the signal. It replaced an `AbortController`. | #3581, elt commit `aed72903` |
|
|
151
|
+
|
|
127
152
|
## The same concepts in other systems
|
|
128
153
|
|
|
129
154
|
| Concept | Other systems |
|
|
@@ -171,13 +196,3 @@ export class Shutdown implements Disposable {
|
|
|
171
196
|
- [ADR 0001](./docs/adr/0001-the-process-dies-by-its-termination-signal-after-the-cleanup.md): the process dies by its termination signal after the cleanup.
|
|
172
197
|
- [ADR 0002](./docs/adr/0002-only-the-first-stop-request-starts-a-shutdown.md): only the first stop request starts a shutdown.
|
|
173
198
|
- [ADR 0003](./docs/adr/0003-a-crash-starts-a-shutdown.md): a crash starts a shutdown.
|
|
174
|
-
|
|
175
|
-
## Who uses each part
|
|
176
|
-
|
|
177
|
-
The consumers below come from the search of 2026-11-02. None of them dies by the termination signal today. The package itself is backlog mutex #3579.
|
|
178
|
-
|
|
179
|
-
| Consumer | Uses | Backlog |
|
|
180
|
-
| ------------------------------------------------------------------------ | ----------------------------- | ---------------------- |
|
|
181
|
-
| context-compiler: logful, startup-credits and its eval, what-is-going-on | `signal`, the exit status | context-compiler #3580 |
|
|
182
|
-
| context-compiler: the MCP server of the Codex plugin | `signal`, to close the server | context-compiler #3580 |
|
|
183
|
-
| elt: the proactive app | `signal`, the exit status | elt #3581 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/shutdown",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.49",
|
|
4
4
|
"description": "A graceful shutdown for a Node.js program: SIGINT, SIGTERM, SIGHUP and crashes abort one AbortSignal, the program runs its cleanup, and then the process dies by the termination signal, so a shell, Docker or Kubernetes sees an interrupted process.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@zukhruf/docker": "^0.8.
|
|
45
|
+
"@zukhruf/docker": "^0.8.49"
|
|
46
46
|
}
|
|
47
47
|
}
|