@waffle-os/runtime 0.0.149 → 0.0.151

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 (41) hide show
  1. package/browser/diagnostic.ts +234 -2
  2. package/browser/process-broker.ts +42 -4
  3. package/browser/worker.ts +34 -5
  4. package/core/open-file.ts +126 -52
  5. package/dist/browser/diagnostic.js +150 -1
  6. package/dist/browser/process-broker.js +47 -2
  7. package/dist/browser/worker.js +24 -6
  8. package/dist/core/open-file.js +112 -50
  9. package/dist/fs/client.js +290 -46
  10. package/dist/fs/protocol.js +7 -4
  11. package/dist/fs/service.js +77 -16
  12. package/dist/fs/shared.js +6 -6
  13. package/dist/interrupt/rewriter-internal.js +2 -1
  14. package/dist/proc/abi.js +152 -16
  15. package/dist/proc/command-stdio.js +81 -33
  16. package/dist/proc/directory.js +111 -0
  17. package/dist/proc/errors.js +1 -0
  18. package/dist/proc/supervisor.js +130 -24
  19. package/dist/proc/table.js +9 -1
  20. package/dist/proc/types.js +5 -0
  21. package/dist/proc/worker-client.js +34 -5
  22. package/dist/runtime.js +3 -0
  23. package/dist/thread/service.js +85 -10
  24. package/dist/tooling/program/program-transformer.json +1 -1
  25. package/fs/client.ts +298 -44
  26. package/fs/protocol.ts +9 -4
  27. package/fs/service.ts +99 -19
  28. package/fs/shared.ts +7 -7
  29. package/interrupt/rewriter-internal.ts +2 -1
  30. package/package.json +1 -1
  31. package/proc/abi.ts +185 -17
  32. package/proc/command-stdio.ts +98 -41
  33. package/proc/directory.ts +131 -0
  34. package/proc/errors.ts +1 -0
  35. package/proc/supervisor.ts +173 -36
  36. package/proc/table.ts +11 -0
  37. package/proc/types.ts +6 -0
  38. package/proc/worker-client.ts +36 -5
  39. package/runtime.ts +3 -0
  40. package/thread/service.ts +125 -7
  41. package/tooling/program/program-transformer.json +1 -1
@@ -290,6 +290,117 @@ export function* directoryGroups(directory) {
290
290
  }
291
291
  }
292
292
  }
293
+ export function peekDirectoryProcess(directory, pid) {
294
+ const capacity = directoryCapacity(directory);
295
+ const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
296
+ for (let slot = 0;slot < capacity; slot++) {
297
+ const token = atomicsLoad(processes, slot * PROCESS_WORDS);
298
+ if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== pid)
299
+ continue;
300
+ try {
301
+ const state = directoryState({ directory, slot, pid });
302
+ if (controlLoad(state.jobPending, 0) >= 0)
303
+ return state;
304
+ } catch (error) {
305
+ if (!isCollectedProcess(error))
306
+ throw error;
307
+ }
308
+ }
309
+ return;
310
+ }
311
+ export function peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid) {
312
+ const matches = [];
313
+ if (!Number.isInteger(processId) || processId <= 0 || processId > 2147483647)
314
+ return matches;
315
+ const capacity = directoryCapacity(directory);
316
+ const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
317
+ for (let slot = 0;slot < capacity; slot++) {
318
+ const base = slot * PROCESS_WORDS;
319
+ const token = atomicsLoad(processes, base);
320
+ if ((token & 0xffffffffn) !== 1n)
321
+ continue;
322
+ const pid = Number(token >> 32n);
323
+ if (callerSlot !== undefined && slot === callerSlot && pid !== callerPid)
324
+ continue;
325
+ if (atomicsLoad(processes, base + PROCESS_ID) !== packedControl(pid, processId))
326
+ continue;
327
+ try {
328
+ const state = directoryState({ directory, slot, pid });
329
+ if (controlLoad(state.jobPending, 0) >= 0)
330
+ arrayAppend(matches, state);
331
+ } catch (error) {
332
+ if (!isCollectedProcess(error))
333
+ throw error;
334
+ }
335
+ }
336
+ return matches;
337
+ }
338
+ export function probeDirectoryProcess(directory, callerPid, pid) {
339
+ const target = peekDirectoryProcess(directory, pid);
340
+ if (!target)
341
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
342
+ try {
343
+ return directoryControls(directory, () => {
344
+ controlLoad(target.controlParent);
345
+ return target.pid;
346
+ }, callerPid);
347
+ } catch (error) {
348
+ if (isCollectedProcess(error))
349
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
350
+ throw error;
351
+ }
352
+ }
353
+ export function probeDirectoryLogicalPid(directory, callerPid, processId, callerSlot) {
354
+ const matches = peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid);
355
+ let found = false;
356
+ for (let index = 0;index < matches.length; index++) {
357
+ const target = matches[index];
358
+ try {
359
+ const controlled = directoryControls(directory, () => {
360
+ controlLoad(target.controlParent);
361
+ return target.pid;
362
+ }, callerPid);
363
+ found = true;
364
+ if (controlled)
365
+ return true;
366
+ } catch (error) {
367
+ if (!isCollectedProcess(error))
368
+ throw error;
369
+ }
370
+ }
371
+ if (!found)
372
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
373
+ return false;
374
+ }
375
+ export function probeDirectoryJob(directory, callerPid, job) {
376
+ const group = peekDirectoryJob(directory, job);
377
+ if (!group)
378
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
379
+ try {
380
+ return directoryControls(directory, () => controlLoad(group.controlParent), callerPid, true);
381
+ } catch (error) {
382
+ if (isCollectedProcess(error))
383
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
384
+ throw error;
385
+ }
386
+ }
387
+ export function peekDirectoryJob(directory, id) {
388
+ for (let slot = 0;slot < directoryCapacity(directory); slot++) {
389
+ const raw = row(directory, slot, true);
390
+ const token = atomicsLoad(raw, 0);
391
+ if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== id)
392
+ continue;
393
+ try {
394
+ if (controlLoad(cells(raw, id, 4)) < 0)
395
+ return;
396
+ return directoryJob(directory, slot, id);
397
+ } catch (error) {
398
+ if (!isCollectedProcess(error))
399
+ throw error;
400
+ }
401
+ }
402
+ return;
403
+ }
293
404
  export function findDirectoryJob(directory, id) {
294
405
  for (let slot = 0;slot < directoryCapacity(directory); slot++) {
295
406
  const raw = row(directory, slot, true), token = atomicsLoad(raw, 0), current = Number(token >> 32n);
@@ -16,5 +16,6 @@ export const ProcessErrno = objectFreeze({
16
16
  INVAL: 28,
17
17
  NOMEM: 48,
18
18
  NOTSUP: 58,
19
+ PERM: 63,
19
20
  SRCH: 71
20
21
  });
@@ -1,5 +1,13 @@
1
1
  import { admitProcessObservation, captureNativeReturn } from "./observation.js";
2
- import { directoryState, directoryHasProcessSibling } from "./directory.js";
2
+ import {
3
+ directoryState,
4
+ directoryHasProcessSibling,
5
+ peekDirectoryJob,
6
+ peekDirectoryProcess,
7
+ probeDirectoryJob,
8
+ probeDirectoryLogicalPid,
9
+ probeDirectoryProcess
10
+ } from "./directory.js";
3
11
  import { allocateSharedControl } from "../memory/control.js";
4
12
  import {
5
13
  isInstance,
@@ -51,7 +59,7 @@ import {
51
59
  ProcessErrno,
52
60
  ProcessError,
53
61
  Signal,
54
- checkedSignal,
62
+ checkedKillSignal,
55
63
  checkedJobLifetime,
56
64
  checkedProcessCwd,
57
65
  snapshotSpawnWorker,
@@ -281,9 +289,23 @@ export class ProcessSupervisor {
281
289
  this.access(pid, id).owner.requests.delete(id);
282
290
  return 0;
283
291
  }
284
- canSignal(callerPid, pid) {
285
- this.table.require(callerPid);
286
- this.table.require(pid);
292
+ requireLiveCaller(callerPid) {
293
+ const caller = this.table.require(callerPid);
294
+ if (this.disposed || caller.result !== null || readOutcome(caller.outcome))
295
+ throw new ProcessError(ProcessErrno.SRCH, "process has exited");
296
+ return caller;
297
+ }
298
+ callerDirectory(caller) {
299
+ return isInstance(caller.outcome, OutcomeCell) ? caller.outcome.validity.buffer : undefined;
300
+ }
301
+ isLiveDirectoryRecord(record) {
302
+ return !isInstance(record.outcome, OutcomeCell) || peekDirectoryProcess(record.outcome.validity.buffer, record.pid) !== undefined;
303
+ }
304
+ isLiveDirectoryJob(jobId) {
305
+ const directory = this.table.jobDirectory(jobId);
306
+ return !directory || peekDirectoryJob(directory, jobId) !== undefined;
307
+ }
308
+ controls(callerPid, pid) {
287
309
  let target = pid;
288
310
  while (target !== undefined && target !== 0) {
289
311
  if (target === callerPid)
@@ -292,13 +314,84 @@ export class ProcessSupervisor {
292
314
  }
293
315
  return false;
294
316
  }
317
+ canSignal(callerPid, pid) {
318
+ const caller = this.requireLiveCaller(callerPid);
319
+ const target = this.table.require(pid);
320
+ if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
321
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
322
+ return this.controls(callerPid, pid);
323
+ }
324
+ canProbe(callerPid, pid) {
325
+ const caller = this.requireLiveCaller(callerPid);
326
+ const target = this.table.get(pid);
327
+ if (target) {
328
+ if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
329
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
330
+ if (isInstance(target.outcome, OutcomeCell)) {
331
+ if (!peekDirectoryProcess(target.outcome.validity.buffer, pid))
332
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
333
+ return this.controls(callerPid, pid) || probeDirectoryProcess(target.outcome.validity.buffer, callerPid, pid);
334
+ }
335
+ return this.controls(callerPid, pid);
336
+ }
337
+ const directory = this.callerDirectory(caller);
338
+ if (directory)
339
+ return probeDirectoryProcess(directory, callerPid, pid);
340
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
341
+ }
342
+ canSignalProcess(callerPid, processId) {
343
+ const caller = this.requireLiveCaller(callerPid);
344
+ if (caller.rootPid === undefined || !Number.isInteger(processId) || processId <= 0 || processId > 2147483647)
345
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
346
+ let found = false;
347
+ for (const record of this.table.values()) {
348
+ if (record.rootPid !== caller.rootPid || record.processIdentity.pid !== processId || !this.isLiveDirectoryRecord(record))
349
+ continue;
350
+ found = true;
351
+ if (this.controls(callerPid, record.pid))
352
+ return true;
353
+ }
354
+ const directory = this.callerDirectory(caller);
355
+ if (directory) {
356
+ try {
357
+ if (probeDirectoryLogicalPid(directory, callerPid, processId))
358
+ return true;
359
+ found = true;
360
+ } catch (error) {
361
+ if (!(error instanceof ProcessError) || error.errno !== ProcessErrno.SRCH)
362
+ throw error;
363
+ }
364
+ }
365
+ if (!found)
366
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
367
+ return false;
368
+ }
295
369
  canSignalJob(callerPid, jobId) {
296
- this.table.require(callerPid);
370
+ const caller = this.requireLiveCaller(callerPid);
297
371
  const job = this.table.job(jobId);
298
- if (!job)
372
+ if (!job || caller.rootPid === undefined || job.rootPid !== caller.rootPid)
299
373
  throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
300
374
  return job.controlParent === callerPid;
301
375
  }
376
+ canProbeJob(callerPid, jobId) {
377
+ const caller = this.requireLiveCaller(callerPid);
378
+ const job = this.table.job(jobId);
379
+ if (job) {
380
+ if (caller.rootPid === undefined || job.rootPid !== caller.rootPid)
381
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
382
+ const directory = this.table.jobDirectory(jobId);
383
+ if (directory) {
384
+ if (!peekDirectoryJob(directory, jobId))
385
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
386
+ return job.controlParent === callerPid || probeDirectoryJob(directory, callerPid, jobId);
387
+ }
388
+ return job.controlParent === callerPid;
389
+ }
390
+ const directory = this.callerDirectory(caller);
391
+ if (directory)
392
+ return probeDirectoryJob(directory, callerPid, jobId);
393
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
394
+ }
302
395
  canWaitJob(callerPid, jobId) {
303
396
  if (this.table.parentOf(callerPid) === undefined)
304
397
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${callerPid}`);
@@ -387,10 +480,11 @@ export class ProcessSupervisor {
387
480
  const observation = admitProcessObservation(pid, outcome, options.observation);
388
481
  const job = options.job !== undefined && this.table.job(options.job) ? this.table.job(options.job) : this.table.createJob(pid, options.foreground ?? true, options.job, isInstance(options.outcome, OutcomeCell) ? {
389
482
  ownerPid: pid,
483
+ rootPid: pid,
390
484
  leaderPid: pid,
391
485
  outcome: options.outcome,
392
486
  foreground: options.foreground ?? true
393
- } : undefined);
487
+ } : undefined, "independent", pid);
394
488
  this.table.insert({
395
489
  pid,
396
490
  parentPid: 0,
@@ -454,20 +548,7 @@ export class ProcessSupervisor {
454
548
  const processIdentity = this.spawnIdentity(pid, processKind, parent, capturedIdentity);
455
549
  this.table.checkPid(pid, admitted ? reservation.outcome : undefined);
456
550
  const observation = admitProcessObservation(pid, outcome, reservation.observation);
457
- let job = reservation.job ?? (selector > 0 ? selector : undefined);
458
- let createdJob = false;
459
- if (job !== undefined && !this.table.job(job)) {
460
- if (reservation.job === undefined)
461
- throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
462
- this.table.createJob(jobIdentity?.ownerPid ?? parentPid, jobIdentity?.foreground ?? selector === 0, job, jobIdentity, jobLifetime);
463
- createdJob = true;
464
- }
465
- if (job === undefined) {
466
- const group = this.table.createJob(parentPid, selector === 0, undefined, undefined, jobLifetime);
467
- group.controlParent = reservation.controlParent ?? parentPid;
468
- job = group.id;
469
- createdJob = true;
470
- }
551
+ const { job, createdJob } = this.resolveSpawnJob(parentPid, parent, selector, jobLifetime, reservation, jobIdentity);
471
552
  if (jobIdentity)
472
553
  this.table.adoptJobIdentity(job, jobIdentity);
473
554
  this.checkJobAdmission(job, admitted);
@@ -532,6 +613,21 @@ export class ProcessSupervisor {
532
613
  }
533
614
  return pid;
534
615
  }
616
+ resolveSpawnJob(parentPid, parent, selector, jobLifetime, reservation, jobIdentity) {
617
+ const rootPid = reservation.rootPid ?? parent?.rootPid;
618
+ const job = reservation.job ?? (selector > 0 ? selector : undefined);
619
+ if (job === undefined) {
620
+ const group = this.table.createJob(parentPid, selector === 0, undefined, undefined, jobLifetime, rootPid);
621
+ group.controlParent = reservation.controlParent ?? parentPid;
622
+ return { job: group.id, createdJob: true };
623
+ }
624
+ if (this.table.job(job))
625
+ return { job, createdJob: false };
626
+ if (reservation.job === undefined)
627
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
628
+ this.table.createJob(jobIdentity?.ownerPid ?? parentPid, jobIdentity?.foreground ?? selector === 0, job, jobIdentity, jobLifetime, rootPid);
629
+ return { job, createdJob: true };
630
+ }
535
631
  checkSpawnParent(parentPid, parent, kind, admitted, retained) {
536
632
  if (!parent && (!admitted || !retained))
537
633
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${parentPid}`);
@@ -939,7 +1035,12 @@ export class ProcessSupervisor {
939
1035
  this.finish(pid, { kind: "exited", code });
940
1036
  }
941
1037
  kill(pid, signal) {
942
- const sig = checkedSignal(signal), record = this.table.require(pid);
1038
+ const sig = checkedKillSignal(signal), record = this.table.require(pid);
1039
+ if (sig === 0) {
1040
+ if (!this.isLiveDirectoryRecord(record))
1041
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
1042
+ return;
1043
+ }
943
1044
  if (record.result)
944
1045
  return;
945
1046
  this.recordSignal(record, sig);
@@ -993,10 +1094,15 @@ export class ProcessSupervisor {
993
1094
  this.finish(pid, winner);
994
1095
  }
995
1096
  killJob(jobId, signal) {
996
- const sig = checkedSignal(signal);
1097
+ const sig = checkedKillSignal(signal);
997
1098
  const job = this.table.job(jobId);
998
1099
  if (!job)
999
1100
  throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
1101
+ if (sig === 0) {
1102
+ if (!this.isLiveDirectoryJob(jobId))
1103
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
1104
+ return;
1105
+ }
1000
1106
  const targets = Array.from(job.members, (pid) => this.table.require(pid));
1001
1107
  for (const record of targets) {
1002
1108
  try {
@@ -62,6 +62,13 @@ export class ProcessTable {
62
62
  const outcome = this.jobs.get(id)?.leaderOutcome;
63
63
  return isInstance(outcome, OutcomeCell) && !outcome.valid();
64
64
  }
65
+ jobDirectory(id) {
66
+ const job = this.jobs.get(id);
67
+ if (!job)
68
+ return;
69
+ const outcome = job.leaderOutcome ?? (job.leaderPid === undefined ? undefined : this.records.get(job.leaderPid)?.outcome);
70
+ return isInstance(outcome, OutcomeCell) ? outcome.validity.buffer : undefined;
71
+ }
65
72
  values() {
66
73
  return this.records.values();
67
74
  }
@@ -74,7 +81,7 @@ export class ProcessTable {
74
81
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
75
82
  return record;
76
83
  }
77
- createJob(ownerPid, foreground, id = this.identities.job(), identity, jobLifetime = "independent") {
84
+ createJob(ownerPid, foreground, id = this.identities.job(), identity, jobLifetime = "independent", rootPid) {
78
85
  if (isInstance(identity?.outcome, OutcomeCell))
79
86
  identity.outcome.check();
80
87
  const captured = isInstance(identity?.outcome, OutcomeCell);
@@ -84,6 +91,7 @@ export class ProcessTable {
84
91
  const job = {
85
92
  id,
86
93
  ownerPid,
94
+ rootPid: rootPid ?? identity?.rootPid ?? this.records.get(ownerPid)?.rootPid ?? ownerPid,
87
95
  controlParent: identity?.controlParent ?? ownerPid,
88
96
  foreground,
89
97
  jobLifetime: identity?.jobLifetime ?? jobLifetime,
@@ -75,3 +75,8 @@ export function checkedSignal(signal) {
75
75
  }
76
76
  return signal;
77
77
  }
78
+ export function checkedKillSignal(signal) {
79
+ if (signal === 0)
80
+ return 0;
81
+ return checkedSignal(signal);
82
+ }
@@ -39,6 +39,8 @@ import {
39
39
  directorySlots,
40
40
  directoryJob,
41
41
  findDirectoryJob,
42
+ probeDirectoryJob,
43
+ probeDirectoryLogicalPid,
42
44
  directoryControls,
43
45
  isCollectedProcess,
44
46
  reserveDirectoryProcess,
@@ -68,6 +70,7 @@ import {
68
70
  ProcessErrno,
69
71
  ProcessError,
70
72
  checkedSignal,
73
+ checkedKillSignal,
71
74
  checkedJobLifetime,
72
75
  checkedProcessCwd,
73
76
  snapshotSpawnWorker,
@@ -290,6 +293,10 @@ export class WorkerProcessClient {
290
293
  if (commandTransferFailed(this.options.transport.commandTransfer))
291
294
  throw new ProcessError(ProcessErrno.SRCH, "command transfer cleanup failed");
292
295
  }
296
+ checkAlive() {
297
+ if (readOutcome(this.signals.outcome))
298
+ throw new ProcessError(ProcessErrno.SRCH, "process has exited");
299
+ }
293
300
  admit() {
294
301
  if (readOutcome(this.signals.outcome)) {
295
302
  this.retireAccesses();
@@ -659,6 +666,7 @@ export class WorkerProcessClient {
659
666
  canSignal(callerPid, pid) {
660
667
  if (callerPid !== this.pid)
661
668
  return false;
669
+ this.checkAlive();
662
670
  const target = this.find(pid);
663
671
  return directoryControls(this.options.transport.directory, () => {
664
672
  controlLoad(target.controlParent);
@@ -672,19 +680,40 @@ export class WorkerProcessClient {
672
680
  const group = this.requireJob(callerPid, job);
673
681
  return directoryControls(this.options.transport.directory, () => controlLoad(group.controlParent), this.pid, true);
674
682
  } catch (error) {
675
- if (error instanceof ProcessError && error.errno === 63)
683
+ if (error instanceof ProcessError && error.errno === ProcessErrno.PERM)
676
684
  return false;
677
685
  throw error;
678
686
  }
679
687
  }
688
+ canProbeJob(callerPid, job) {
689
+ if (callerPid !== this.pid)
690
+ return false;
691
+ this.checkAlive();
692
+ return probeDirectoryJob(this.options.transport.directory, this.pid, job);
693
+ }
694
+ canSignalProcess(callerPid, processId) {
695
+ if (callerPid !== this.pid)
696
+ return false;
697
+ this.checkAlive();
698
+ return probeDirectoryLogicalPid(this.options.transport.directory, callerPid, processId, this.options.transport.slot);
699
+ }
680
700
  kill(pid, signal) {
701
+ const sig = checkedKillSignal(signal);
681
702
  if (!this.canSignal(this.pid, pid))
682
- throw new ProcessError(63, "process is outside the caller's ancestry");
683
- this.deliver([this.find(pid)], checkedSignal(signal));
703
+ throw new ProcessError(ProcessErrno.PERM, "process is outside the caller's ancestry");
704
+ if (sig === 0)
705
+ return;
706
+ this.deliver([this.find(pid)], sig);
684
707
  }
685
708
  killJob(job, signal) {
709
+ const sig = checkedKillSignal(signal);
710
+ if (sig === 0) {
711
+ if (!this.canProbeJob(this.pid, job))
712
+ throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
713
+ return;
714
+ }
686
715
  if (!this.canSignalJob(this.pid, job))
687
- throw new ProcessError(63, "job is not owned by the caller");
716
+ throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
688
717
  const directory = this.options.transport.directory;
689
718
  const slots = directorySlots(directory);
690
719
  const targets = [];
@@ -699,7 +728,7 @@ export class WorkerProcessClient {
699
728
  throw error;
700
729
  }
701
730
  }
702
- this.deliver(targets, checkedSignal(signal), true);
731
+ this.deliver(targets, sig, true);
703
732
  }
704
733
  deliver(targets, signal, collective = false) {
705
734
  this.admit();
package/dist/runtime.js CHANGED
@@ -84,7 +84,10 @@ const supervisorCapabilities = {
84
84
  jobOf: "method",
85
85
  cwd: "method",
86
86
  canSignal: "method",
87
+ canProbe: "method",
87
88
  canSignalJob: "method",
89
+ canProbeJob: "method",
90
+ canSignalProcess: "method",
88
91
  canWaitJob: "method",
89
92
  accessStart: "method",
90
93
  accessTake: "method",
@@ -221,12 +221,25 @@ export class ThreadService {
221
221
  await Promise.race([previous?.catch(() => {}), this.cancelled.promise]);
222
222
  this.live();
223
223
  const lease = await this.offset(resource);
224
+ const leaseState = { current: "held" };
224
225
  try {
225
- return await this.file(operation, resource, lease.position, lease);
226
+ return await this.file(operation, resource, lease.position, lease, (next) => {
227
+ leaseState.current = next;
228
+ });
226
229
  } finally {
227
230
  if (this.phase !== "open")
228
- await this.retire(operation);
229
- lease.release();
231
+ await this.retireSequential(operation, lease, leaseState.current);
232
+ if (leaseState.current !== "deferred")
233
+ lease.release();
234
+ }
235
+ }
236
+ async retireSequential(operation, lease, leaseState) {
237
+ try {
238
+ await this.retire(operation);
239
+ } catch (error) {
240
+ if (leaseState === "settled")
241
+ lease.release();
242
+ throw error;
230
243
  }
231
244
  }
232
245
  async offset(resource) {
@@ -240,7 +253,7 @@ export class ThreadService {
240
253
  await this.wait(claim.word, claim.observed);
241
254
  }
242
255
  }
243
- async file(operation, resource, position, lease) {
256
+ async file(operation, resource, position, lease, onLeaseState) {
244
257
  this.live();
245
258
  const request = operation.request;
246
259
  if (request.kind === "tell")
@@ -251,6 +264,7 @@ export class ThreadService {
251
264
  this.position(next);
252
265
  this.live();
253
266
  lease.commit(next);
267
+ onLeaseState?.("settled");
254
268
  return { kind: "ok", value: next };
255
269
  }
256
270
  if (request.kind !== "read" && request.kind !== "write")
@@ -263,33 +277,94 @@ export class ThreadService {
263
277
  position
264
278
  ]);
265
279
  lease?.commit(position + bytes.length);
280
+ if (lease)
281
+ onLeaseState?.("settled");
266
282
  this.live();
267
283
  this.scatter(request.spans, bytes);
268
284
  return { kind: "ok", value: bytes.length };
269
285
  }
270
286
  const data = this.gather(request.spans, count);
271
287
  const append = lease?.append === true;
272
- const result = await this.request(operation, append ? "append" : "write", [resource.handle, position], data);
288
+ const result = await this.request(operation, append ? "append" : "write", [resource.handle, position], data, lease ? {
289
+ lease,
290
+ commit: (committed) => {
291
+ const committedWritten = append ? committed.written : committed;
292
+ lease.commit(append ? committed.offset : position + committedWritten);
293
+ },
294
+ onState: onLeaseState
295
+ } : undefined);
273
296
  const written = append ? result.written : result;
274
- lease?.commit(append ? result.offset : position + written);
275
297
  this.live();
276
298
  return { kind: "ok", value: written };
277
299
  }
278
- async request(operation, name, args, data) {
300
+ async request(operation, name, args, data, writeLease) {
279
301
  this.live();
280
302
  const completion = this.options.filesystem.submit(name, args, data);
281
303
  operation.completions.add(completion);
304
+ let committedApplied = false;
282
305
  try {
283
- return await completion.value();
306
+ const value = await completion.value();
307
+ if (writeLease) {
308
+ committedApplied = true;
309
+ writeLease.commit(value);
310
+ writeLease.onState?.("settled");
311
+ }
312
+ return value;
284
313
  } catch (error) {
285
314
  if (error instanceof FilesystemTransportError || isRevoked(this.options.filesystem.connection))
286
315
  this.cancelAll();
287
316
  throw error;
288
317
  } finally {
289
- if (this.phase !== "open")
290
- await this.retire(operation);
318
+ await this.finishRequest(operation, completion, committedApplied, writeLease);
319
+ }
320
+ }
321
+ async finishRequest(operation, completion, committedApplied, writeLease) {
322
+ const retiredError = this.phase !== "open" ? await this.tryRetire(operation) : undefined;
323
+ if (writeLease && !committedApplied) {
324
+ this.settleWriteLease(operation, completion, writeLease, retiredError === undefined);
325
+ } else if (retiredError === undefined || committedApplied || completion.backendSettled) {
326
+ completion.release();
327
+ operation.completions.delete(completion);
328
+ }
329
+ if (retiredError !== undefined)
330
+ throw retiredError;
331
+ }
332
+ settleWriteLease(operation, completion, writeLease, retiredCleanly) {
333
+ if (retiredCleanly || completion.backendSettled) {
334
+ this.consumeCommitted(completion, writeLease.commit);
335
+ writeLease.onState?.("settled");
291
336
  completion.release();
292
337
  operation.completions.delete(completion);
338
+ return;
339
+ }
340
+ writeLease.onState?.("deferred");
341
+ this.options.filesystem.trackDeferredWrite(completion, () => {
342
+ try {
343
+ this.consumeCommitted(completion, writeLease.commit);
344
+ } finally {
345
+ completion.release();
346
+ operation.completions.delete(completion);
347
+ writeLease.lease.release();
348
+ }
349
+ });
350
+ }
351
+ async tryRetire(operation) {
352
+ try {
353
+ await this.retire(operation);
354
+ return;
355
+ } catch (error) {
356
+ return error;
357
+ }
358
+ }
359
+ consumeCommitted(completion, onCommitted) {
360
+ try {
361
+ const committed = completion.tryConsumeCommitted();
362
+ if (committed === undefined)
363
+ return false;
364
+ onCommitted(committed);
365
+ return true;
366
+ } catch {
367
+ return false;
293
368
  }
294
369
  }
295
370
  async stream(operation) {
@@ -1 +1 @@
1
- {"commit":"7b0a762d210309fb18e4772f1a1cb50d96602dc1","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
1
+ {"commit":"da522cacff05cf6f18e820967a2e0f52d2f19680","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}