@comonad/cocode 0.1.16 → 0.1.19

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.
Files changed (2) hide show
  1. package/bin/cocode.mjs +54 -4
  2. package/package.json +1 -1
package/bin/cocode.mjs CHANGED
@@ -15,6 +15,7 @@ const DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
15
15
  // RFC 8628: 5 seconds when the server names no interval, and 5 more after each slow_down.
16
16
  const DEFAULT_POLL_SECONDS = 5;
17
17
  const SLOW_DOWN_MS = 5_000;
18
+ const TERMINATE_GRACE_MS = 5_000;
18
19
 
19
20
  function cocodeHome() {
20
21
  const raw = (process.env.COCODE_HOME || "").trim();
@@ -285,13 +286,62 @@ async function main() {
285
286
  child.on("message", (message) => process.connected && process.send(message));
286
287
  process.on("disconnect", () => child.connected && child.disconnect());
287
288
  }
289
+ let force;
288
290
  for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
289
- process.on(signal, () => (ipc || signal !== "SIGINT") && child.kill(signal));
291
+ process.on(signal, () => {
292
+ if (!ipc && signal === "SIGINT") return;
293
+ if (child.kill(signal)) {
294
+ force ??= setTimeout(() => stopRuntimeTree(child), TERMINATE_GRACE_MS).unref();
295
+ }
296
+ });
290
297
  }
291
- child.on("error", (error) => fail(executableError(key, error.message)));
292
- child.on("exit", (code, signal) =>
293
- process.exit(code ?? 128 + (NodeOS.constants.signals[signal] || 1)),
298
+ child.on("error", (error) =>
299
+ fail(executableError(`${process.platform}-${process.arch}`, error.message)),
294
300
  );
301
+ child.on("exit", (code, signal) => {
302
+ clearTimeout(force);
303
+ process.exit(code ?? 128 + (NodeOS.constants.signals[signal] || 1));
304
+ });
305
+ }
306
+
307
+ function stopRuntimeTree(child) {
308
+ const { pid } = child;
309
+ if (pid === undefined || child.exitCode !== null || child.signalCode !== null) return;
310
+
311
+ if (process.platform !== "win32") {
312
+ const table = NodeChildProcess.spawnSync("ps", ["-A", "-o", "pid=,ppid="], {
313
+ encoding: "utf8",
314
+ timeout: 2_000,
315
+ });
316
+ if (table.status === 0) {
317
+ const parents = new Map();
318
+ const children = new Map();
319
+ for (const row of table.stdout.trim().split("\n")) {
320
+ const [childPid, parentPid] = row.trim().split(/\s+/).map(Number);
321
+ if (!Number.isSafeInteger(childPid) || childPid <= 0 || !Number.isSafeInteger(parentPid)) {
322
+ continue;
323
+ }
324
+ parents.set(childPid, parentPid);
325
+ const siblings = children.get(parentPid) ?? [];
326
+ siblings.push(childPid);
327
+ children.set(parentPid, siblings);
328
+ }
329
+ if (parents.get(pid) === process.pid) {
330
+ const descendants = [pid];
331
+ for (const ancestor of descendants) {
332
+ descendants.push(...(children.get(ancestor) ?? []));
333
+ }
334
+ for (const descendant of descendants.slice(1).toReversed()) {
335
+ try {
336
+ process.kill(descendant, "SIGKILL");
337
+ } catch (error) {
338
+ if (error.code !== "ESRCH") process.stderr.write(`cocode: ${error.message}\n`);
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+ child.kill("SIGKILL");
295
345
  }
296
346
 
297
347
  function executableError(key, detail) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comonad/cocode",
3
- "version": "0.1.16",
3
+ "version": "0.1.19",
4
4
  "description": "CoCode CLI. Signs you in, then downloads the self-contained executable for this platform.",
5
5
  "license": "MIT",
6
6
  "repository": {