@antelopejs/dms 0.17.0 → 0.18.1
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/CHANGELOG.md +65 -0
- package/README.md +7 -11
- package/bin/control/backend-records.test.mjs +276 -0
- package/bin/control/backend.mjs +212 -0
- package/bin/control/backend.test.mjs +325 -0
- package/bin/control/base.mjs +3 -7
- package/bin/control/browser-client.mjs +6 -4
- package/bin/control/browser.mjs +173 -36
- package/bin/control/build-cache.mjs +394 -0
- package/bin/control/build-cache.test.mjs +270 -0
- package/bin/control/build-inputs.mjs +172 -0
- package/bin/control/build-key-refusals.test.mjs +138 -0
- package/bin/control/build-key.mjs +479 -0
- package/bin/control/build-key.test.mjs +392 -0
- package/bin/control/build-layout.test.mjs +314 -0
- package/bin/control/commands-stack.mjs +46 -6
- package/bin/control/commands-verify.mjs +9 -2
- package/bin/control/doctor.mjs +87 -16
- package/bin/control/forwarder.mjs +86 -0
- package/bin/control/forwarder.test.mjs +141 -0
- package/bin/control/frontend-check.mjs +54 -0
- package/bin/control/frontend-workspace.mjs +150 -0
- package/bin/control/frontend-workspace.test.mjs +133 -0
- package/bin/control/helpers/env.mjs +21 -0
- package/bin/control/helpers/evidence.mjs +13 -0
- package/bin/control/helpers/fake-run.mjs +290 -0
- package/bin/control/helpers/key-fixture.mjs +82 -0
- package/bin/control/helpers/pids.mjs +27 -0
- package/bin/control/helpers/repo.mjs +13 -0
- package/bin/control/helpers/tree.mjs +40 -0
- package/bin/control/identity.mjs +238 -0
- package/bin/control/identity.test.mjs +166 -0
- package/bin/control/journey.mjs +64 -16
- package/bin/control/journey.test.mjs +60 -2
- package/bin/control/namespaces.test.mjs +211 -0
- package/bin/control/options.mjs +2 -0
- package/bin/control/pid-guard.test.mjs +202 -0
- package/bin/control/prepared-workspace.mjs +20 -0
- package/bin/control/processes.mjs +122 -33
- package/bin/control/processes.test.mjs +255 -0
- package/bin/control/renderer.test.mjs +2 -1
- package/bin/control/run-copy.mjs +77 -0
- package/bin/control/run-state.mjs +142 -16
- package/bin/control/stack.mjs +259 -210
- package/bin/control/tab.mjs +13 -6
- package/bin/control/tab.test.mjs +444 -0
- package/bin/control/timing.mjs +244 -0
- package/bin/control/timing.test.mjs +197 -0
- package/bin/control/verify/decode.mjs +34 -21
- package/bin/control/verify/decode.test.mjs +39 -0
- package/bin/control/verify/doors.test.mjs +2 -0
- package/bin/control/verify/map.mjs +57 -7
- package/bin/control/verify/map.test.mjs +264 -0
- package/bin/control/verify/skill.mjs +12 -3
- package/bin/control/verify/types.mjs +2 -2
- package/bin/control/verify/verdict.mjs +37 -7
- package/bin/dms-control.mjs +14 -6
- package/dist/config/internal/state.d.ts +2 -2
- package/dist/config/internal/state.js.map +1 -1
- package/dist/utils/sign-in-country.d.ts +0 -1
- package/dist/utils/sign-in-country.js +7 -17
- package/dist/utils/sign-in-country.js.map +1 -1
- package/frontend-vue/layers/dms-layout/app/components/layout/DashboardSearch.vue +24 -1
- package/frontend-vue/layers/dms-layout/app/composables/layout/usePageHeaderActions.ts +1 -1
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/scripts/check-autoimport-boundaries.mjs +19 -3
- package/frontend-vue/scripts/test-autoimport-boundaries.mjs +19 -0
- package/package.json +5 -5
- package/skills/dms-project/SKILL.md +6 -3
- package/data/NOTICE.md +0 -12
- package/data/dbip-country-lite.mmdb +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
4
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { createRunFolders, saveRun } from "./run-state.mjs";
|
|
8
|
+
import {
|
|
9
|
+
assertSignalable,
|
|
10
|
+
isAlive,
|
|
11
|
+
isRunning,
|
|
12
|
+
startTimeOf,
|
|
13
|
+
} from "./identity.mjs";
|
|
14
|
+
import { killRecorded, stopRecorded } from "./processes.mjs";
|
|
15
|
+
|
|
16
|
+
const UNSAFE = [
|
|
17
|
+
-1,
|
|
18
|
+
0,
|
|
19
|
+
1,
|
|
20
|
+
undefined,
|
|
21
|
+
Number.NaN,
|
|
22
|
+
2.5,
|
|
23
|
+
"1234",
|
|
24
|
+
null,
|
|
25
|
+
2 ** 31,
|
|
26
|
+
2 ** 32 + 1,
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Replaces `process.kill` with a recorder for the test, so a regression in
|
|
31
|
+
* the guard records a call instead of signalling every process of the user.
|
|
32
|
+
*
|
|
33
|
+
* @param {import("node:test").TestContext} t The test
|
|
34
|
+
* @returns {unknown[][]} The calls `process.kill` received
|
|
35
|
+
*/
|
|
36
|
+
function recordKills(t) {
|
|
37
|
+
/** @type {unknown[][]} */
|
|
38
|
+
const calls = [];
|
|
39
|
+
const original = process.kill.bind(process);
|
|
40
|
+
process.kill = (...args) => {
|
|
41
|
+
calls.push(args);
|
|
42
|
+
return true;
|
|
43
|
+
};
|
|
44
|
+
t.after(() => {
|
|
45
|
+
process.kill = original;
|
|
46
|
+
});
|
|
47
|
+
return calls;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {unknown} pid */
|
|
51
|
+
const skipped = (pid) => `skipped pid ${String(pid)}: not a process id above 1`;
|
|
52
|
+
|
|
53
|
+
await test("stopRecorded skips and reports every record whose pid is not a whole number above 1 and sends no signal; a guard after the first kill lets kill(-1) reach every process of the user", async (t) => {
|
|
54
|
+
const calls = recordKills(t);
|
|
55
|
+
for (const pid of UNSAFE) {
|
|
56
|
+
assert.deepEqual(await stopRecorded([{ pid }]), [skipped(pid)]);
|
|
57
|
+
}
|
|
58
|
+
assert.deepEqual(calls, []);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await test("assertSignalable accepts a real process id and refuses the rest; a guard that only rejects -1 still lets 0 signal the caller's own group", () => {
|
|
62
|
+
assert.doesNotThrow(() => assertSignalable(process.pid));
|
|
63
|
+
for (const pid of UNSAFE) {
|
|
64
|
+
assert.throws(() => assertSignalable(pid), TypeError, String(pid));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
await test("isAlive answers false for those values without probing; probing kill(-1, 0) reports some process alive", (t) => {
|
|
69
|
+
const calls = recordKills(t);
|
|
70
|
+
for (const pid of UNSAFE) {
|
|
71
|
+
assert.equal(isAlive(pid), false, String(pid));
|
|
72
|
+
}
|
|
73
|
+
assert.deepEqual(calls, []);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
await test("stopRecorded skips and reports the bad pids a run recorded and still stops the good ones; refusing the whole run leaves its real processes running", async (t) => {
|
|
77
|
+
const child = spawn("sleep", ["60"], { stdio: "ignore", detached: true });
|
|
78
|
+
const own = child.pid;
|
|
79
|
+
if (own === undefined) throw new Error("sleep did not start");
|
|
80
|
+
t.after(() => {
|
|
81
|
+
if (child.exitCode === null && child.signalCode === null) child.kill();
|
|
82
|
+
});
|
|
83
|
+
/** @type {unknown[][]} */
|
|
84
|
+
const calls = [];
|
|
85
|
+
const original = process.kill.bind(process);
|
|
86
|
+
process.kill = (pid, signal) => {
|
|
87
|
+
calls.push([pid, signal]);
|
|
88
|
+
if (pid !== own && pid !== -own) return true;
|
|
89
|
+
return original(pid, signal);
|
|
90
|
+
};
|
|
91
|
+
t.after(() => {
|
|
92
|
+
process.kill = original;
|
|
93
|
+
});
|
|
94
|
+
const lines = await stopRecorded([
|
|
95
|
+
{ pid: -1 },
|
|
96
|
+
{ pid: own, startTime: startTimeOf(own) },
|
|
97
|
+
{ pid: 0 },
|
|
98
|
+
]);
|
|
99
|
+
assert.deepEqual(
|
|
100
|
+
calls.filter(([pid]) => pid !== own && pid !== -own),
|
|
101
|
+
[],
|
|
102
|
+
);
|
|
103
|
+
assert.ok(lines.includes("skipped pid -1: not a process id above 1"));
|
|
104
|
+
assert.ok(lines.includes("skipped pid 0: not a process id above 1"));
|
|
105
|
+
assert.ok(lines.some((line) => line.startsWith(`stopped pid ${own}`)));
|
|
106
|
+
assert.equal(isRunning(own), false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await test("killRecorded skips and reports every record whose pid is not a whole number above 1 and sends no signal; a hard kill without the guard sends SIGKILL to every process of the user", async (t) => {
|
|
110
|
+
const calls = recordKills(t);
|
|
111
|
+
for (const pid of UNSAFE) {
|
|
112
|
+
assert.deepEqual(await killRecorded([{ pid }]), [skipped(pid)]);
|
|
113
|
+
}
|
|
114
|
+
assert.deepEqual(calls, []);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
await test("killRecorded skips and reports the bad pids a run recorded and still kills the good ones; refusing the whole list leaves the real backend running", async (t) => {
|
|
118
|
+
const child = spawn("sleep", ["60"], { stdio: "ignore", detached: true });
|
|
119
|
+
const own = child.pid;
|
|
120
|
+
if (own === undefined) throw new Error("sleep did not start");
|
|
121
|
+
t.after(() => {
|
|
122
|
+
if (child.exitCode === null && child.signalCode === null) child.kill();
|
|
123
|
+
});
|
|
124
|
+
/** @type {unknown[][]} */
|
|
125
|
+
const calls = [];
|
|
126
|
+
const original = process.kill.bind(process);
|
|
127
|
+
process.kill = (pid, signal) => {
|
|
128
|
+
calls.push([pid, signal]);
|
|
129
|
+
if (pid !== own && pid !== -own) return true;
|
|
130
|
+
return original(pid, signal);
|
|
131
|
+
};
|
|
132
|
+
t.after(() => {
|
|
133
|
+
process.kill = original;
|
|
134
|
+
});
|
|
135
|
+
const lines = await killRecorded([
|
|
136
|
+
{ pid: -1 },
|
|
137
|
+
{ pid: own, startTime: startTimeOf(own) },
|
|
138
|
+
{ pid: 0 },
|
|
139
|
+
]);
|
|
140
|
+
assert.deepEqual(
|
|
141
|
+
calls.filter(([pid]) => pid !== own && pid !== -own),
|
|
142
|
+
[],
|
|
143
|
+
);
|
|
144
|
+
assert.ok(lines.includes("skipped pid -1: not a process id above 1"));
|
|
145
|
+
assert.ok(lines.includes("skipped pid 0: not a process id above 1"));
|
|
146
|
+
assert.ok(lines.includes(`killed pid ${own}`));
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const CONTROL = join(import.meta.dirname, "..", "dms-control.mjs");
|
|
150
|
+
const NO_UNSAFE_KILL = `data:text/javascript,const k=process.kill.bind(process);process.kill=(p,s)=>{if(!(Number.isInteger(p)&&Math.abs(p)>1))throw new Error("unsafe kill "+p);return k(p,s);};`;
|
|
151
|
+
|
|
152
|
+
await test("dms-control down prints the recorded pid it skipped; dropping the line leaves the operator unaware a record was corrupt", (t) => {
|
|
153
|
+
const dir = mkdtempSync(join(tmpdir(), "dms-control-pid-guard-down-"));
|
|
154
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
155
|
+
saveRun({
|
|
156
|
+
...createRunFolders(dir),
|
|
157
|
+
id: "20261107T000000-abc123",
|
|
158
|
+
createdAt: "2026-11-07T00:00:00.000Z",
|
|
159
|
+
environment: "docs",
|
|
160
|
+
playground: dir,
|
|
161
|
+
moduleRoot: dir,
|
|
162
|
+
repo: null,
|
|
163
|
+
headSha: null,
|
|
164
|
+
baseRef: "origin/main",
|
|
165
|
+
skill: null,
|
|
166
|
+
apiPort: 5010,
|
|
167
|
+
webPort: 5011,
|
|
168
|
+
smtpPort: 5012,
|
|
169
|
+
mail: join(dir, "evidence", "mail"),
|
|
170
|
+
apiUrl: "http://127.0.0.1:5010",
|
|
171
|
+
webUrl: "http://127.0.0.1:5011",
|
|
172
|
+
databasePrefix: "verify_x",
|
|
173
|
+
database: "verify_x",
|
|
174
|
+
mongoUrl: "",
|
|
175
|
+
ownsMongo: false,
|
|
176
|
+
secrets: {},
|
|
177
|
+
adminPassword: "",
|
|
178
|
+
extraEnv: {},
|
|
179
|
+
processes: [
|
|
180
|
+
{
|
|
181
|
+
name: "bogus",
|
|
182
|
+
pid: -1,
|
|
183
|
+
command: "node bogus",
|
|
184
|
+
cwd: dir,
|
|
185
|
+
log: join(dir, "logs", "bogus.log"),
|
|
186
|
+
startedAt: "2026-11-07T00:00:00.000Z",
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
accounts: {},
|
|
190
|
+
tokens: {},
|
|
191
|
+
ready: false,
|
|
192
|
+
});
|
|
193
|
+
const out = execFileSync(
|
|
194
|
+
process.execPath,
|
|
195
|
+
["--import", NO_UNSAFE_KILL, CONTROL, "down", dir],
|
|
196
|
+
{ encoding: "utf8" },
|
|
197
|
+
);
|
|
198
|
+
assert.deepEqual(
|
|
199
|
+
out.split("\n").filter((line) => line.startsWith("skipped")),
|
|
200
|
+
["skipped pid -1: not a process id above 1"],
|
|
201
|
+
);
|
|
202
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { describeFailure } from "./failure.mjs";
|
|
3
|
+
import {
|
|
4
|
+
buildInPreparedWorkspace,
|
|
5
|
+
PREPARED_WORKSPACE_REFUSED,
|
|
6
|
+
} from "./frontend-workspace.mjs";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Preloaded (`node --import`) into the `ajs dms build` that `up` runs in a
|
|
10
|
+
* workspace it prepared: the build reads that workspace as it is, the
|
|
11
|
+
* sources its key hashed, instead of copying them from the checkout again.
|
|
12
|
+
* When it cannot, the process ends with {@link PREPARED_WORKSPACE_REFUSED}
|
|
13
|
+
* before the build starts, and `up` builds as `ajs dms build` does.
|
|
14
|
+
*/
|
|
15
|
+
try {
|
|
16
|
+
buildInPreparedWorkspace(join(dirname(process.argv[1] ?? ""), ".."));
|
|
17
|
+
} catch (error) {
|
|
18
|
+
process.stderr.write(`dms-control: ${describeFailure(error)}\n`);
|
|
19
|
+
process.exit(PREPARED_WORKSPACE_REFUSED);
|
|
20
|
+
}
|
|
@@ -10,8 +10,18 @@ import {
|
|
|
10
10
|
import { createConnection, createServer } from "node:net";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
13
|
+
import {
|
|
14
|
+
assertOwnProc,
|
|
15
|
+
assertSignalable,
|
|
16
|
+
isAlive,
|
|
17
|
+
isSameProcess,
|
|
18
|
+
isSignalable,
|
|
19
|
+
startTimeOf,
|
|
20
|
+
} from "./identity.mjs";
|
|
13
21
|
|
|
14
22
|
const POLL_MS = 500;
|
|
23
|
+
const READY_POLL_MS = 100;
|
|
24
|
+
const KILL_POLL_MS = 10;
|
|
15
25
|
const STOP_GRACE_MS = 10_000;
|
|
16
26
|
const PROBE_TIMEOUT_MS = 3_000;
|
|
17
27
|
const LISTEN_STATE = "0A";
|
|
@@ -49,12 +59,16 @@ export function freePort() {
|
|
|
49
59
|
|
|
50
60
|
/**
|
|
51
61
|
* Starts a long-running program in its own process group, its output going
|
|
52
|
-
* straight to `<logs>/<name>.log` so it survives the caller.
|
|
62
|
+
* straight to `<logs>/<name>.log` so it survives the caller. The record
|
|
63
|
+
* holds the kernel's start time of the process, which tells it apart from
|
|
64
|
+
* a later process given the same pid.
|
|
53
65
|
*
|
|
54
66
|
* @param {{ name: string, program: string, args: string[], cwd: string, env: NodeJS.ProcessEnv, logs: string }} spec What to start and where its log goes
|
|
55
|
-
* @returns {{ name: string, pid: number, command: string, cwd: string, log: string, startedAt: string }} The process record
|
|
67
|
+
* @returns {{ name: string, pid: number, startTime: number | null, command: string, cwd: string, log: string, startedAt: string }} The process record
|
|
68
|
+
* @throws When /proc is not this PID namespace's, before it starts anything
|
|
56
69
|
*/
|
|
57
70
|
export function startDetached(spec) {
|
|
71
|
+
assertOwnProc();
|
|
58
72
|
const log = join(spec.logs, `${spec.name}.log`);
|
|
59
73
|
const output = openSync(log, "a");
|
|
60
74
|
const child = spawn(spec.program, spec.args, {
|
|
@@ -71,6 +85,7 @@ export function startDetached(spec) {
|
|
|
71
85
|
return {
|
|
72
86
|
name: spec.name,
|
|
73
87
|
pid: child.pid,
|
|
88
|
+
startTime: startTimeOf(child.pid),
|
|
74
89
|
command: [spec.program, ...spec.args].join(" "),
|
|
75
90
|
cwd: spec.cwd,
|
|
76
91
|
log,
|
|
@@ -78,12 +93,25 @@ export function startDetached(spec) {
|
|
|
78
93
|
};
|
|
79
94
|
}
|
|
80
95
|
|
|
96
|
+
/** A program that ran to its end and exited with another code than 0. */
|
|
97
|
+
export class ExitError extends Error {
|
|
98
|
+
/**
|
|
99
|
+
* @param {string} message What exited, how, and where its log is
|
|
100
|
+
* @param {number | null} exitCode Its exit code, or null when a signal ended it
|
|
101
|
+
*/
|
|
102
|
+
constructor(message, exitCode) {
|
|
103
|
+
super(message);
|
|
104
|
+
/** Its exit code, or null when a signal ended it. */
|
|
105
|
+
this.exitCode = exitCode;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
81
109
|
/**
|
|
82
110
|
* Runs a program to completion, its output appended to `<logs>/<name>.log`.
|
|
83
111
|
*
|
|
84
112
|
* @param {{ name: string, program: string, args: string[], cwd: string, env: NodeJS.ProcessEnv, logs: string }} spec What to run
|
|
85
113
|
* @returns {Promise<void>} Resolves on exit code 0
|
|
86
|
-
* @throws When the program exits otherwise, naming its log
|
|
114
|
+
* @throws {ExitError} When the program exits otherwise, naming its log
|
|
87
115
|
*/
|
|
88
116
|
export function runToEnd(spec) {
|
|
89
117
|
const log = join(spec.logs, `${spec.name}.log`);
|
|
@@ -102,27 +130,15 @@ export function runToEnd(spec) {
|
|
|
102
130
|
return;
|
|
103
131
|
}
|
|
104
132
|
reject(
|
|
105
|
-
new
|
|
133
|
+
new ExitError(
|
|
134
|
+
`${spec.name} exited with ${code ?? signal}; see ${log}`,
|
|
135
|
+
code,
|
|
136
|
+
),
|
|
106
137
|
);
|
|
107
138
|
});
|
|
108
139
|
});
|
|
109
140
|
}
|
|
110
141
|
|
|
111
|
-
/**
|
|
112
|
-
* Whether a process with this PID exists.
|
|
113
|
-
*
|
|
114
|
-
* @param {number} pid The process id
|
|
115
|
-
* @returns {boolean} True while it runs
|
|
116
|
-
*/
|
|
117
|
-
export function isAlive(pid) {
|
|
118
|
-
try {
|
|
119
|
-
process.kill(pid, 0);
|
|
120
|
-
return true;
|
|
121
|
-
} catch {
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
142
|
/**
|
|
127
143
|
* The status of one GET, or `null` when nothing answered.
|
|
128
144
|
*
|
|
@@ -157,7 +173,7 @@ export async function waitForOk(url, record, timeoutMs) {
|
|
|
157
173
|
if (!isAlive(record.pid)) {
|
|
158
174
|
throw new Error(`${url}: the process exited first; see ${record.log}`);
|
|
159
175
|
}
|
|
160
|
-
await sleep(
|
|
176
|
+
await sleep(READY_POLL_MS);
|
|
161
177
|
}
|
|
162
178
|
throw new Error(
|
|
163
179
|
`${url} did not answer 200 in ${timeoutMs} ms; see ${record.log}`,
|
|
@@ -316,6 +332,7 @@ export function listenersOf(port) {
|
|
|
316
332
|
* @param {NodeJS.Signals} signal
|
|
317
333
|
*/
|
|
318
334
|
function signalGroup(pid, signal) {
|
|
335
|
+
assertSignalable(pid);
|
|
319
336
|
try {
|
|
320
337
|
process.kill(-pid, signal);
|
|
321
338
|
} catch {
|
|
@@ -327,21 +344,93 @@ function signalGroup(pid, signal) {
|
|
|
327
344
|
}
|
|
328
345
|
}
|
|
329
346
|
|
|
347
|
+
/** @typedef {{ pid: number, startTime?: number | null, startedAt?: string }} RecordedProcess A process a run recorded, as {@link isSameProcess} tells it apart */
|
|
348
|
+
|
|
330
349
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
350
|
+
* The recorded processes that still run as the run started them, and their
|
|
351
|
+
* descendants, each with the start time that tells it apart from a later
|
|
352
|
+
* process given its pid. A record whose pid another process holds now, or
|
|
353
|
+
* whose pid is not a whole number above 1, is left out, so no descendant of
|
|
354
|
+
* 0 or 1 is ever looked up.
|
|
333
355
|
*
|
|
334
|
-
* @param {
|
|
335
|
-
* @returns {
|
|
356
|
+
* @param {RecordedProcess[]} records The recorded processes
|
|
357
|
+
* @returns {{ pid: number, startTime: number | null }[]} One entry per process
|
|
336
358
|
*/
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
|
|
359
|
+
function ownProcesses(records) {
|
|
360
|
+
const roots = records.filter(isSameProcess).map((record) => record.pid);
|
|
361
|
+
const pids = [...new Set([...roots, ...descendantsOf(roots)])];
|
|
362
|
+
return pids.map((pid) => ({ pid, startTime: startTimeOf(pid) }));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @param {RecordedProcess[]} records
|
|
367
|
+
* @returns {string[]} One line per record whose pid is not a whole number above 1
|
|
368
|
+
*/
|
|
369
|
+
function skippedRecords(records) {
|
|
370
|
+
return records
|
|
371
|
+
.filter((record) => !isSignalable(record.pid))
|
|
372
|
+
.map(
|
|
373
|
+
(record) => `skipped pid ${String(record.pid)}: not a process id above 1`,
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Stops the processes a run recorded and every descendant: SIGTERM to each
|
|
379
|
+
* process group, then SIGKILL to whatever outlives the grace period. Each is
|
|
380
|
+
* signalled only while its pid still runs the process recorded, checked
|
|
381
|
+
* right before each signal. A record whose pid is not a whole number above
|
|
382
|
+
* 1 is reported, never signalled, and the rest still stop.
|
|
383
|
+
*
|
|
384
|
+
* @param {RecordedProcess[]} records The recorded processes
|
|
385
|
+
* @returns {Promise<string[]>} One line per record skipped and per process signalled
|
|
386
|
+
* @throws When /proc is not this PID namespace's, before it signals anything
|
|
387
|
+
*/
|
|
388
|
+
export async function stopRecorded(records) {
|
|
389
|
+
const targets = ownProcesses(records);
|
|
390
|
+
for (const target of targets) signalGroup(target.pid, "SIGTERM");
|
|
340
391
|
const deadline = Date.now() + STOP_GRACE_MS;
|
|
341
|
-
while (targets.some(
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
392
|
+
while (targets.some(isSameProcess) && Date.now() < deadline) {
|
|
393
|
+
await sleep(READY_POLL_MS);
|
|
394
|
+
}
|
|
395
|
+
const stubborn = targets.filter(isSameProcess);
|
|
396
|
+
for (const target of stubborn) signalGroup(target.pid, "SIGKILL");
|
|
397
|
+
return [
|
|
398
|
+
...skippedRecords(records),
|
|
399
|
+
...targets.map(
|
|
400
|
+
({ pid }) =>
|
|
401
|
+
`stopped pid ${pid}${stubborn.some((target) => target.pid === pid) ? " (SIGKILL)" : ""}`,
|
|
402
|
+
),
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Kills the processes a run recorded and every descendant with SIGKILL at
|
|
408
|
+
* once, for a backend whose database goes next, and resolves once none of
|
|
409
|
+
* them runs. Nothing flushes or shuts down. A zombie whose threads have all
|
|
410
|
+
* exited no longer runs, so one that nothing reaps does not hold the wait.
|
|
411
|
+
* Each is signalled only while its pid still runs the process recorded,
|
|
412
|
+
* checked right before the signal, so a pid another process took while the
|
|
413
|
+
* caller waited is left alone. A record whose pid is not a whole number
|
|
414
|
+
* above 1 is reported, never signalled, and the rest are still killed.
|
|
415
|
+
*
|
|
416
|
+
* @param {RecordedProcess[]} records The recorded processes
|
|
417
|
+
* @returns {Promise<string[]>} One line per record skipped and per process killed
|
|
418
|
+
* @throws When /proc is not this PID namespace's, before it signals anything
|
|
419
|
+
* @throws When a process still runs after the grace period
|
|
420
|
+
*/
|
|
421
|
+
export async function killRecorded(records) {
|
|
422
|
+
const targets = ownProcesses(records);
|
|
423
|
+
for (const target of targets) signalGroup(target.pid, "SIGKILL");
|
|
424
|
+
const deadline = Date.now() + STOP_GRACE_MS;
|
|
425
|
+
while (targets.some(isSameProcess)) {
|
|
426
|
+
if (Date.now() >= deadline) {
|
|
427
|
+
const left = targets.filter(isSameProcess).map(({ pid }) => pid);
|
|
428
|
+
throw new Error(`pid ${left.join(", ")} survived SIGKILL`);
|
|
429
|
+
}
|
|
430
|
+
await sleep(KILL_POLL_MS);
|
|
431
|
+
}
|
|
432
|
+
return [
|
|
433
|
+
...skippedRecords(records),
|
|
434
|
+
...targets.map(({ pid }) => `killed pid ${pid}`),
|
|
435
|
+
];
|
|
347
436
|
}
|