cliprobe 1.2.1 → 1.4.0
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 +40 -10
- package/dist/cli.js +412 -184
- package/package.json +1 -1
- package/schema.json +8 -1
package/README.md
CHANGED
|
@@ -93,7 +93,9 @@ The other checks cover the rest of what goes wrong between a program and the thi
|
|
|
93
93
|
- [Stability](#stability)
|
|
94
94
|
- [Checks](#checks): [closed-pipe](#closed-pipe), [color](#color), [pipe-escapes](#pipe-escapes), [stdin](#stdin), [ctrl-c](#ctrl-c), [sigterm](#sigterm), [restore](#restore), [control-chars](#control-chars), [info checks](#info-checks)
|
|
95
95
|
- [Fixing a failure in a Node.js CLI](#fixing-a-failure-in-a-nodejs-cli)
|
|
96
|
+
- [Agent skill](#agent-skill)
|
|
96
97
|
- [Safety](#safety)
|
|
98
|
+
- [FAQ](#faq)
|
|
97
99
|
- [Contributing](#contributing)
|
|
98
100
|
- [License](#license)
|
|
99
101
|
|
|
@@ -283,22 +285,24 @@ In the census of the top 100 npm CLIs, 9 of 238 `--help` and `--version` runs fa
|
|
|
283
285
|
|
|
284
286
|
cliprobe lets the command run for 1 second, then sends SIGINT to its process group, which is what a terminal does when you press Ctrl-C, and gives it 3 seconds to exit. stdin is left open, as a terminal leaves it, so a server that stops at the end of stdin (`esbuild --serve`) keeps running until the Ctrl-C. Results:
|
|
285
287
|
|
|
286
|
-
| Verdict | Meaning
|
|
287
|
-
| ------- |
|
|
288
|
-
| `PASS` | Stopped by SIGINT, exited 130 (the convention), or exited with another failure code
|
|
289
|
-
| `FAIL` | Still running 3 seconds later, or printed a stack trace or traceback after the Ctrl-C
|
|
290
|
-
| `WARN` | Exited 0, so a script or an agent can't tell the work was interrupted
|
|
291
|
-
| `SKIP` | Finished before the Ctrl-C, as `--version` and `--help` do: probe a command that keeps running
|
|
288
|
+
| Verdict | Meaning |
|
|
289
|
+
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
290
|
+
| `PASS` | Stopped by SIGINT, exited 130 (the convention), or exited with another failure code |
|
|
291
|
+
| `FAIL` | Still running 3 seconds later, or printed a stack trace or traceback after the Ctrl-C |
|
|
292
|
+
| `WARN` | Exited 0, so a script or an agent can't tell the work was interrupted, or exited but left processes running that ignored the Ctrl-C (the evidence names the first) |
|
|
293
|
+
| `SKIP` | Finished before the Ctrl-C, as `--version` and `--help` do: probe a command that keeps running |
|
|
292
294
|
|
|
293
295
|
A stack trace printed before the Ctrl-C doesn't count. Python's default `KeyboardInterrupt` traceback is the classic failure here, so `python3 -c 'import time; time.sleep(30)'` fails. Across 42 watch modes, dev servers and long tasks run offline (vite, webpack, tsc, jest, Next.js, Nuxt, Astro, wrangler, nodemon and more), every one stopped within 60 ms of the Ctrl-C, whether it came after 1, 3 or 6 seconds, so the timing rarely matters. 13 of them exit 0 on purpose, among them `tsc --watch`, webpack, `next dev` and `nuxt dev`: that is a warn, not a fail.
|
|
294
296
|
|
|
297
|
+
The verdict is about the program itself: it passes once it exits, even if a child still holds its output open. What it leaves running gets 1 more second to follow, then is listed and killed. A process still there ignored the Ctrl-C, as a shell's background job does (`sh -c 'worker & server'`), so it would outlive the terminal's Ctrl-C too: that is a warn, added in 1.4.
|
|
298
|
+
|
|
295
299
|
### sigterm
|
|
296
300
|
|
|
297
|
-
`kill <pid>`, `docker stop`, systemd, pm2 and CI runners that cancel a job send SIGTERM to the program alone, not to its process group as a terminal sends Ctrl-C. So a program that started children (a dev server, a worker pool, a shell running a command) has to stop them itself. If it exits without doing so, they keep running with nobody to stop them, holding their ports and files. cliprobe lets the command run for 1 second, sends SIGTERM to it alone, and gives it 3 seconds to exit and 1 more second for its children to follow, then lists what is still running in its process group and
|
|
301
|
+
`kill <pid>`, `docker stop`, systemd, pm2 and CI runners that cancel a job send SIGTERM to the program alone, not to its process group as a terminal sends Ctrl-C. So a program that started children (a dev server, a worker pool, a shell running a command) has to stop them itself. If it exits without doing so, they keep running with nobody to stop them, holding their ports and files. cliprobe lets the command run for 1 second, sends SIGTERM to it alone, and gives it 3 seconds to exit and 1 more second for its children to follow, then lists what is still running and kills it: in its process group, and the processes it started in a group or session of their own (Node's `detached`, `setsid`, daemons). stdin is left open, as for ctrl-c. Results:
|
|
298
302
|
|
|
299
303
|
| Verdict | Meaning |
|
|
300
304
|
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
301
|
-
| `PASS` | Exited, by the signal or with any exit code, and left nothing running
|
|
305
|
+
| `PASS` | Exited, by the signal or with any exit code, and left nothing running |
|
|
302
306
|
| `WARN` | Exited but left processes running (the evidence names the first), still running 3 seconds later, or printed a stack trace after the SIGTERM |
|
|
303
307
|
| `SKIP` | Finished before the SIGTERM, as `--version` and `--help` do: probe a command that keeps running |
|
|
304
308
|
|
|
@@ -417,15 +421,41 @@ const shown = name.replace(
|
|
|
417
421
|
|
|
418
422
|
Also escape U+2028, U+2029 and the bidi controls (U+202A to U+202E, U+2066 to U+2069) if names may come from untrusted sources.
|
|
419
423
|
|
|
424
|
+
## Agent skill
|
|
425
|
+
|
|
426
|
+
An official [Agent Skill](https://agentskills.io) lets coding agents such as Claude Code, Codex, Gemini CLI and OpenCode use cliprobe on the CLIs they build or review: run every check with the right probes, report each failure with its evidence, propose the fix, and add a CI step. The agents run cliprobe and read its JSON report instead of guessing.
|
|
427
|
+
|
|
428
|
+
```sh
|
|
429
|
+
npx skills add KhaledSaeed18/cliprobe --skill cliprobe
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
Then ask in your own words, for example "check my CLI for pipe and Ctrl-C bugs before I publish it". See [skills/README.md](https://github.com/KhaledSaeed18/cliprobe/blob/main/skills/README.md) for other ways to install it and the evals.
|
|
433
|
+
|
|
420
434
|
## Safety
|
|
421
435
|
|
|
422
|
-
cliprobe runs the target in your current directory without a shell, with stdin closed (`/dev/null`; the stdin, ctrl-c and sigterm checks use a pipe that they never write to), a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. By default cliprobe passes only the arguments you give, after the command or with `--probe`, or `--version` and `--help`, or none with `--bare`; only the baseline check, when you name it, adds an unknown flag. The only input it ever sends is Ctrl-C, from the ctrl-c and restore checks, and SIGTERM, from the sigterm check, to probes you write. It kills each run's whole process group on timeout, including under a pseudo-terminal, and kills whatever
|
|
436
|
+
cliprobe runs the target in your current directory without a shell, with stdin closed (`/dev/null`; the stdin, ctrl-c and sigterm checks use a pipe that they never write to), a temporary `HOME`, and a minimal environment (no tokens from your shell). It still executes the program, so only probe programs you trust. By default cliprobe passes only the arguments you give, after the command or with `--probe`, or `--version` and `--help`, or none with `--bare`; only the baseline check, when you name it, adds an unknown flag. The only input it ever sends is Ctrl-C, from the ctrl-c and restore checks, and SIGTERM, from the sigterm check, to probes you write. It kills each run's whole process group on timeout, including under a pseudo-terminal, and when a program exits it kills whatever it left running, in its group or started in another one, in every check. If cliprobe itself is stopped by Ctrl-C, SIGTERM or SIGHUP, it first kills the probe it is running and removes its temporary directories, then exits by the same signal (130, 143 or 129). It runs on macOS and Linux, including Alpine; the terminal checks need the `script` command, which Alpine ships in `util-linux-misc`. Windows isn't supported yet.
|
|
437
|
+
|
|
438
|
+
## FAQ
|
|
439
|
+
|
|
440
|
+
**Why were ctrl-c, sigterm and control-chars skipped?** They need probes that a plain `--version` and `--help` run doesn't have: ctrl-c and sigterm need a command that keeps running, such as a server or a watcher, and control-chars needs one that prints file names from `{fixture}`. Add them with `--probe`, so one run covers every check: `npx cliprobe --probe "serve --port 0" --probe "list {fixture}" -- my-cli`.
|
|
441
|
+
|
|
442
|
+
**My program works when I run it, so why does a check fail?** cliprobe sets up conditions that are rare by hand: stdout closed before the program starts, output into a pipe, stdin that nobody writes to, a minimal environment. Reproduce the failure the way the check's section shows, for example `my-cli --help | head -n 1` for closed-pipe or `my-cli --help | cat -v` for color, and read the evidence in `--json`. If you still believe the verdict is wrong, [open a wrong verdict issue](https://github.com/KhaledSaeed18/cliprobe/issues/new?template=wrong_verdict.yml).
|
|
443
|
+
|
|
444
|
+
**Why does my program behave differently under cliprobe?** It runs without your shell's variables or your home directory: the environment has only `PATH`, `LANG`, `TERM`, `CI` and a temporary `HOME` and `TMPDIR`. A program that needs a token or a config file from your home directory sees neither, as on a fresh CI runner.
|
|
445
|
+
|
|
446
|
+
**Does a pass mean my CLI has no terminal bugs?** No. Each check tests one behavior, on the probes that ran; a subcommand that wasn't probed wasn't checked. cliprobe reports evidence, not a score, so add a `--probe` for each command your users pipe or interrupt.
|
|
447
|
+
|
|
448
|
+
**Does it only work for Node.js programs?** No. It tests any command as a black box, whatever it is written in. Only the [fixes](#fixing-a-failure-in-a-nodejs-cli) are specific to Node.js.
|
|
449
|
+
|
|
450
|
+
**Is it safe to probe any program?** cliprobe runs the program with your privileges, so probing it is like running it. Probe untrusted programs inside a disposable container with networking off, for example `docker run --rm --network none`.
|
|
451
|
+
|
|
452
|
+
**Does it run on Windows?** Not yet. It runs on macOS and Linux, including Alpine. It may work under WSL, but that isn't tested.
|
|
423
453
|
|
|
424
454
|
## Contributing
|
|
425
455
|
|
|
426
456
|
Bug reports and pull requests are welcome. The most useful report is a verdict you believe is wrong: open a [wrong verdict issue](https://github.com/KhaledSaeed18/cliprobe/issues/new?template=wrong_verdict.yml) with the program, its version and the `--format json` report. For a bug in cliprobe itself, an idea for a new check or a gap in these docs, [choose an issue form](https://github.com/KhaledSaeed18/cliprobe/issues/new/choose).
|
|
427
457
|
|
|
428
|
-
Development needs macOS or Linux, Node.js 22.
|
|
458
|
+
Development needs macOS or Linux, Node.js 22.18 or later and pnpm (the version is pinned in `package.json`):
|
|
429
459
|
|
|
430
460
|
```sh
|
|
431
461
|
pnpm install
|
package/dist/cli.js
CHANGED
|
@@ -1,67 +1,205 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
-
import { execFileSync, spawn } from "node:child_process";
|
|
3
|
+
import { execFile, execFileSync, spawn } from "node:child_process";
|
|
4
4
|
import { accessSync, closeSync, constants, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
|
|
5
5
|
import { delimiter, dirname, join } from "node:path";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
|
-
// src/core/
|
|
7
|
+
// src/core/interrupt.ts
|
|
8
|
+
const cleanups = /* @__PURE__ */ new Set();
|
|
9
|
+
/** Registers `cleanup` until the returned function is called. */
|
|
10
|
+
function onInterrupt(cleanup) {
|
|
11
|
+
cleanups.add(cleanup);
|
|
12
|
+
return () => {
|
|
13
|
+
cleanups.delete(cleanup);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Runs every registered cleanup, newest first, so processes die before their directories go. */
|
|
17
|
+
function runInterruptCleanups() {
|
|
18
|
+
const pending = [...cleanups].reverse();
|
|
19
|
+
cleanups.clear();
|
|
20
|
+
for (const cleanup of pending) try {
|
|
21
|
+
cleanup();
|
|
22
|
+
} catch {}
|
|
23
|
+
}
|
|
24
|
+
/** The signals that stop cliprobe and how a shell reports each: 128 plus its number. */
|
|
25
|
+
const INTERRUPT_SIGNALS = /* @__PURE__ */ new Map([
|
|
26
|
+
["SIGHUP", 129],
|
|
27
|
+
["SIGINT", 130],
|
|
28
|
+
["SIGTERM", 143]
|
|
29
|
+
]);
|
|
8
30
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* stay in the group for good although they have exited.
|
|
31
|
+
* On SIGINT, SIGTERM or SIGHUP: cleans up, then dies by the same signal, so the
|
|
32
|
+
* shell or CI runner sees 130, 143 or 129 as for any other program.
|
|
12
33
|
*/
|
|
13
|
-
function
|
|
14
|
-
|
|
34
|
+
function handleInterrupts(target = process) {
|
|
35
|
+
const signals = [...INTERRUPT_SIGNALS.keys()];
|
|
36
|
+
for (const [signal, code] of INTERRUPT_SIGNALS) target.once(signal, () => {
|
|
37
|
+
runInterruptCleanups();
|
|
38
|
+
for (const s of signals) target.removeAllListeners(s);
|
|
39
|
+
try {
|
|
40
|
+
target.kill(target.pid, signal);
|
|
41
|
+
} catch {
|
|
42
|
+
target.exit(code);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
15
45
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
46
|
+
// src/core/group.ts
|
|
47
|
+
const isGone = (state) => state.startsWith("Z") || state.startsWith("X");
|
|
48
|
+
/** Fields of a /proc/<pid>/stat line; the name may hold spaces and parentheses. */
|
|
49
|
+
function parseProcStat(line) {
|
|
50
|
+
const open = line.indexOf("(");
|
|
51
|
+
const close = line.lastIndexOf(")");
|
|
52
|
+
const fields = line.slice(close + 1).trim().split(" ");
|
|
53
|
+
return {
|
|
54
|
+
name: line.slice(open + 1, close),
|
|
55
|
+
state: fields[0] ?? "",
|
|
56
|
+
ppid: Number(fields[1] ?? ""),
|
|
57
|
+
pgid: Number(fields[2] ?? ""),
|
|
58
|
+
start: fields[19] ?? ""
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** Every running process, without zombies, read from `<root>`. */
|
|
62
|
+
function tableFromProc(root = "/proc", keep = () => true) {
|
|
63
|
+
const table = [];
|
|
19
64
|
for (const entry of readdirSync(root)) {
|
|
20
65
|
if (!/^\d+$/.test(entry)) continue;
|
|
21
66
|
try {
|
|
22
67
|
const stat = parseProcStat(readFileSync(`${root}/${entry}/stat`, "utf8"));
|
|
23
|
-
if (stat.
|
|
68
|
+
if (isGone(stat.state) || !keep(stat)) continue;
|
|
24
69
|
const cmdline = readFileSync(`${root}/${entry}/cmdline`, "utf8").split("\0").filter(Boolean).join(" ");
|
|
25
|
-
|
|
70
|
+
table.push({
|
|
26
71
|
pid: Number(entry),
|
|
72
|
+
ppid: stat.ppid,
|
|
73
|
+
pgid: stat.pgid,
|
|
74
|
+
start: stat.start,
|
|
27
75
|
command: cmdline || stat.name
|
|
28
76
|
});
|
|
29
77
|
} catch {}
|
|
30
78
|
}
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
/** macOS and other systems without /proc: asks ps. */
|
|
34
|
-
function membersFromPs(pgid) {
|
|
35
|
-
return parsePs(execFileSync("ps", [
|
|
36
|
-
"-A",
|
|
37
|
-
"-o",
|
|
38
|
-
"pid=,pgid=,stat=,args="
|
|
39
|
-
], { encoding: "utf8" }), pgid);
|
|
79
|
+
return table.sort((a, b) => a.pid - b.pid);
|
|
40
80
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const members = [];
|
|
81
|
+
/** lstart is the only start time macOS ps gives; LC_ALL=C keeps its format fixed. */
|
|
82
|
+
const PS_ARGS = [
|
|
83
|
+
"-A",
|
|
84
|
+
"-o",
|
|
85
|
+
"pid=,ppid=,pgid=,lstart=,stat=,args="
|
|
86
|
+
];
|
|
87
|
+
const PS_ENV = {
|
|
88
|
+
...process.env,
|
|
89
|
+
LC_ALL: "C"
|
|
90
|
+
};
|
|
91
|
+
const PS_LINE = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\w{3} \w{3}\s+\d+ [\d:]{8} \d{4})\s+(\S+)\s+(.*)$/;
|
|
92
|
+
/** Running processes from `ps -A -o pid=,ppid=,pgid=,lstart=,stat=,args=` output. */
|
|
93
|
+
function parsePsTable(out) {
|
|
94
|
+
const table = [];
|
|
56
95
|
for (const line of out.split("\n")) {
|
|
57
|
-
const m =
|
|
58
|
-
if (!m ||
|
|
59
|
-
|
|
96
|
+
const m = PS_LINE.exec(line);
|
|
97
|
+
if (!m || isGone(m[5] ?? "")) continue;
|
|
98
|
+
table.push({
|
|
60
99
|
pid: Number(m[1]),
|
|
61
|
-
|
|
100
|
+
ppid: Number(m[2]),
|
|
101
|
+
pgid: Number(m[3]),
|
|
102
|
+
start: m[4] ?? "",
|
|
103
|
+
command: (m[6] ?? "").trim()
|
|
62
104
|
});
|
|
63
105
|
}
|
|
64
|
-
return
|
|
106
|
+
return table.sort((a, b) => a.pid - b.pid);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A snapshot of every running process, read at once: from /proc on Linux, from
|
|
110
|
+
* ps elsewhere. `platform` and `ps` are parameters so tests can take the ps path
|
|
111
|
+
* on Linux and see it fail.
|
|
112
|
+
*/
|
|
113
|
+
function processTable(platform = process.platform, ps = "ps") {
|
|
114
|
+
if (platform === "linux") return tableFromProc();
|
|
115
|
+
try {
|
|
116
|
+
return parsePsTable(execFileSync(ps, PS_ARGS, {
|
|
117
|
+
encoding: "utf8",
|
|
118
|
+
env: PS_ENV
|
|
119
|
+
}));
|
|
120
|
+
} catch {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** processTable() without blocking: on macOS, ps takes a few milliseconds. */
|
|
125
|
+
function processTableAsync(platform = process.platform, ps = "ps") {
|
|
126
|
+
if (platform === "linux") return Promise.resolve(tableFromProc());
|
|
127
|
+
return new Promise((resolve) => {
|
|
128
|
+
execFile(ps, PS_ARGS, {
|
|
129
|
+
encoding: "utf8",
|
|
130
|
+
env: PS_ENV
|
|
131
|
+
}, (error, out) => {
|
|
132
|
+
resolve(error ? [] : parsePsTable(out));
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* What one run started, followed by parent PID as well as by process group: a
|
|
138
|
+
* child in its own group or session (Node's `detached`, setsid, daemons) leaves
|
|
139
|
+
* the program's group, and once the program exits it is reparented, so only a
|
|
140
|
+
* snapshot taken while the program runs links it to the program.
|
|
141
|
+
*/
|
|
142
|
+
var ProcessTree = class {
|
|
143
|
+
#root;
|
|
144
|
+
#groups;
|
|
145
|
+
/** Descendants seen so far, by PID, with their start time. */
|
|
146
|
+
#seen = /* @__PURE__ */ new Map();
|
|
147
|
+
/**
|
|
148
|
+
* @param root the program's PID
|
|
149
|
+
* @param groups the process groups whose members belong to the run, read
|
|
150
|
+
* each time: the program's own, and the one under a pseudo-terminal
|
|
151
|
+
*/
|
|
152
|
+
constructor(root, groups) {
|
|
153
|
+
this.#root = root;
|
|
154
|
+
this.#groups = groups;
|
|
155
|
+
}
|
|
156
|
+
/** Once the program has exited and been reaped, its PID may belong to another process. */
|
|
157
|
+
programExited() {
|
|
158
|
+
this.#root = void 0;
|
|
159
|
+
}
|
|
160
|
+
/** Records the program's descendants from a snapshot taken while it runs. */
|
|
161
|
+
record(table) {
|
|
162
|
+
for (const p of this.running(table)) this.#seen.set(p.pid, p.start);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The run's processes in `table`: the program, the members of its groups,
|
|
166
|
+
* the descendants recorded earlier (the same PID and start time), and
|
|
167
|
+
* everything below any of them. cliprobe itself is never one of them.
|
|
168
|
+
*/
|
|
169
|
+
running(table) {
|
|
170
|
+
const groups = new Set(this.#groups());
|
|
171
|
+
const found = /* @__PURE__ */ new Map();
|
|
172
|
+
const candidates = table.filter((p) => p.pid !== 1 && p.pid !== process.pid);
|
|
173
|
+
for (const p of candidates) if (p.pid === this.#root || groups.has(p.pgid) || this.#seen.has(p.pid) && this.#seen.get(p.pid) === p.start) found.set(p.pid, p);
|
|
174
|
+
let grew = true;
|
|
175
|
+
while (grew) {
|
|
176
|
+
grew = false;
|
|
177
|
+
for (const p of candidates) if (!found.has(p.pid) && found.has(p.ppid)) {
|
|
178
|
+
found.set(p.pid, p);
|
|
179
|
+
grew = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return [...found.values()].sort((a, b) => a.pid - b.pid);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* SIGKILLs the given processes, the run's groups, and every group that one of
|
|
187
|
+
* the processes leads, so what they started after the snapshot goes too.
|
|
188
|
+
*/
|
|
189
|
+
function killAll(processes, groups) {
|
|
190
|
+
const targets = new Set(groups.map((g) => -g));
|
|
191
|
+
for (const p of processes) {
|
|
192
|
+
targets.add(p.pid);
|
|
193
|
+
if (p.pgid === p.pid) targets.add(-p.pid);
|
|
194
|
+
}
|
|
195
|
+
targets.delete(-process.pid);
|
|
196
|
+
targets.delete(process.pid);
|
|
197
|
+
targets.delete(-1);
|
|
198
|
+
targets.delete(1);
|
|
199
|
+
targets.delete(0);
|
|
200
|
+
for (const target of targets) try {
|
|
201
|
+
process.kill(target, "SIGKILL");
|
|
202
|
+
} catch {}
|
|
65
203
|
}
|
|
66
204
|
// src/core/sandbox.ts
|
|
67
205
|
/** A minimal environment: no tokens or config from the caller's shell. */
|
|
@@ -83,12 +221,21 @@ function sandbox() {
|
|
|
83
221
|
// src/core/run.ts
|
|
84
222
|
const STDERR_CAP = 65536;
|
|
85
223
|
const STDOUT_CAP = 1048576;
|
|
224
|
+
/** How often the process table is read while the program runs, to learn its descendants. */
|
|
225
|
+
const TREE_POLL_MS = 200;
|
|
226
|
+
/** How long to wait for the output pipes to close once everything the run started is gone. */
|
|
227
|
+
const CLOSE_WAIT_MS = 1e3;
|
|
228
|
+
/** How often to look for leftovers while waiting for them to finish. */
|
|
229
|
+
const LEFTOVER_POLL_MS = 50;
|
|
230
|
+
const delay = (ms) => new Promise((resolve) => {
|
|
231
|
+
setTimeout(resolve, ms);
|
|
232
|
+
});
|
|
86
233
|
/**
|
|
87
234
|
* Runs one invocation. With `closedStdout`, stdout is the write end of a FIFO
|
|
88
235
|
* whose read end is closed before the program starts, so its first write to
|
|
89
236
|
* stdout fails (EPIPE, or SIGPIPE for programs that keep the default handler).
|
|
90
237
|
*/
|
|
91
|
-
function run$1(command, args, { closedStdout = false, timeout, env: overrides = {},
|
|
238
|
+
function run$1(command, args, { closedStdout = false, timeout, env: overrides = {}, extraGroup, signal, stdin = "null", leftoversMs = 250 }) {
|
|
92
239
|
const { dir, env: base } = sandbox();
|
|
93
240
|
const env = { ...base };
|
|
94
241
|
for (const [key, value] of Object.entries(overrides)) if (value === void 0) delete env[key];
|
|
@@ -111,12 +258,30 @@ function run$1(command, args, { closedStdout = false, timeout, env: overrides =
|
|
|
111
258
|
let signalSentMs;
|
|
112
259
|
let stderrAtSignal;
|
|
113
260
|
let leftRunning;
|
|
114
|
-
let afterExit = Promise.resolve();
|
|
115
261
|
let exitMs;
|
|
262
|
+
let tree;
|
|
263
|
+
let pollTimer;
|
|
116
264
|
const start = performance.now();
|
|
265
|
+
const groups = () => {
|
|
266
|
+
const extra = extraGroup?.();
|
|
267
|
+
return [...child?.pid === void 0 ? [] : [child.pid], ...extra === void 0 ? [] : [extra]];
|
|
268
|
+
};
|
|
269
|
+
/** Kills everything the run started, from a fresh look at the process table. */
|
|
270
|
+
const killRun = () => {
|
|
271
|
+
if (tree) killAll(tree.running(processTable()), groups());
|
|
272
|
+
};
|
|
273
|
+
const unregister = onInterrupt(() => {
|
|
274
|
+
killRun();
|
|
275
|
+
rmSync(dir, {
|
|
276
|
+
recursive: true,
|
|
277
|
+
force: true
|
|
278
|
+
});
|
|
279
|
+
});
|
|
117
280
|
const finish = (outcome) => {
|
|
118
281
|
if (finished) return;
|
|
119
282
|
finished = true;
|
|
283
|
+
clearInterval(pollTimer);
|
|
284
|
+
unregister();
|
|
120
285
|
if (typeof stdoutFd === "number") closeSync(stdoutFd);
|
|
121
286
|
rmSync(dir, {
|
|
122
287
|
recursive: true,
|
|
@@ -156,44 +321,85 @@ function run$1(command, args, { closedStdout = false, timeout, env: overrides =
|
|
|
156
321
|
});
|
|
157
322
|
return;
|
|
158
323
|
}
|
|
324
|
+
const proc = child;
|
|
325
|
+
if (proc.pid !== void 0) {
|
|
326
|
+
tree = new ProcessTree(proc.pid, groups);
|
|
327
|
+
let polling = false;
|
|
328
|
+
pollTimer = setInterval(() => {
|
|
329
|
+
if (polling || exited) return;
|
|
330
|
+
polling = true;
|
|
331
|
+
(async () => {
|
|
332
|
+
const table = await processTableAsync();
|
|
333
|
+
polling = false;
|
|
334
|
+
if (!exited) tree?.record(table);
|
|
335
|
+
})();
|
|
336
|
+
}, TREE_POLL_MS);
|
|
337
|
+
}
|
|
338
|
+
const recordTree = () => {
|
|
339
|
+
tree?.record(processTable());
|
|
340
|
+
};
|
|
159
341
|
const timer = setTimeout(() => {
|
|
160
342
|
timedOut = true;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (child.pid !== void 0) process.kill(-child.pid, "SIGKILL");
|
|
164
|
-
} catch {}
|
|
343
|
+
recordTree();
|
|
344
|
+
killRun();
|
|
165
345
|
}, timeout);
|
|
166
346
|
const signalTimer = signal && setTimeout(() => {
|
|
167
|
-
if (exited ||
|
|
347
|
+
if (exited || proc.pid === void 0) return;
|
|
348
|
+
recordTree();
|
|
168
349
|
signalSentMs = Math.round(performance.now() - start);
|
|
169
350
|
stderrAtSignal = stderr.length;
|
|
170
351
|
try {
|
|
171
|
-
process.kill(signal.to === "process" ?
|
|
352
|
+
process.kill(signal.to === "process" ? proc.pid : -proc.pid, signal.name);
|
|
172
353
|
} catch {}
|
|
173
354
|
}, signal.afterMs);
|
|
174
|
-
const closeTimer = typeof stdin === "object" ? setTimeout(() =>
|
|
175
|
-
|
|
355
|
+
const closeTimer = typeof stdin === "object" ? setTimeout(() => proc.stdin?.destroy(), stdin.closeAfterMs) : void 0;
|
|
356
|
+
let closed = false;
|
|
357
|
+
let onClosed;
|
|
358
|
+
const outputClosed = new Promise((resolveClosed) => {
|
|
359
|
+
onClosed = resolveClosed;
|
|
360
|
+
});
|
|
361
|
+
proc.on("exit", (exitCode, exitSignal) => {
|
|
176
362
|
exited = true;
|
|
363
|
+
exitMs = Math.round(performance.now() - start);
|
|
364
|
+
clearTimeout(timer);
|
|
177
365
|
clearTimeout(signalTimer);
|
|
178
366
|
clearTimeout(closeTimer);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
367
|
+
clearInterval(pollTimer);
|
|
368
|
+
tree?.programExited();
|
|
369
|
+
proc.stdin?.destroy();
|
|
370
|
+
(async () => {
|
|
371
|
+
if (tree) {
|
|
372
|
+
const left = await watchLeftovers(tree, timedOut ? 0 : leftoversMs);
|
|
373
|
+
killAll(left, groups());
|
|
374
|
+
leftRunning = left.map((p) => ({
|
|
375
|
+
pid: p.pid,
|
|
376
|
+
command: p.command
|
|
377
|
+
}));
|
|
378
|
+
}
|
|
379
|
+
if (!closed) {
|
|
380
|
+
let giveUp;
|
|
381
|
+
const gaveUp = new Promise((resolveWait) => {
|
|
382
|
+
giveUp = setTimeout(resolveWait, CLOSE_WAIT_MS);
|
|
383
|
+
});
|
|
384
|
+
await Promise.race([outputClosed, gaveUp]);
|
|
385
|
+
clearTimeout(giveUp);
|
|
386
|
+
}
|
|
387
|
+
proc.stdout?.destroy();
|
|
388
|
+
proc.stderr?.destroy();
|
|
389
|
+
finish({
|
|
390
|
+
exitCode,
|
|
391
|
+
signal: exitSignal
|
|
392
|
+
});
|
|
393
|
+
})();
|
|
188
394
|
});
|
|
189
|
-
|
|
395
|
+
proc.stdout?.on("data", (chunk) => {
|
|
190
396
|
if (stdoutBytes < STDOUT_CAP) stdoutChunks.push(chunk);
|
|
191
397
|
stdoutBytes += chunk.length;
|
|
192
398
|
});
|
|
193
|
-
|
|
399
|
+
proc.stderr?.on("data", (chunk) => {
|
|
194
400
|
if (stderr.length < STDERR_CAP) stderr += chunk.toString("utf8");
|
|
195
401
|
});
|
|
196
|
-
|
|
402
|
+
proc.on("error", (error) => {
|
|
197
403
|
clearTimeout(timer);
|
|
198
404
|
clearTimeout(signalTimer);
|
|
199
405
|
finish({
|
|
@@ -202,33 +408,25 @@ function run$1(command, args, { closedStdout = false, timeout, env: overrides =
|
|
|
202
408
|
spawnError: error.message
|
|
203
409
|
});
|
|
204
410
|
});
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
afterExit.then(() => finish({
|
|
209
|
-
exitCode,
|
|
210
|
-
signal: exitSignal
|
|
211
|
-
}));
|
|
411
|
+
proc.on("close", () => {
|
|
412
|
+
closed = true;
|
|
413
|
+
onClosed?.();
|
|
212
414
|
});
|
|
213
415
|
});
|
|
214
416
|
}
|
|
215
|
-
/** How often to look at the group while waiting for it to empty. */
|
|
216
|
-
const GROUP_POLL_MS = 50;
|
|
217
417
|
/**
|
|
218
|
-
* Waits up to `ms` for
|
|
219
|
-
* parent take a moment to exit,
|
|
418
|
+
* Waits up to `ms` for what the program left running to finish on its own, as
|
|
419
|
+
* children stopped by their parent take a moment to exit, and returns what is
|
|
420
|
+
* still running then.
|
|
220
421
|
*/
|
|
221
|
-
async function watchLeftovers(
|
|
422
|
+
async function watchLeftovers(tree, ms) {
|
|
222
423
|
const deadline = performance.now() + ms;
|
|
223
|
-
let
|
|
224
|
-
while (
|
|
225
|
-
await
|
|
226
|
-
|
|
424
|
+
let left = tree.running(processTable());
|
|
425
|
+
while (left.length > 0 && performance.now() < deadline) {
|
|
426
|
+
await delay(LEFTOVER_POLL_MS);
|
|
427
|
+
left = tree.running(processTable());
|
|
227
428
|
}
|
|
228
|
-
|
|
229
|
-
process.kill(-pgid, "SIGKILL");
|
|
230
|
-
} catch {}
|
|
231
|
-
return members;
|
|
429
|
+
return left;
|
|
232
430
|
}
|
|
233
431
|
// src/checks/baseline.ts
|
|
234
432
|
/** A flag no real program defines. */
|
|
@@ -777,6 +975,10 @@ async function runPty(command, args, { timeout, env = {}, input }) {
|
|
|
777
975
|
const support = ptySupport();
|
|
778
976
|
if (!support.ok) throw new Error(`no PTY: ${support.reason}`);
|
|
779
977
|
const dir = mkdtempSync(join(tmpdir(), "cliprobe-pty-"));
|
|
978
|
+
const unregister = onInterrupt(() => rmSync(dir, {
|
|
979
|
+
recursive: true,
|
|
980
|
+
force: true
|
|
981
|
+
}));
|
|
780
982
|
const pidFile = join(dir, "pid");
|
|
781
983
|
let launcher = "script";
|
|
782
984
|
let argv = ptyArgs(support.flavor, pidFile, command, args);
|
|
@@ -820,7 +1022,7 @@ async function runPty(command, args, { timeout, env = {}, input }) {
|
|
|
820
1022
|
SHELL: "/bin/sh",
|
|
821
1023
|
...env
|
|
822
1024
|
},
|
|
823
|
-
|
|
1025
|
+
extraGroup: () => programGroup(pidFile)
|
|
824
1026
|
});
|
|
825
1027
|
return {
|
|
826
1028
|
output: cleanPtyOutput(result.stdout, support.flavor),
|
|
@@ -835,6 +1037,7 @@ async function runPty(command, args, { timeout, env = {}, input }) {
|
|
|
835
1037
|
clearTimeout(typeTimer);
|
|
836
1038
|
clearInterval(watchTimer);
|
|
837
1039
|
closeKeyboard();
|
|
1040
|
+
unregister();
|
|
838
1041
|
rmSync(dir, {
|
|
839
1042
|
recursive: true,
|
|
840
1043
|
force: true
|
|
@@ -867,12 +1070,6 @@ function programState(pidFile) {
|
|
|
867
1070
|
return "gone";
|
|
868
1071
|
}
|
|
869
1072
|
}
|
|
870
|
-
function killGroupFromPidFile(pidFile) {
|
|
871
|
-
const pid = programGroup(pidFile);
|
|
872
|
-
try {
|
|
873
|
-
if (pid !== void 0) process.kill(-pid, "SIGKILL");
|
|
874
|
-
} catch {}
|
|
875
|
-
}
|
|
876
1073
|
// src/checks/color.ts
|
|
877
1074
|
/**
|
|
878
1075
|
* Every color run gets a terminal type that supports color, so a program cannot
|
|
@@ -1238,13 +1435,115 @@ const controlCharsCheck = {
|
|
|
1238
1435
|
return results;
|
|
1239
1436
|
}
|
|
1240
1437
|
};
|
|
1438
|
+
// src/checks/sigterm.ts
|
|
1439
|
+
/** How long the program runs before SIGTERM. */
|
|
1440
|
+
const SIGTERM_DELAY_MS = 1e3;
|
|
1441
|
+
/** How long it gets to exit after SIGTERM. */
|
|
1442
|
+
const SIGTERM_GRACE_MS = 3e3;
|
|
1443
|
+
/** How long its children get to follow it once it has exited. */
|
|
1444
|
+
const SIGTERM_LEFTOVERS_MS = 1e3;
|
|
1445
|
+
/** Lines worth quoting from a trace printed on SIGTERM, best first. */
|
|
1446
|
+
const TRACE_MESSAGES$1 = [/^(\w*Error\b|\w*Exception\b|panic:)/, TRACE_RE];
|
|
1447
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "es"}`;
|
|
1448
|
+
/** "1 process" or "3 processes", and the evidence naming the first one, for a check that reports leftovers. */
|
|
1449
|
+
function describeLeftovers(left) {
|
|
1450
|
+
const [first] = left;
|
|
1451
|
+
const more = left.length > 1 ? ` (and ${left.length - 1} more)` : "";
|
|
1452
|
+
return {
|
|
1453
|
+
count: plural(left.length, "process"),
|
|
1454
|
+
evidence: `still running: ${first?.command ?? ""}${more}`
|
|
1455
|
+
};
|
|
1456
|
+
}
|
|
1457
|
+
function judgeSigterm(r) {
|
|
1458
|
+
if (r.spawnError) return {
|
|
1459
|
+
verdict: "error",
|
|
1460
|
+
reason: `could not start: ${r.spawnError}`
|
|
1461
|
+
};
|
|
1462
|
+
if (r.signalSentMs === void 0) return {
|
|
1463
|
+
verdict: "skip",
|
|
1464
|
+
reason: `finished in ${r.durationMs} ms, before SIGTERM at ${SIGTERM_DELAY_MS} ms; probe a command that keeps running`
|
|
1465
|
+
};
|
|
1466
|
+
if (r.timedOut) return {
|
|
1467
|
+
verdict: "warn",
|
|
1468
|
+
reason: `ignored SIGTERM: still running ${SIGTERM_GRACE_MS / 1e3} s later, so docker stop or a process manager has to kill it`
|
|
1469
|
+
};
|
|
1470
|
+
const left = r.leftRunning ?? [];
|
|
1471
|
+
if (left.length > 0) {
|
|
1472
|
+
const { count, evidence } = describeLeftovers(left);
|
|
1473
|
+
return {
|
|
1474
|
+
verdict: "warn",
|
|
1475
|
+
reason: `exited on SIGTERM but left ${count} running, which keep ports and files busy`,
|
|
1476
|
+
evidence
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
const after = r.stderr.slice(r.stderrAtSignal ?? 0);
|
|
1480
|
+
if (TRACE_RE.test(after)) return {
|
|
1481
|
+
verdict: "warn",
|
|
1482
|
+
reason: "printed a stack trace on SIGTERM",
|
|
1483
|
+
evidence: crashEvidence(after, TRACE_MESSAGES$1)
|
|
1484
|
+
};
|
|
1485
|
+
const ms = `${r.durationMs - r.signalSentMs} ms`;
|
|
1486
|
+
return {
|
|
1487
|
+
verdict: "pass",
|
|
1488
|
+
reason: `${r.signal ? `stopped by ${r.signal}` : `exited with code ${r.exitCode}`} in ${ms} and left nothing running`
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
async function sigterm(command, args) {
|
|
1492
|
+
const r = await run$1(command, args, {
|
|
1493
|
+
timeout: 4e3,
|
|
1494
|
+
signal: {
|
|
1495
|
+
name: "SIGTERM",
|
|
1496
|
+
afterMs: SIGTERM_DELAY_MS,
|
|
1497
|
+
to: "process"
|
|
1498
|
+
},
|
|
1499
|
+
stdin: "open",
|
|
1500
|
+
leftoversMs: SIGTERM_LEFTOVERS_MS
|
|
1501
|
+
});
|
|
1502
|
+
const sent = r.signalSentMs;
|
|
1503
|
+
return {
|
|
1504
|
+
check: "sigterm",
|
|
1505
|
+
invocation: [command, ...args],
|
|
1506
|
+
...judgeSigterm(r),
|
|
1507
|
+
sentAfterMs: sent ?? null,
|
|
1508
|
+
exitedAfterMs: sent === void 0 || r.timedOut ? null : r.durationMs - sent,
|
|
1509
|
+
exitCode: r.exitCode,
|
|
1510
|
+
signal: r.signal,
|
|
1511
|
+
leftRunning: (r.leftRunning ?? []).map((m) => m.command),
|
|
1512
|
+
stderrAfter: sent === void 0 ? "" : r.stderr.slice(r.stderrAtSignal ?? 0).slice(-2e3)
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
const sigtermCheck = {
|
|
1516
|
+
name: "sigterm",
|
|
1517
|
+
kind: "check",
|
|
1518
|
+
summary: "ignores SIGTERM, or exits on it and leaves its child processes running",
|
|
1519
|
+
async run(target) {
|
|
1520
|
+
const own = target.probes.filter((p) => p.own);
|
|
1521
|
+
if (own.length === 0) return [{
|
|
1522
|
+
check: "sigterm",
|
|
1523
|
+
invocation: [target.command],
|
|
1524
|
+
verdict: "skip",
|
|
1525
|
+
reason: "needs a probe that keeps running, such as a watch or serve command (--probe), or --bare",
|
|
1526
|
+
sentAfterMs: null,
|
|
1527
|
+
exitedAfterMs: null,
|
|
1528
|
+
exitCode: null,
|
|
1529
|
+
signal: null,
|
|
1530
|
+
leftRunning: [],
|
|
1531
|
+
stderrAfter: ""
|
|
1532
|
+
}];
|
|
1533
|
+
const results = [];
|
|
1534
|
+
for (const { args } of own) results.push(await sigterm(target.command, args));
|
|
1535
|
+
return results;
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1241
1538
|
// src/checks/ctrl-c.ts
|
|
1242
1539
|
/** How long the program runs before Ctrl-C. */
|
|
1243
1540
|
const CTRL_C_DELAY_MS = 1e3;
|
|
1244
1541
|
/** How long it gets to exit after Ctrl-C. */
|
|
1245
1542
|
const CTRL_C_GRACE_MS = 3e3;
|
|
1543
|
+
/** How long the rest of its group gets to follow it once it has exited. */
|
|
1544
|
+
const CTRL_C_LEFTOVERS_MS = 1e3;
|
|
1246
1545
|
/** Lines worth quoting from a trace printed on Ctrl-C, best first. */
|
|
1247
|
-
const TRACE_MESSAGES
|
|
1546
|
+
const TRACE_MESSAGES = [/^(KeyboardInterrupt|\w*Error\b|\w*Exception\b|panic:)/, TRACE_RE];
|
|
1248
1547
|
function judgeCtrlC(r) {
|
|
1249
1548
|
if (r.spawnError) return {
|
|
1250
1549
|
verdict: "error",
|
|
@@ -1263,8 +1562,17 @@ function judgeCtrlC(r) {
|
|
|
1263
1562
|
if (TRACE_RE.test(after)) return {
|
|
1264
1563
|
verdict: "fail",
|
|
1265
1564
|
reason: "printed a stack trace on Ctrl-C",
|
|
1266
|
-
evidence: crashEvidence(after, TRACE_MESSAGES
|
|
1565
|
+
evidence: crashEvidence(after, TRACE_MESSAGES)
|
|
1267
1566
|
};
|
|
1567
|
+
const left = r.leftRunning ?? [];
|
|
1568
|
+
if (left.length > 0) {
|
|
1569
|
+
const { count, evidence } = describeLeftovers(left);
|
|
1570
|
+
return {
|
|
1571
|
+
verdict: "warn",
|
|
1572
|
+
reason: `exited on Ctrl-C in ${ms} but left ${count} running that ignored it`,
|
|
1573
|
+
evidence
|
|
1574
|
+
};
|
|
1575
|
+
}
|
|
1268
1576
|
if (r.signal) return {
|
|
1269
1577
|
verdict: "pass",
|
|
1270
1578
|
reason: `stopped by ${r.signal} in ${ms}`
|
|
@@ -1289,7 +1597,8 @@ async function ctrlC(command, args) {
|
|
|
1289
1597
|
name: "SIGINT",
|
|
1290
1598
|
afterMs: CTRL_C_DELAY_MS
|
|
1291
1599
|
},
|
|
1292
|
-
stdin: "open"
|
|
1600
|
+
stdin: "open",
|
|
1601
|
+
leftoversMs: CTRL_C_LEFTOVERS_MS
|
|
1293
1602
|
});
|
|
1294
1603
|
const sent = r.signalSentMs;
|
|
1295
1604
|
return {
|
|
@@ -1300,6 +1609,7 @@ async function ctrlC(command, args) {
|
|
|
1300
1609
|
exitedAfterMs: sent === void 0 || r.timedOut ? null : r.durationMs - sent,
|
|
1301
1610
|
exitCode: r.exitCode,
|
|
1302
1611
|
signal: r.signal,
|
|
1612
|
+
leftRunning: (r.leftRunning ?? []).map((m) => m.command),
|
|
1303
1613
|
stderrAfter: sent === void 0 ? "" : r.stderr.slice(r.stderrAtSignal ?? 0).slice(-2e3)
|
|
1304
1614
|
};
|
|
1305
1615
|
}
|
|
@@ -1318,6 +1628,7 @@ const ctrlCCheck = {
|
|
|
1318
1628
|
exitedAfterMs: null,
|
|
1319
1629
|
exitCode: null,
|
|
1320
1630
|
signal: null,
|
|
1631
|
+
leftRunning: [],
|
|
1321
1632
|
stderrAfter: ""
|
|
1322
1633
|
}];
|
|
1323
1634
|
const results = [];
|
|
@@ -1516,95 +1827,6 @@ const restoreCheck = {
|
|
|
1516
1827
|
return results;
|
|
1517
1828
|
}
|
|
1518
1829
|
};
|
|
1519
|
-
// src/checks/sigterm.ts
|
|
1520
|
-
/** How long the program runs before SIGTERM. */
|
|
1521
|
-
const SIGTERM_DELAY_MS = 1e3;
|
|
1522
|
-
/** How long it gets to exit after SIGTERM. */
|
|
1523
|
-
const SIGTERM_GRACE_MS = 3e3;
|
|
1524
|
-
/** How long its children get to follow it once it has exited. */
|
|
1525
|
-
const SIGTERM_LEFTOVERS_MS = 1e3;
|
|
1526
|
-
/** Lines worth quoting from a trace printed on SIGTERM, best first. */
|
|
1527
|
-
const TRACE_MESSAGES = [/^(\w*Error\b|\w*Exception\b|panic:)/, TRACE_RE];
|
|
1528
|
-
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "es"}`;
|
|
1529
|
-
function judgeSigterm(r) {
|
|
1530
|
-
if (r.spawnError) return {
|
|
1531
|
-
verdict: "error",
|
|
1532
|
-
reason: `could not start: ${r.spawnError}`
|
|
1533
|
-
};
|
|
1534
|
-
if (r.signalSentMs === void 0) return {
|
|
1535
|
-
verdict: "skip",
|
|
1536
|
-
reason: `finished in ${r.durationMs} ms, before SIGTERM at ${SIGTERM_DELAY_MS} ms; probe a command that keeps running`
|
|
1537
|
-
};
|
|
1538
|
-
if (r.timedOut) return {
|
|
1539
|
-
verdict: "warn",
|
|
1540
|
-
reason: `ignored SIGTERM: still running ${SIGTERM_GRACE_MS / 1e3} s later, so docker stop or a process manager has to kill it`
|
|
1541
|
-
};
|
|
1542
|
-
const left = r.leftRunning ?? [];
|
|
1543
|
-
const [firstLeft] = left;
|
|
1544
|
-
if (firstLeft) return {
|
|
1545
|
-
verdict: "warn",
|
|
1546
|
-
reason: `exited on SIGTERM but left ${plural(left.length, "process")} running, which keep ports and files busy`,
|
|
1547
|
-
evidence: `still running: ${firstLeft.command}${left.length > 1 ? ` (and ${left.length - 1} more)` : ""}`
|
|
1548
|
-
};
|
|
1549
|
-
const after = r.stderr.slice(r.stderrAtSignal ?? 0);
|
|
1550
|
-
if (TRACE_RE.test(after)) return {
|
|
1551
|
-
verdict: "warn",
|
|
1552
|
-
reason: "printed a stack trace on SIGTERM",
|
|
1553
|
-
evidence: crashEvidence(after, TRACE_MESSAGES)
|
|
1554
|
-
};
|
|
1555
|
-
const ms = `${r.durationMs - r.signalSentMs} ms`;
|
|
1556
|
-
return {
|
|
1557
|
-
verdict: "pass",
|
|
1558
|
-
reason: `${r.signal ? `stopped by ${r.signal}` : `exited with code ${r.exitCode}`} in ${ms} and left nothing running`
|
|
1559
|
-
};
|
|
1560
|
-
}
|
|
1561
|
-
async function sigterm(command, args) {
|
|
1562
|
-
const r = await run$1(command, args, {
|
|
1563
|
-
timeout: 4e3,
|
|
1564
|
-
signal: {
|
|
1565
|
-
name: "SIGTERM",
|
|
1566
|
-
afterMs: SIGTERM_DELAY_MS,
|
|
1567
|
-
to: "process"
|
|
1568
|
-
},
|
|
1569
|
-
stdin: "open",
|
|
1570
|
-
leftoversMs: SIGTERM_LEFTOVERS_MS
|
|
1571
|
-
});
|
|
1572
|
-
const sent = r.signalSentMs;
|
|
1573
|
-
return {
|
|
1574
|
-
check: "sigterm",
|
|
1575
|
-
invocation: [command, ...args],
|
|
1576
|
-
...judgeSigterm(r),
|
|
1577
|
-
sentAfterMs: sent ?? null,
|
|
1578
|
-
exitedAfterMs: sent === void 0 || r.timedOut ? null : r.durationMs - sent,
|
|
1579
|
-
exitCode: r.exitCode,
|
|
1580
|
-
signal: r.signal,
|
|
1581
|
-
leftRunning: (r.leftRunning ?? []).map((m) => m.command),
|
|
1582
|
-
stderrAfter: sent === void 0 ? "" : r.stderr.slice(r.stderrAtSignal ?? 0).slice(-2e3)
|
|
1583
|
-
};
|
|
1584
|
-
}
|
|
1585
|
-
const sigtermCheck = {
|
|
1586
|
-
name: "sigterm",
|
|
1587
|
-
kind: "check",
|
|
1588
|
-
summary: "ignores SIGTERM, or exits on it and leaves its child processes running",
|
|
1589
|
-
async run(target) {
|
|
1590
|
-
const own = target.probes.filter((p) => p.own);
|
|
1591
|
-
if (own.length === 0) return [{
|
|
1592
|
-
check: "sigterm",
|
|
1593
|
-
invocation: [target.command],
|
|
1594
|
-
verdict: "skip",
|
|
1595
|
-
reason: "needs a probe that keeps running, such as a watch or serve command (--probe), or --bare",
|
|
1596
|
-
sentAfterMs: null,
|
|
1597
|
-
exitedAfterMs: null,
|
|
1598
|
-
exitCode: null,
|
|
1599
|
-
signal: null,
|
|
1600
|
-
leftRunning: [],
|
|
1601
|
-
stderrAfter: ""
|
|
1602
|
-
}];
|
|
1603
|
-
const results = [];
|
|
1604
|
-
for (const { args } of own) results.push(await sigterm(target.command, args));
|
|
1605
|
-
return results;
|
|
1606
|
-
}
|
|
1607
|
-
};
|
|
1608
1830
|
function median(values) {
|
|
1609
1831
|
const sorted = [...values].sort((a, b) => a - b);
|
|
1610
1832
|
const mid = Math.floor(sorted.length / 2);
|
|
@@ -2206,7 +2428,8 @@ Checks:
|
|
|
2206
2428
|
prompts and skips if it reads stdin like a filter.
|
|
2207
2429
|
ctrl-c Sends Ctrl-C (SIGINT) after 1 s, with stdin left open as in a
|
|
2208
2430
|
terminal. Fails if the command is still running 3 s later or
|
|
2209
|
-
prints a stack trace; warns if it exits 0
|
|
2431
|
+
prints a stack trace; warns if it exits 0, or exits and leaves
|
|
2432
|
+
processes running that ignored the Ctrl-C. Skipped when the
|
|
2210
2433
|
command finishes first, so probe a command that keeps running
|
|
2211
2434
|
(a watcher, a server, a long task), e.g. with --probe "serve".
|
|
2212
2435
|
sigterm Sends SIGTERM to the command alone after 1 s, as kill, docker
|
|
@@ -2259,7 +2482,9 @@ Exit codes: 0 no failures, 1 a check failed (or warned, with --strict), 2 usage
|
|
|
2259
2482
|
error or could not run.
|
|
2260
2483
|
Runs the target in the current directory without a shell, with stdin closed
|
|
2261
2484
|
(the stdin, ctrl-c and sigterm checks use a pipe they never write to), a
|
|
2262
|
-
temporary HOME and a minimal environment. Only run programs you trust
|
|
2485
|
+
temporary HOME and a minimal environment. Only run programs you trust. When a
|
|
2486
|
+
probe exits, or when cliprobe is stopped, it kills whatever the probe left
|
|
2487
|
+
running and removes its temporary files.`;
|
|
2263
2488
|
// src/cli/program.ts
|
|
2264
2489
|
/**
|
|
2265
2490
|
* 0 no failures, 1 a check failed (or, with `strict`, warned), 2 nothing could be
|
|
@@ -2321,6 +2546,7 @@ async function run(argv, io) {
|
|
|
2321
2546
|
own: true
|
|
2322
2547
|
}))];
|
|
2323
2548
|
const fixture = probes.some((p) => usesFixture(p.args)) ? createFixture() : void 0;
|
|
2549
|
+
const unregister = fixture && onInterrupt(() => fixture.cleanup());
|
|
2324
2550
|
const results = [];
|
|
2325
2551
|
try {
|
|
2326
2552
|
const target = {
|
|
@@ -2337,6 +2563,7 @@ async function run(argv, io) {
|
|
|
2337
2563
|
};
|
|
2338
2564
|
for (const check of checks) results.push(...await check.run(target, ctx));
|
|
2339
2565
|
} finally {
|
|
2566
|
+
unregister?.();
|
|
2340
2567
|
fixture?.cleanup();
|
|
2341
2568
|
}
|
|
2342
2569
|
const report = {
|
|
@@ -2364,6 +2591,7 @@ process.stdout.on("error", (error) => {
|
|
|
2364
2591
|
if (error.code === "EPIPE") process.exit(process.exitCode ?? 0);
|
|
2365
2592
|
throw error;
|
|
2366
2593
|
});
|
|
2594
|
+
handleInterrupts();
|
|
2367
2595
|
process.exitCode = await run(process.argv.slice(2), {
|
|
2368
2596
|
stdout: (text) => process.stdout.write(text),
|
|
2369
2597
|
stderr: (text) => process.stderr.write(text),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cliprobe",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Black-box test any command-line program for terminal-edge bugs: crashes on a closed pipe (| head), color and control codes leaking into pipes, hangs waiting on stdin, broken Ctrl-C handling, child processes left running after SIGTERM, a terminal left broken after Ctrl-C, and control characters from file names reaching your terminal.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/schema.json
CHANGED
|
@@ -454,6 +454,13 @@
|
|
|
454
454
|
"type": ["string", "null"],
|
|
455
455
|
"description": "Signal that ended it, such as \"SIGPIPE\"; null if it exited."
|
|
456
456
|
},
|
|
457
|
+
"leftRunning": {
|
|
458
|
+
"type": "array",
|
|
459
|
+
"items": {
|
|
460
|
+
"type": "string"
|
|
461
|
+
},
|
|
462
|
+
"description": "Command lines of the processes still running after it exited, in its group or started by it; they ignored the Ctrl-C too. Added in 1.4; absent from earlier reports."
|
|
463
|
+
},
|
|
457
464
|
"stderrAfter": {
|
|
458
465
|
"type": "string",
|
|
459
466
|
"description": "The last 2000 characters it printed to stderr after SIGINT."
|
|
@@ -495,7 +502,7 @@
|
|
|
495
502
|
"items": {
|
|
496
503
|
"type": "string"
|
|
497
504
|
},
|
|
498
|
-
"description": "Command lines of the processes still running in its group
|
|
505
|
+
"description": "Command lines of the processes still running after it exited, in its group or started by it in another group or session."
|
|
499
506
|
},
|
|
500
507
|
"stderrAfter": {
|
|
501
508
|
"type": "string",
|