@ai-ecoverse/slicc-kernel 1.31.0 → 1.31.2

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/node.js CHANGED
@@ -4492,6 +4492,7 @@ var SAB_I_TIMERS = 9;
4492
4492
  var SAB_I_MEMORY = 10;
4493
4493
  var SAB_I_TOTAL_HI = 11;
4494
4494
  var SAB_I_OFFSET_HI = 12;
4495
+ var SAB_I_KILLED = 13;
4495
4496
  var U32 = 2 ** 32;
4496
4497
  function storeU53(header3, lo, hi, value) {
4497
4498
  Atomics.store(header3, hi, Math.floor(value / U32));
@@ -7535,6 +7536,7 @@ function isWasmSyscall(req) {
7535
7536
  return typeof op === "string" && SYSCALL_OPS.has(op);
7536
7537
  }
7537
7538
  var MAX_READ = 1024 * 1024;
7539
+ var DYING = { ok: false, errno: "EINTR", message: "the process is being killed" };
7538
7540
  var HOSTS_ONLY = new Resolver({ routes: new Routes() });
7539
7541
  var WasmProcess = class {
7540
7542
  exited = false;
@@ -7543,6 +7545,7 @@ var WasmProcess = class {
7543
7545
  ignored = 0;
7544
7546
  inherited = 0;
7545
7547
  interrupt = new AbortController();
7548
+ dying = false;
7546
7549
  execChild;
7547
7550
  execTermsig;
7548
7551
  stopped = 0;
@@ -7575,7 +7578,10 @@ var WasmProcess = class {
7575
7578
  signal(sig) {
7576
7579
  if (this.execChild !== void 0) {
7577
7580
  void Promise.resolve(this.options.kill?.(this.execChild, sig)).catch(() => void 0);
7578
- return sig === SIG.KILL ? "terminate" : "forward";
7581
+ if (sig !== SIG.KILL) return "forward";
7582
+ if (!this.options.writesBack?.(this.execChild)) return "terminate";
7583
+ this.wake?.();
7584
+ return "forward";
7579
7585
  }
7580
7586
  if (sig === SIG.KILL) return "terminate";
7581
7587
  if (sig === SIG.CONT) this.cont();
@@ -7619,14 +7625,22 @@ var WasmProcess = class {
7619
7625
  this.options.onSyscall?.(req);
7620
7626
  for (; ; ) {
7621
7627
  await this.resumed;
7628
+ if (this.dying) return DYING;
7622
7629
  const stops = this.stops;
7623
7630
  const result = await this.dispatch(req);
7631
+ if (this.dying) return DYING;
7624
7632
  const restart = !result.ok && result.errno === "EINTR" && this.stops !== stops;
7625
7633
  if (restart && !this.options.hasPending?.()) continue;
7626
7634
  await this.resumed;
7627
7635
  return result;
7628
7636
  }
7629
7637
  }
7638
+ die() {
7639
+ if (this.dying) return;
7640
+ this.dying = true;
7641
+ this.interrupt.abort();
7642
+ this.wake?.();
7643
+ }
7630
7644
  async dispatch(req) {
7631
7645
  try {
7632
7646
  if (isFdSyscall(req)) return await this.fdSyscall(req);
@@ -8087,6 +8101,7 @@ var WASM_MAX_THREADS = 256;
8087
8101
 
8088
8102
  // src/kernel/host.ts
8089
8103
  var CRASHED = 70;
8104
+ var KILL_GRACE_MS = 2e3;
8090
8105
  var descIds = /* @__PURE__ */ new WeakMap();
8091
8106
  var nextDescId = 1;
8092
8107
  function descId(file) {
@@ -8110,6 +8125,41 @@ function inheritedFds(fds) {
8110
8125
  };
8111
8126
  });
8112
8127
  }
8128
+ var Dying = class {
8129
+ ending;
8130
+ timer;
8131
+ busy = 0;
8132
+ finish;
8133
+ start(code, sig, header3, process, finish) {
8134
+ if (this.ending) return;
8135
+ this.ending = { code, sig };
8136
+ this.finish = finish;
8137
+ this.arm();
8138
+ Atomics.store(header3, SAB_I_KILLED, code);
8139
+ process.die();
8140
+ }
8141
+ async serving(work) {
8142
+ this.busy++;
8143
+ clearTimeout(this.timer);
8144
+ try {
8145
+ return await work();
8146
+ } finally {
8147
+ this.busy--;
8148
+ this.arm();
8149
+ }
8150
+ }
8151
+ arm() {
8152
+ clearTimeout(this.timer);
8153
+ if (this.busy === 0 && this.finish) this.timer = setTimeout(this.finish, KILL_GRACE_MS);
8154
+ }
8155
+ clear() {
8156
+ clearTimeout(this.timer);
8157
+ }
8158
+ status(reported) {
8159
+ if (this.ending) return [this.ending.code, this.ending.sig];
8160
+ return [typeof reported === "number" ? reported : CRASHED, void 0];
8161
+ }
8162
+ };
8113
8163
  function spawnWasmProcess(opts) {
8114
8164
  const sab = new SharedArrayBuffer(SAB_HEADER_BYTES + SAB_DEFAULT_WINDOW_BYTES);
8115
8165
  const header3 = new Int32Array(sab, 0, SAB_HEADER_I32);
@@ -8121,6 +8171,7 @@ function spawnWasmProcess(opts) {
8121
8171
  forker: opts.forker,
8122
8172
  fs: opts.fs,
8123
8173
  kill: opts.kill,
8174
+ ...opts.writesBack ? { writesBack: opts.writesBack } : {},
8124
8175
  processes: opts.processes,
8125
8176
  openFiles: opts.openFiles,
8126
8177
  ...opts.nodes ? { nodes: opts.nodes } : {},
@@ -8151,18 +8202,16 @@ function spawnWasmProcess(opts) {
8151
8202
  renamed: (from, to) => holds.renamed(from, to)
8152
8203
  });
8153
8204
  const worker = opts.createWorker();
8154
- const dispatch2 = async (req) => {
8155
- if (isWasmSyscall(req)) return process.syscall(req);
8156
- return dispatchSyncFs(req);
8157
- };
8205
+ const dying = new Dying();
8206
+ const dispatch2 = (req) => dying.serving(() => isWasmSyscall(req) ? process.syscall(req) : dispatchSyncFs(req));
8158
8207
  const responder = attachSyncSabResponder(worker, sab, token, { dispatch: dispatch2 });
8159
- let settle;
8160
- const exited = new Promise((resolve) => settle = resolve);
8208
+ const { promise: exited, resolve: settle } = Promise.withResolvers();
8161
8209
  let done4 = false;
8162
8210
  let endedBy;
8163
8211
  const finish = (code, sig) => {
8164
8212
  if (done4) return;
8165
8213
  done4 = true;
8214
+ dying.clear();
8166
8215
  endedBy = sig ?? process.execTermsig;
8167
8216
  worker.removeEventListener("message", onMessage);
8168
8217
  worker.removeEventListener("error", onError);
@@ -8178,10 +8227,10 @@ function spawnWasmProcess(opts) {
8178
8227
  };
8179
8228
  const handle = (data) => {
8180
8229
  if (data?.type === WASM_PROCESS_EXIT) {
8181
- finish(typeof data.code === "number" ? data.code : CRASHED);
8230
+ finish(...dying.status(data.code));
8182
8231
  } else if (data?.type === WASM_PROCESS_ERROR) {
8183
- opts.onError?.(String(data.message));
8184
- finish(CRASHED);
8232
+ if (!dying.ending) opts.onError?.(String(data.message));
8233
+ finish(...dying.status(CRASHED));
8185
8234
  } else if (data?.type === WASM_THREAD_SPAWN && data.thread) {
8186
8235
  spawnThread(data.thread);
8187
8236
  }
@@ -8251,7 +8300,10 @@ function spawnWasmProcess(opts) {
8251
8300
  };
8252
8301
  worker.postMessage(init, opts.fork ? [opts.fork.memory.buffer] : []);
8253
8302
  const signal = (sig) => {
8254
- if (process.signal(sig) === "terminate") finish(sig === SIG.KILL ? 137 : 128 + sig, sig);
8303
+ if (process.signal(sig) !== "terminate") return;
8304
+ const code = sig === SIG.KILL ? 137 : 128 + sig;
8305
+ if (opts.program.abi !== "wasi" || done4) finish(code, sig);
8306
+ else dying.start(code, sig, header3, process, () => finish(code, sig));
8255
8307
  };
8256
8308
  const kill = (code = 137) => finish(code, code > 128 && code < 160 ? code - 128 : void 0);
8257
8309
  return {
@@ -8262,7 +8314,8 @@ function spawnWasmProcess(opts) {
8262
8314
  termsig: () => endedBy,
8263
8315
  onState: (listener) => process.onState(listener),
8264
8316
  memory: () => Atomics.load(header3, SAB_I_MEMORY) * 65536,
8265
- ignoredSignals: (fork) => process.inheritable(fork)
8317
+ ignoredSignals: (fork) => process.inheritable(fork),
8318
+ writesBack: () => opts.program.abi === "wasi" && !done4
8266
8319
  };
8267
8320
  }
8268
8321
 
@@ -10000,6 +10053,7 @@ var Launcher = class {
10000
10053
  spawner: this.spawner(pid, req.report),
10001
10054
  forker: this.forker(pid, req),
10002
10055
  kill: (target, sig) => this.kill(target, sig),
10056
+ writesBack: (target) => this.processes.get(target)?.writesBack() === true,
10003
10057
  processes: () => ({ boot: this.boot, processes: this.list() }),
10004
10058
  openFiles: this.openFiles,
10005
10059
  nodes: this.nodes,