@fungi.computer/spore 0.15.0 → 0.16.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.
@@ -15788,7 +15788,9 @@ var DEFAULTS = {
15788
15788
  defaultTimeoutMs: 32e4,
15789
15789
  // After SIGTERM, give the child this long to exit on its own
15790
15790
  // before sending SIGKILL.
15791
- killGraceMs: 5e3
15791
+ killGraceMs: 5e3,
15792
+ // After the shell exits, how long to wait for its output pipes to end.
15793
+ pipeDrainGraceMs: 250
15792
15794
  };
15793
15795
  function signalGroup(child, signal) {
15794
15796
  if (child.pid === void 0) return;
@@ -15799,10 +15801,42 @@ function signalGroup(child, signal) {
15799
15801
  throw err;
15800
15802
  }
15801
15803
  }
15804
+ var terminating = /* @__PURE__ */ new Set([
15805
+ "SIGTERM",
15806
+ "SIGINT",
15807
+ "SIGHUP",
15808
+ "SIGQUIT"
15809
+ ]);
15810
+ var pkillMissing = false;
15811
+ function signalSession(child, signal) {
15812
+ if (child.pid === void 0 || pkillMissing) return;
15813
+ (0, import_node_child_process.execFile)(
15814
+ "pkill",
15815
+ [`-${signal.slice(3)}`, "-s", String(child.pid)],
15816
+ { timeout: 5e3 },
15817
+ (err) => {
15818
+ if (err === null || "code" in err && err.code === 1) return;
15819
+ if ("code" in err && err.code === "ENOENT") {
15820
+ pkillMissing = true;
15821
+ console.error(
15822
+ "computerd: pkill is not installed; Stop signals only the command's process group"
15823
+ );
15824
+ return;
15825
+ }
15826
+ console.error(`computerd: pkill failed: ${err.message}`);
15827
+ }
15828
+ );
15829
+ }
15830
+ function signalTree(child, signal) {
15831
+ signalGroup(child, signal);
15832
+ signalSession(child, signal);
15833
+ }
15802
15834
  var Runner = class {
15803
15835
  db;
15804
15836
  opts;
15805
15837
  records = /* @__PURE__ */ new Map();
15838
+ /** SIGKILL back-ups for stopped commands; they outlive the command's shell. */
15839
+ backups = /* @__PURE__ */ new Set();
15806
15840
  sweepTimer;
15807
15841
  disposed = false;
15808
15842
  constructor(init) {
@@ -15861,6 +15895,7 @@ var Runner = class {
15861
15895
  };
15862
15896
  this.records.set(id, record);
15863
15897
  const onData = (name) => (chunk) => {
15898
+ if (!record.live) return;
15864
15899
  const value = new Uint8Array(chunk);
15865
15900
  const seq = log.append(name, value);
15866
15901
  record.lastOutputAt = this.opts.now();
@@ -15897,10 +15932,6 @@ var Runner = class {
15897
15932
  clearTimeout(record.timeoutTimer);
15898
15933
  record.timeoutTimer = void 0;
15899
15934
  }
15900
- if (record.killTimer !== void 0) {
15901
- clearTimeout(record.killTimer);
15902
- record.killTimer = void 0;
15903
- }
15904
15935
  if (record.heartbeatTimer !== void 0) {
15905
15936
  clearTimeout(record.heartbeatTimer);
15906
15937
  record.heartbeatTimer = void 0;
@@ -15922,7 +15953,22 @@ var Runner = class {
15922
15953
  record.subscriber = void 0;
15923
15954
  this.scheduleSweep();
15924
15955
  };
15925
- child.once("exit", finalise);
15956
+ child.once("exit", (code, signal) => {
15957
+ let settled = false;
15958
+ const settle = () => {
15959
+ if (settled) return;
15960
+ settled = true;
15961
+ clearTimeout(grace);
15962
+ child.stdout?.destroy();
15963
+ child.stderr?.destroy();
15964
+ finalise(code, signal);
15965
+ };
15966
+ child.stdout?.resume();
15967
+ child.stderr?.resume();
15968
+ const grace = setTimeout(settle, DEFAULTS.pipeDrainGraceMs);
15969
+ grace.unref?.();
15970
+ child.once("close", settle);
15971
+ });
15926
15972
  child.once("error", (err) => {
15927
15973
  if (!record.live) return;
15928
15974
  const value = new TextEncoder().encode(`spawn failed: ${err.message}
@@ -15935,11 +15981,8 @@ var Runner = class {
15935
15981
  if (timeoutMs > 0) {
15936
15982
  record.timeoutTimer = setTimeout(() => {
15937
15983
  if (!record.live) return;
15938
- signalGroup(child, "SIGTERM");
15939
- record.killTimer = setTimeout(() => {
15940
- if (!record.live) return;
15941
- signalGroup(child, "SIGKILL");
15942
- }, DEFAULTS.killGraceMs);
15984
+ signalTree(child, "SIGTERM");
15985
+ this.backUpWithKill(child);
15943
15986
  }, timeoutMs);
15944
15987
  }
15945
15988
  this.scheduleSweep();
@@ -15969,7 +16012,25 @@ var Runner = class {
15969
16012
  throw new ExecError("ENOENT", `no exec record for id ${id}`);
15970
16013
  }
15971
16014
  if (!record.live) return;
15972
- signalGroup(record.child, signal);
16015
+ signalTree(record.child, signal);
16016
+ if (terminating.has(signal)) this.backUpWithKill(record.child);
16017
+ }
16018
+ /**
16019
+ * After the grace, SIGKILL whatever still holds the command's group or
16020
+ * session. The shell usually exits on SIGTERM and finishes the record while
16021
+ * a server it started through timeout(1) keeps running, so this does not
16022
+ * wait on the record. If nothing of the session is left by then, the signal
16023
+ * finds no process; a pid reused for a new session leader inside the grace
16024
+ * is the one case it could reach, and the kernel does not reuse a pid while
16025
+ * any process still holds it as a group or session id.
16026
+ */
16027
+ backUpWithKill(child) {
16028
+ const timer = setTimeout(() => {
16029
+ this.backups.delete(timer);
16030
+ signalTree(child, "SIGKILL");
16031
+ }, DEFAULTS.killGraceMs);
16032
+ timer.unref?.();
16033
+ this.backups.add(timer);
15973
16034
  }
15974
16035
  dispose(id) {
15975
16036
  const record = this.records.get(id);
@@ -15985,6 +16046,8 @@ var Runner = class {
15985
16046
  }
15986
16047
  disposeAll() {
15987
16048
  this.disposed = true;
16049
+ for (const timer of this.backups) clearTimeout(timer);
16050
+ this.backups.clear();
15988
16051
  for (const record of [...this.records.values()]) {
15989
16052
  this.disposeRecord(record);
15990
16053
  }
@@ -16096,16 +16159,12 @@ var Runner = class {
16096
16159
  clearTimeout(record.timeoutTimer);
16097
16160
  record.timeoutTimer = void 0;
16098
16161
  }
16099
- if (record.killTimer !== void 0) {
16100
- clearTimeout(record.killTimer);
16101
- record.killTimer = void 0;
16102
- }
16103
16162
  if (record.heartbeatTimer !== void 0) {
16104
16163
  clearTimeout(record.heartbeatTimer);
16105
16164
  record.heartbeatTimer = void 0;
16106
16165
  }
16107
16166
  if (record.child.exitCode === null && record.child.signalCode === null) {
16108
- signalGroup(record.child, "SIGKILL");
16167
+ signalTree(record.child, "SIGKILL");
16109
16168
  }
16110
16169
  record.subscriber?.close();
16111
16170
  record.subscriber = void 0;