@trama-dev/runtime 0.3.1 → 0.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/dist/child-process.d.ts +1 -20
- package/dist/child-process.js +2 -77
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/ipc-server.d.ts +8 -2
- package/dist/ipc-server.js +36 -15
- package/dist/journal-view.d.ts +9 -0
- package/dist/journal-view.js +131 -0
- package/dist/journal.d.ts +60 -0
- package/dist/journal.js +322 -0
- package/dist/runner-types.d.ts +2 -0
- package/dist/runner.d.ts +20 -6
- package/dist/runner.js +176 -63
- package/dist/{watchdog.d.ts → stopping.d.ts} +12 -0
- package/dist/stopping.js +230 -0
- package/dist/tools.js +4 -3
- package/package.json +2 -2
- package/dist/watchdog.js +0 -167
package/dist/stopping.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// Stopping what a runner started, and removing what it made.
|
|
2
|
+
//
|
|
3
|
+
// A program, and every shell command it runs, leads its own process group — so
|
|
4
|
+
// that a timeout can take down everything it started. The cost is that none of
|
|
5
|
+
// them die with the runner: something has to stop them, and delete the temp
|
|
6
|
+
// directories they worked in.
|
|
7
|
+
//
|
|
8
|
+
// Never SIGKILL first. A group gets the chance to end properly — a program to
|
|
9
|
+
// close what it opened, a `trama` command to stop its own run and report it.
|
|
10
|
+
// Killed outright, nothing in it gets to finish anything. So a group is asked
|
|
11
|
+
// to stop, and killed only if it is still there when the grace period is over.
|
|
12
|
+
//
|
|
13
|
+
// That policy is carried out in two places. While the runner is there, it does
|
|
14
|
+
// it itself and waits for the result (stopGroups). When the runner is gone, it
|
|
15
|
+
// cannot: a process that is killed outright, or runs out of memory, or
|
|
16
|
+
// crashes, runs no code on its way out. So there is a process of its own, the
|
|
17
|
+
// watchdog. The runner keeps a pipe to it open, and tells it what it has
|
|
18
|
+
// started and what has ended. When the pipe closes — the runner is gone,
|
|
19
|
+
// however it went — the watchdog stops whatever is still on its list and
|
|
20
|
+
// deletes what is left of the directories. One runner carries every run of a
|
|
21
|
+
// session, so one list covers the whole tree.
|
|
22
|
+
//
|
|
23
|
+
// Nothing here listens for a signal or for the process exiting: the process
|
|
24
|
+
// that hosts a runner keeps its own way of handling both.
|
|
25
|
+
import { spawn } from "child_process";
|
|
26
|
+
import { rmSync } from "fs";
|
|
27
|
+
/** How long a process group gets to exit after SIGTERM before it is sent SIGKILL. */
|
|
28
|
+
const KILL_GRACE_MS = 5000;
|
|
29
|
+
const STOP_POLL_MS = 25;
|
|
30
|
+
/** How long a group is waited for after SIGKILL before it is given up on. */
|
|
31
|
+
const KILL_SETTLE_MS = 1000;
|
|
32
|
+
/**
|
|
33
|
+
* Send a signal to a whole process group. Children spawned with `detached: true`
|
|
34
|
+
* lead their own group, so this also reaches whatever they started in the background.
|
|
35
|
+
* Signal 0 only probes. Returns false if the group no longer exists.
|
|
36
|
+
*/
|
|
37
|
+
export function signalGroup(pgid, signal) {
|
|
38
|
+
try {
|
|
39
|
+
process.kill(-pgid, signal);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// --- The watchdog ---
|
|
47
|
+
// Run by the watchdog process: the same policy as stopGroups below. A group
|
|
48
|
+
// the runner had already asked to stop is not asked a second time.
|
|
49
|
+
const WATCHDOG = `
|
|
50
|
+
const { rmSync } = require("fs");
|
|
51
|
+
const { grace, settle, poll } = JSON.parse(process.argv[1]);
|
|
52
|
+
const lists = { g: new Set(), d: new Set(), asked: new Set() };
|
|
53
|
+
const alive = (pgid) => { try { process.kill(-pgid, 0); return true; } catch { return false; } };
|
|
54
|
+
const signal = (pgid, name) => { try { process.kill(-pgid, name); } catch {} };
|
|
55
|
+
|
|
56
|
+
let unread = "";
|
|
57
|
+
process.stdin.setEncoding("utf8");
|
|
58
|
+
process.stdin.on("data", (chunk) => {
|
|
59
|
+
unread += chunk;
|
|
60
|
+
for (let end; (end = unread.indexOf("\\n")) !== -1; unread = unread.slice(end + 1)) {
|
|
61
|
+
const [op, list, item] = JSON.parse(unread.slice(0, end));
|
|
62
|
+
lists[list][op === "+" ? "add" : "delete"](item);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// The pipe has closed: the runner is gone.
|
|
67
|
+
let leaving = false;
|
|
68
|
+
const takeOver = () => {
|
|
69
|
+
if (leaving) return;
|
|
70
|
+
leaving = true;
|
|
71
|
+
const finish = () => {
|
|
72
|
+
for (const dir of lists.d) { try { rmSync(dir, { recursive: true, force: true }); } catch {} }
|
|
73
|
+
process.exit(0);
|
|
74
|
+
};
|
|
75
|
+
let left = [...lists.g].filter(alive);
|
|
76
|
+
for (const pgid of left) { if (!lists.asked.has(pgid)) signal(pgid, "SIGTERM"); }
|
|
77
|
+
let deadline = performance.now() + grace;
|
|
78
|
+
let killed = false;
|
|
79
|
+
const check = () => {
|
|
80
|
+
// A group seen gone is never signalled again: its id may belong to another by now.
|
|
81
|
+
left = left.filter(alive);
|
|
82
|
+
if (left.length === 0) return finish();
|
|
83
|
+
if (performance.now() < deadline) return;
|
|
84
|
+
if (killed) return finish();
|
|
85
|
+
for (const pgid of left) signal(pgid, "SIGKILL");
|
|
86
|
+
killed = true;
|
|
87
|
+
deadline = performance.now() + settle;
|
|
88
|
+
};
|
|
89
|
+
check();
|
|
90
|
+
setInterval(check, poll);
|
|
91
|
+
};
|
|
92
|
+
process.stdin.on("end", takeOver);
|
|
93
|
+
process.stdin.on("close", takeOver);
|
|
94
|
+
process.stdin.on("error", takeOver);
|
|
95
|
+
`;
|
|
96
|
+
/** What the watchdog has been told about and not yet told to forget. */
|
|
97
|
+
const listed = { g: new Set(), d: new Set(), asked: new Set() };
|
|
98
|
+
let watchdog = null;
|
|
99
|
+
const tell = (to, op, list, item) => {
|
|
100
|
+
to.stdin?.write(JSON.stringify([op, list, item]) + "\n");
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Start the watchdog if there is none, and return it — or null if it cannot be
|
|
104
|
+
* started. A watchdog that has itself died is replaced, and told again what is
|
|
105
|
+
* still listed.
|
|
106
|
+
*/
|
|
107
|
+
function startWatchdog() {
|
|
108
|
+
if (watchdog)
|
|
109
|
+
return watchdog;
|
|
110
|
+
let child;
|
|
111
|
+
try {
|
|
112
|
+
const timings = { grace: KILL_GRACE_MS, settle: KILL_SETTLE_MS, poll: STOP_POLL_MS };
|
|
113
|
+
child = spawn(process.execPath, ["-e", WATCHDOG, JSON.stringify(timings)], {
|
|
114
|
+
detached: true, // a signal sent to this process's group must not reach it
|
|
115
|
+
stdio: ["pipe", "ignore", "ignore"],
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
const lost = () => { if (watchdog === child)
|
|
122
|
+
watchdog = null; };
|
|
123
|
+
child.on("error", lost);
|
|
124
|
+
child.on("exit", lost);
|
|
125
|
+
child.stdin?.on("error", lost);
|
|
126
|
+
if (child.pid === undefined)
|
|
127
|
+
return null; // a failed spawn has no pid from the start
|
|
128
|
+
// Neither the watchdog nor the pipe to it keeps this process alive.
|
|
129
|
+
child.unref();
|
|
130
|
+
child.stdin?.unref?.();
|
|
131
|
+
watchdog = child;
|
|
132
|
+
for (const name of ["g", "d", "asked"]) {
|
|
133
|
+
for (const item of listed[name])
|
|
134
|
+
tell(child, "+", name, item);
|
|
135
|
+
}
|
|
136
|
+
return child;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Make sure there is a watchdog before anything is started that would need
|
|
140
|
+
* one. Throws if one cannot be started: running without it would mean leaving
|
|
141
|
+
* processes behind with nobody to stop them.
|
|
142
|
+
*/
|
|
143
|
+
export function requireWatchdog() {
|
|
144
|
+
if (!startWatchdog()) {
|
|
145
|
+
throw new Error("Could not start trama's watchdog process, which stops what a run started if the runner dies.");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** Put an item on one of the watchdog's lists. */
|
|
149
|
+
function enlist(on, item) {
|
|
150
|
+
listed[on].add(item);
|
|
151
|
+
const to = startWatchdog();
|
|
152
|
+
if (to)
|
|
153
|
+
tell(to, "+", on, item);
|
|
154
|
+
}
|
|
155
|
+
/** Take it off again. */
|
|
156
|
+
function delist(from, item) {
|
|
157
|
+
if (listed[from].delete(item) && watchdog)
|
|
158
|
+
tell(watchdog, "-", from, item);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Register a process group this process started: if this process ends before
|
|
162
|
+
* the returned function is called, the group is stopped.
|
|
163
|
+
*/
|
|
164
|
+
export function stopWithThisProcess(pgid) {
|
|
165
|
+
enlist("g", pgid);
|
|
166
|
+
return () => {
|
|
167
|
+
delist("g", pgid);
|
|
168
|
+
delist("asked", pgid);
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Register a temp directory this process created: if this process ends before
|
|
173
|
+
* the returned function is called, the directory is removed — once the groups
|
|
174
|
+
* that may be writing to it are gone.
|
|
175
|
+
*/
|
|
176
|
+
export function removeWithThisProcess(dir) {
|
|
177
|
+
enlist("d", dir);
|
|
178
|
+
return () => delist("d", dir);
|
|
179
|
+
}
|
|
180
|
+
/** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
|
|
181
|
+
export function removeTempDir(dir) {
|
|
182
|
+
try {
|
|
183
|
+
rmSync(dir, { recursive: true, force: true });
|
|
184
|
+
}
|
|
185
|
+
catch { /* left for the OS to reap */ }
|
|
186
|
+
}
|
|
187
|
+
// --- Stopping, while this process is there to see it through ---
|
|
188
|
+
/** Groups this process has asked to stop and is still watching. */
|
|
189
|
+
const stopping = new Map();
|
|
190
|
+
function stopGroup(pgid) {
|
|
191
|
+
// Asked once is enough: to some programs a second SIGTERM means "now".
|
|
192
|
+
const watched = stopping.get(pgid);
|
|
193
|
+
if (watched)
|
|
194
|
+
return watched;
|
|
195
|
+
if (!signalGroup(pgid, "SIGTERM"))
|
|
196
|
+
return Promise.resolve();
|
|
197
|
+
// The watchdog, should it take over from here, must not ask again either.
|
|
198
|
+
if (listed.g.has(pgid))
|
|
199
|
+
enlist("asked", pgid);
|
|
200
|
+
const gone = new Promise((resolve) => {
|
|
201
|
+
let deadline = performance.now() + KILL_GRACE_MS;
|
|
202
|
+
let killed = false;
|
|
203
|
+
const poll = setInterval(() => {
|
|
204
|
+
const alive = signalGroup(pgid, 0);
|
|
205
|
+
if (alive && performance.now() < deadline)
|
|
206
|
+
return;
|
|
207
|
+
if (alive && !killed) {
|
|
208
|
+
// Out of patience. A killed group is gone in a moment; allow it that
|
|
209
|
+
// moment, but do not wait on one that for some reason never goes.
|
|
210
|
+
signalGroup(pgid, "SIGKILL");
|
|
211
|
+
killed = true;
|
|
212
|
+
deadline = performance.now() + KILL_SETTLE_MS;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
clearInterval(poll);
|
|
216
|
+
stopping.delete(pgid);
|
|
217
|
+
resolve();
|
|
218
|
+
}, STOP_POLL_MS);
|
|
219
|
+
});
|
|
220
|
+
stopping.set(pgid, gone);
|
|
221
|
+
return gone;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Stop process groups for good: SIGTERM now, SIGKILL after the grace period to
|
|
225
|
+
* whichever are still there. Resolves when none is left — so that what follows
|
|
226
|
+
* (merging a directory they were writing to, deleting it) finds them gone.
|
|
227
|
+
*/
|
|
228
|
+
export async function stopGroups(pgids) {
|
|
229
|
+
await Promise.all(pgids.map(stopGroup));
|
|
230
|
+
}
|
package/dist/tools.js
CHANGED
|
@@ -2,8 +2,8 @@ import { spawn } from "child_process";
|
|
|
2
2
|
import { readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
3
3
|
import { resolve, dirname } from "path";
|
|
4
4
|
import { guardPath } from "./path-guard.js";
|
|
5
|
-
import {
|
|
6
|
-
import { stopWithThisProcess } from "./
|
|
5
|
+
import { superviseChild } from "./child-process.js";
|
|
6
|
+
import { signalGroup, stopGroups, stopWithThisProcess } from "./stopping.js";
|
|
7
7
|
const MAX_FETCH_BODY = 10 * 1024 * 1024; // 10 MB
|
|
8
8
|
const DEFAULT_SHELL_TIMEOUT = 30_000;
|
|
9
9
|
const GROUP_SWEEP_MS = 250;
|
|
@@ -86,7 +86,8 @@ export function createTools(projectDir, signal) {
|
|
|
86
86
|
const child = spawn("sh", ["-c", command], {
|
|
87
87
|
cwd,
|
|
88
88
|
env: shellEnv,
|
|
89
|
-
|
|
89
|
+
// Nothing is ever sent to a command: one that reads its input finds it at an end, rather than waiting.
|
|
90
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
90
91
|
detached: true,
|
|
91
92
|
});
|
|
92
93
|
const pgid = child.pid;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trama-dev/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "tsc"
|
|
16
|
+
"build": "rm -rf dist && tsc"
|
|
17
17
|
},
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
package/dist/watchdog.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
// What must not outlive this process: the process groups it started, and the
|
|
2
|
-
// temp directories it made.
|
|
3
|
-
//
|
|
4
|
-
// A program, and every shell command it runs, leads its own process group — so
|
|
5
|
-
// that a timeout can take down everything it started. The cost is that none of
|
|
6
|
-
// them die with the runner. Something has to stop them when the runner goes,
|
|
7
|
-
// and it cannot be the runner: a process that is killed outright, or runs out
|
|
8
|
-
// of memory, or crashes, runs no code on its way out.
|
|
9
|
-
//
|
|
10
|
-
// So it is a process of its own, the watchdog. The runner keeps a pipe to it
|
|
11
|
-
// open, and tells it what it has started and what has ended. When the pipe
|
|
12
|
-
// closes — the runner is gone, however it went — the watchdog stops whatever
|
|
13
|
-
// is still on its list and deletes what is left of the directories. One runner
|
|
14
|
-
// carries every run of a session, so one list covers the whole tree.
|
|
15
|
-
//
|
|
16
|
-
// Nothing here listens for a signal or for the process exiting: the process
|
|
17
|
-
// that hosts a runner keeps its own way of handling both.
|
|
18
|
-
import { spawn } from "child_process";
|
|
19
|
-
import { rmSync } from "fs";
|
|
20
|
-
import { STOP_TIMINGS, onGroupAskedToStop } from "./child-process.js";
|
|
21
|
-
// Run by the watchdog process. It applies the same policy as stopGroups (see
|
|
22
|
-
// child-process.ts): ask, wait out the grace period, kill what is left. A
|
|
23
|
-
// group the runner had already asked is not asked a second time.
|
|
24
|
-
const WATCHDOG = `
|
|
25
|
-
const { rmSync } = require("fs");
|
|
26
|
-
const { grace, settle, poll } = JSON.parse(process.argv[1]);
|
|
27
|
-
const lists = { g: new Set(), d: new Set(), asked: new Set() };
|
|
28
|
-
const alive = (pgid) => { try { process.kill(-pgid, 0); return true; } catch { return false; } };
|
|
29
|
-
const signal = (pgid, name) => { try { process.kill(-pgid, name); } catch {} };
|
|
30
|
-
|
|
31
|
-
let unread = "";
|
|
32
|
-
process.stdin.setEncoding("utf8");
|
|
33
|
-
process.stdin.on("data", (chunk) => {
|
|
34
|
-
unread += chunk;
|
|
35
|
-
for (let end; (end = unread.indexOf("\\n")) !== -1; unread = unread.slice(end + 1)) {
|
|
36
|
-
const [op, list, item] = JSON.parse(unread.slice(0, end));
|
|
37
|
-
lists[list][op === "+" ? "add" : "delete"](item);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// The pipe has closed: the runner is gone.
|
|
42
|
-
let leaving = false;
|
|
43
|
-
const takeOver = () => {
|
|
44
|
-
if (leaving) return;
|
|
45
|
-
leaving = true;
|
|
46
|
-
const finish = () => {
|
|
47
|
-
for (const dir of lists.d) { try { rmSync(dir, { recursive: true, force: true }); } catch {} }
|
|
48
|
-
process.exit(0);
|
|
49
|
-
};
|
|
50
|
-
let left = [...lists.g].filter(alive);
|
|
51
|
-
for (const pgid of left) { if (!lists.asked.has(pgid)) signal(pgid, "SIGTERM"); }
|
|
52
|
-
let deadline = performance.now() + grace;
|
|
53
|
-
let killed = false;
|
|
54
|
-
const check = () => {
|
|
55
|
-
// A group seen gone is never signalled again: its id may belong to another by now.
|
|
56
|
-
left = left.filter(alive);
|
|
57
|
-
if (left.length === 0) return finish();
|
|
58
|
-
if (performance.now() < deadline) return;
|
|
59
|
-
if (killed) return finish();
|
|
60
|
-
for (const pgid of left) signal(pgid, "SIGKILL");
|
|
61
|
-
killed = true;
|
|
62
|
-
deadline = performance.now() + settle;
|
|
63
|
-
};
|
|
64
|
-
check();
|
|
65
|
-
setInterval(check, poll);
|
|
66
|
-
};
|
|
67
|
-
process.stdin.on("end", takeOver);
|
|
68
|
-
process.stdin.on("close", takeOver);
|
|
69
|
-
process.stdin.on("error", takeOver);
|
|
70
|
-
`;
|
|
71
|
-
/** What this process has told the watchdog about and not yet taken back. */
|
|
72
|
-
const pending = { g: new Set(), d: new Set(), asked: new Set() };
|
|
73
|
-
let watchdog = null;
|
|
74
|
-
const tell = (to, op, list, item) => {
|
|
75
|
-
to.stdin?.write(JSON.stringify([op, list, item]) + "\n");
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Start the watchdog if there is none, and return it — or null if it cannot be
|
|
79
|
-
* started. A watchdog that has itself died is replaced, and told again what is
|
|
80
|
-
* still pending.
|
|
81
|
-
*/
|
|
82
|
-
function startWatchdog() {
|
|
83
|
-
if (watchdog)
|
|
84
|
-
return watchdog;
|
|
85
|
-
let child;
|
|
86
|
-
try {
|
|
87
|
-
child = spawn(process.execPath, ["-e", WATCHDOG, JSON.stringify(STOP_TIMINGS)], {
|
|
88
|
-
detached: true, // a signal sent to this process's group must not reach it
|
|
89
|
-
stdio: ["pipe", "ignore", "ignore"],
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
catch {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
const lost = () => { if (watchdog === child)
|
|
96
|
-
watchdog = null; };
|
|
97
|
-
child.on("error", lost);
|
|
98
|
-
child.on("exit", lost);
|
|
99
|
-
child.stdin?.on("error", lost);
|
|
100
|
-
if (child.pid === undefined)
|
|
101
|
-
return null; // a failed spawn has no pid from the start
|
|
102
|
-
// Neither the watchdog nor the pipe to it keeps this process alive.
|
|
103
|
-
child.unref();
|
|
104
|
-
child.stdin?.unref?.();
|
|
105
|
-
watchdog = child;
|
|
106
|
-
for (const list of ["g", "d", "asked"]) {
|
|
107
|
-
for (const item of pending[list])
|
|
108
|
-
tell(child, "+", list, item);
|
|
109
|
-
}
|
|
110
|
-
return child;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Make sure there is a watchdog before anything is started that would need
|
|
114
|
-
* one. Throws if one cannot be started: running without it would mean leaving
|
|
115
|
-
* processes behind with nobody to stop them.
|
|
116
|
-
*/
|
|
117
|
-
export function requireWatchdog() {
|
|
118
|
-
if (!startWatchdog()) {
|
|
119
|
-
throw new Error("Could not start trama's watchdog process, which stops what a run started if the runner dies.");
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/** Keep `item` on the watchdog's list until the returned function is called. */
|
|
123
|
-
function watch(list, item) {
|
|
124
|
-
pending[list].add(item);
|
|
125
|
-
const to = startWatchdog();
|
|
126
|
-
if (to)
|
|
127
|
-
tell(to, "+", list, item);
|
|
128
|
-
return () => {
|
|
129
|
-
if (!pending[list].delete(item))
|
|
130
|
-
return;
|
|
131
|
-
if (watchdog)
|
|
132
|
-
tell(watchdog, "-", list, item);
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Register a process group this process started: if this process ends before
|
|
137
|
-
* the returned function is called, the group is stopped.
|
|
138
|
-
*/
|
|
139
|
-
export function stopWithThisProcess(pgid) {
|
|
140
|
-
const forget = watch("g", pgid);
|
|
141
|
-
return () => {
|
|
142
|
-
forget();
|
|
143
|
-
asked.get(pgid)?.();
|
|
144
|
-
asked.delete(pgid);
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
/** Groups on the watchdog's list that this process has asked to stop, and how to take each off again. */
|
|
148
|
-
const asked = new Map();
|
|
149
|
-
onGroupAskedToStop((pgid) => {
|
|
150
|
-
if (pending.g.has(pgid) && !asked.has(pgid))
|
|
151
|
-
asked.set(pgid, watch("asked", pgid));
|
|
152
|
-
});
|
|
153
|
-
/**
|
|
154
|
-
* Register a temp directory this process created: if this process ends before
|
|
155
|
-
* the returned function is called, the directory is removed — once the groups
|
|
156
|
-
* that may be writing to it are gone.
|
|
157
|
-
*/
|
|
158
|
-
export function removeWithThisProcess(dir) {
|
|
159
|
-
return watch("d", dir);
|
|
160
|
-
}
|
|
161
|
-
/** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
|
|
162
|
-
export function removeTempDir(dir) {
|
|
163
|
-
try {
|
|
164
|
-
rmSync(dir, { recursive: true, force: true });
|
|
165
|
-
}
|
|
166
|
-
catch { /* left for the OS to reap */ }
|
|
167
|
-
}
|