@waffle-os/runtime 0.0.145 → 0.0.146
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/README.md +32 -0
- package/browser/command-input.ts +86 -0
- package/browser/command-owner.ts +5 -0
- package/browser/display/guest.ts +2 -18
- package/browser/display/surface-info.ts +19 -0
- package/browser/operations/cells.ts +90 -0
- package/browser/operations/guest.ts +94 -0
- package/browser/operations/owner.ts +201 -0
- package/browser/operations/protocol.ts +16 -0
- package/browser/operations/types.ts +41 -0
- package/browser/operations/writer.ts +106 -0
- package/browser/preview1-worker.ts +8 -1
- package/browser/process-broker.ts +372 -22
- package/browser/process-start.ts +5 -2
- package/browser/run-command.ts +388 -0
- package/browser/shared-fs.ts +20 -3
- package/browser/worker-memory.ts +6 -0
- package/browser/worker.ts +121 -27
- package/dist/browser/command-input.js +82 -0
- package/dist/browser/command-owner.js +2 -0
- package/dist/browser/display/guest.js +2 -18
- package/dist/browser/display/surface-info.js +18 -0
- package/dist/browser/operations/cells.js +80 -0
- package/dist/browser/operations/guest.js +67 -0
- package/dist/browser/operations/owner.js +175 -0
- package/dist/browser/operations/protocol.js +0 -0
- package/dist/browser/operations/types.js +0 -0
- package/dist/browser/operations/writer.js +98 -0
- package/dist/browser/preview1-worker.js +4 -0
- package/dist/browser/process-broker.js +317 -18
- package/dist/browser/process-start.js +3 -0
- package/dist/browser/run-command.js +377 -0
- package/dist/browser/shared-fs.js +8 -3
- package/dist/browser/worker-memory.js +10 -1
- package/dist/browser/worker.js +83 -28
- package/dist/fs/client.js +81 -18
- package/dist/fs/resolve-observation.js +11 -0
- package/dist/fs/service.js +51 -4
- package/dist/proc/command-transfer.js +72 -0
- package/dist/proc/directory.js +9 -1
- package/dist/proc/intrinsics/host.js +4 -1
- package/dist/proc/intrinsics.js +1 -0
- package/dist/proc/pipes.js +37 -4
- package/dist/proc/registry.js +25 -14
- package/dist/proc/start-command.js +101 -0
- package/dist/proc/supervisor.js +34 -10
- package/dist/proc/worker-broker.js +86 -31
- package/dist/proc/worker-client.js +67 -31
- package/dist/programs/collection.js +2 -0
- package/dist/runtime.js +14 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +113 -17
- package/fs/resolve-observation.ts +25 -0
- package/fs/service.ts +63 -4
- package/index.ts +5 -0
- package/package.json +9 -1
- package/proc/command-transfer.ts +96 -0
- package/proc/directory.ts +10 -1
- package/proc/intrinsics/host.ts +5 -1
- package/proc/intrinsics.ts +1 -0
- package/proc/pipes.ts +39 -6
- package/proc/registry.ts +52 -17
- package/proc/start-command.ts +157 -0
- package/proc/supervisor.ts +34 -9
- package/proc/worker-broker.ts +100 -38
- package/proc/worker-client.ts +84 -31
- package/programs/collection.ts +4 -0
- package/runtime.ts +44 -3
- package/scripts/test-browser.ts +49 -29
- package/tests/browser-controls-discovery.test.ts +2 -10
- package/tests/browser-matrix.test.ts +105 -59
- package/tests/browser-navigation.test.ts +1 -1
- package/tests/browser-runner-lifecycle.ts +23 -13
- package/tests/command-input.test.ts +40 -0
- package/tests/command-scope.test.ts +422 -0
- package/tests/filesystem-client-intrinsics.test.ts +2 -0
- package/tests/filesystem-refcount.test.ts +2 -0
- package/tests/filesystem-resolve-observation.test.ts +185 -0
- package/tests/fixtures/command-bounded-admission.ts +31 -0
- package/tests/fixtures/command-disposal.ts +47 -0
- package/tests/fixtures/command-guests.ts +123 -0
- package/tests/fixtures/operation-fs-worker.ts +114 -0
- package/tests/fixtures/operation-observer-worker.ts +41 -0
- package/tests/fixtures/operation-page.ts +522 -0
- package/tests/fixtures/process-start-worker.ts +32 -0
- package/tests/link-browser-runner.ts +95 -22
- package/tests/node-exports.test.ts +29 -0
- package/tests/operation-diagnostics-browser.ts +192 -0
- package/tests/operation-diagnostics.test.ts +291 -0
- package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
- package/tests/page-runner/browser-controls-runner.test.ts +20 -0
- package/tests/page-runner/browser-controls-runner.ts +1 -0
- package/tests/page-runner/browser-controls-suite.ts +20 -3
- package/tests/page-runner/control-run.ts +9 -1
- package/tests/page-runner/driver.ts +2 -0
- package/tests/page-runner/playwright-driver.ts +1 -1
- package/tests/page-runner/suite-host.ts +26 -1
- package/tests/page-runner/suite-startup.test.ts +39 -0
- package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
- package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
- package/tests/page-runner/worker-capability-controls.ts +174 -15
- package/tests/pipe-reader-reuse-races.test.ts +44 -0
- package/tests/process-start-browser-runner.ts +12 -4
- package/tests/process-start-browser.ts +66 -5
- package/tests/program-build-tools.test.ts +1 -1
- package/tests/runtime-facade.test.ts +5 -0
- package/tests/start-command.test.ts +109 -0
- package/tests/threads-guest-fixture.ts +1 -1
- package/tests/threads-guest-module.ts +6 -3
- package/tests/threads-guest.test.ts +25 -0
- package/tests/threads-runtime-browser-runner.ts +36 -13
- package/tests/threads-runtime-browser.ts +231 -92
- package/tests/threads-runtime-cases.ts +18 -1
- package/tests/threads-runtime-cleanup.ts +5 -1
- package/tests/threads-runtime-fd-browser.ts +247 -0
- package/tests/threads-runtime-fd-control.ts +3 -385
- package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
- package/tests/threads-runtime-fd-module.ts +13 -2
- package/tests/threads-runtime-fd-service.ts +23 -2
- package/tests/threads-runtime-growth-worker.ts +61 -0
- package/tests/threads-runtime-host.ts +17 -4
- package/tests/threads-runtime-modules.ts +23 -5
- package/tests/threads-runtime-regression-controls.ts +386 -0
- package/tests/threads-runtime-regression-modules.ts +242 -0
- package/tests/threads-runtime-service-barrier.ts +8 -1
- package/tests/webkit-diagnostics.ts +125 -0
- package/tests/webkit-error-output.test.ts +118 -0
- package/tests/worker-memory-bootstrap.test.ts +19 -1
- package/tests/worker-memory-broker.test.ts +16 -8
- package/tooling/ci-paths.test.ts +329 -61
- package/tooling/private-authority.ts +14 -0
- package/tooling/program/program-transformer.json +1 -1
- package/tooling/tarball-smoke.ts +3 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
function text(value) {
|
|
2
|
+
return typeof value === "string" && !value.includes("\x00") && !/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(value);
|
|
3
|
+
}
|
|
4
|
+
function path(value) {
|
|
5
|
+
return text(value) && value.startsWith("/");
|
|
6
|
+
}
|
|
7
|
+
function output(value) {
|
|
8
|
+
if (value === "closed" || value === undefined)
|
|
9
|
+
return value;
|
|
10
|
+
if (!value || typeof value !== "object")
|
|
11
|
+
throw new TypeError("Invalid command output");
|
|
12
|
+
const { limitBytes, onChunk } = value;
|
|
13
|
+
if (limitBytes !== undefined && (!Number.isSafeInteger(limitBytes) || limitBytes < 0))
|
|
14
|
+
throw new RangeError("Command output limit must be a nonnegative integer");
|
|
15
|
+
if (onChunk !== undefined && typeof onChunk !== "function")
|
|
16
|
+
throw new TypeError("Command output callback must be a function");
|
|
17
|
+
return { limitBytes, onChunk };
|
|
18
|
+
}
|
|
19
|
+
function snapshot(executable, options) {
|
|
20
|
+
if (options?.filesystem !== "runtime-root")
|
|
21
|
+
throw new TypeError('Commands require filesystem: "runtime-root"');
|
|
22
|
+
if (!path(executable))
|
|
23
|
+
throw new TypeError("Command executable must be an absolute path");
|
|
24
|
+
const { args, cwd = "/", env, stdin, stdout, stderr, deadlineMs } = options;
|
|
25
|
+
if (!path(cwd))
|
|
26
|
+
throw new TypeError("Command cwd must be an absolute path");
|
|
27
|
+
if (args !== undefined && !Array.isArray(args))
|
|
28
|
+
throw new TypeError("Invalid command args");
|
|
29
|
+
const argv = Array.from(args ?? []);
|
|
30
|
+
if (!argv.every(text))
|
|
31
|
+
throw new TypeError("Invalid command argument");
|
|
32
|
+
if (env !== undefined && (!env || typeof env !== "object" || Array.isArray(env)))
|
|
33
|
+
throw new TypeError("Invalid command environment");
|
|
34
|
+
const entries = Object.entries(env ?? {});
|
|
35
|
+
if (entries.some(([key, value]) => !text(key) || !text(value) || !key || key.includes("=")))
|
|
36
|
+
throw new TypeError("Invalid command environment entry");
|
|
37
|
+
if (stdin !== undefined && stdin !== "closed" && !(stdin instanceof Uint8Array) && !(stdin instanceof ReadableStream))
|
|
38
|
+
throw new TypeError("Invalid command stdin");
|
|
39
|
+
if (stdin instanceof ReadableStream && stdin.locked)
|
|
40
|
+
throw new TypeError("Command stdin is already locked");
|
|
41
|
+
if (deadlineMs !== undefined && (!Number.isInteger(deadlineMs) || deadlineMs < 1 || deadlineMs > 2147483647))
|
|
42
|
+
throw new RangeError("Command deadline must be between 1 and 2147483647 ms");
|
|
43
|
+
return {
|
|
44
|
+
filesystem: "runtime-root",
|
|
45
|
+
args: argv,
|
|
46
|
+
cwd,
|
|
47
|
+
env: Object.fromEntries(entries),
|
|
48
|
+
stdin: stdin instanceof Uint8Array ? new Uint8Array(stdin) : stdin,
|
|
49
|
+
stdout: output(stdout),
|
|
50
|
+
stderr: output(stderr),
|
|
51
|
+
deadlineMs
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function startCommand(runtime, executable, options) {
|
|
55
|
+
const started = performance.now();
|
|
56
|
+
const input = snapshot(executable, options);
|
|
57
|
+
const stopped = new AbortController;
|
|
58
|
+
let stopRequested;
|
|
59
|
+
let settled = false;
|
|
60
|
+
const stop = (reason) => {
|
|
61
|
+
if (settled || stopRequested)
|
|
62
|
+
return;
|
|
63
|
+
stopRequested = reason;
|
|
64
|
+
stopped.abort();
|
|
65
|
+
};
|
|
66
|
+
let timer;
|
|
67
|
+
if (input.deadlineMs !== undefined) {
|
|
68
|
+
const remaining = input.deadlineMs - (performance.now() - started);
|
|
69
|
+
if (remaining <= 0)
|
|
70
|
+
stop("deadline");
|
|
71
|
+
else
|
|
72
|
+
timer = setTimeout(() => stop("deadline"), Math.ceil(remaining));
|
|
73
|
+
}
|
|
74
|
+
const running = import("../browser/run-command.js").then(({ runCommand }) => runCommand(runtime, executable, input, stopped.signal));
|
|
75
|
+
let active = running;
|
|
76
|
+
const result = running.then((command) => command.result).then(({ exit, stdout, stderr }) => {
|
|
77
|
+
if (exit)
|
|
78
|
+
return { exit, stdout, stderr, ...stopRequested ? { stopRequested } : {} };
|
|
79
|
+
if (!stopRequested)
|
|
80
|
+
throw new Error("Command did not reserve its leader");
|
|
81
|
+
return { exit: null, stdout, stderr, stopRequested };
|
|
82
|
+
}).finally(() => {
|
|
83
|
+
settled = true;
|
|
84
|
+
active = undefined;
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
result,
|
|
89
|
+
async signal(signal) {
|
|
90
|
+
if (signal === 9) {
|
|
91
|
+
stop("kill");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
await (await active)?.signal(signal);
|
|
95
|
+
},
|
|
96
|
+
kill() {
|
|
97
|
+
stop("kill");
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
package/dist/proc/supervisor.js
CHANGED
|
@@ -186,6 +186,20 @@ export class ProcessSupervisor {
|
|
|
186
186
|
}
|
|
187
187
|
return complete;
|
|
188
188
|
}
|
|
189
|
+
rootConsumersRetired(rootPid) {
|
|
190
|
+
let complete = true;
|
|
191
|
+
for (const records of this.consumers.values()) {
|
|
192
|
+
for (const record of records) {
|
|
193
|
+
if (record.rootPid !== rootPid || record.pid === rootPid)
|
|
194
|
+
continue;
|
|
195
|
+
if (record.retirement?.kind === "failed")
|
|
196
|
+
throw record.retirement.error;
|
|
197
|
+
if (!record.retirement)
|
|
198
|
+
complete = false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return complete;
|
|
202
|
+
}
|
|
189
203
|
cwd(pid) {
|
|
190
204
|
return this.accessOwner(pid).image.cwd;
|
|
191
205
|
}
|
|
@@ -613,7 +627,7 @@ export class ProcessSupervisor {
|
|
|
613
627
|
const executable = await this.options.registry.resolve(image.request.executable, image.request.cwd, () => {
|
|
614
628
|
if (record.result || record.revoked || readOutcome(record.outcome))
|
|
615
629
|
throw new Error("process exited before executable admission completed");
|
|
616
|
-
}, image.request.argv);
|
|
630
|
+
}, image.request.argv, this.options.byteBackedGuestOnly?.(record.rootPid));
|
|
617
631
|
if (record.result || record.revoked)
|
|
618
632
|
return;
|
|
619
633
|
const settled = readOutcome(record.outcome);
|
|
@@ -726,10 +740,7 @@ export class ProcessSupervisor {
|
|
|
726
740
|
discard: () => {
|
|
727
741
|
if (transferred)
|
|
728
742
|
return Promise.resolve();
|
|
729
|
-
return discarded ??= Promise.resolve().then(
|
|
730
|
-
this.disposeDescriptors(staged.descriptors);
|
|
731
|
-
await staged.discard();
|
|
732
|
-
});
|
|
743
|
+
return discarded ??= Promise.resolve().then(() => staged.discard(staged.descriptors));
|
|
733
744
|
},
|
|
734
745
|
cancel: async (error = new ProcessError(ProcessErrno.SRCH, "replacement cancelled")) => {
|
|
735
746
|
if (replacement.state.phase === "done")
|
|
@@ -760,7 +771,7 @@ export class ProcessSupervisor {
|
|
|
760
771
|
};
|
|
761
772
|
try {
|
|
762
773
|
active();
|
|
763
|
-
const resolved = await this.options.registry.resolve(replacement.request.executable, replacement.request.cwd, active, replacement.request.argv);
|
|
774
|
+
const resolved = await this.options.registry.resolve(replacement.request.executable, replacement.request.cwd, active, replacement.request.argv, this.options.byteBackedGuestOnly?.(record.rootPid));
|
|
764
775
|
active();
|
|
765
776
|
if (record.processKind === "thread")
|
|
766
777
|
throw new ProcessError(ProcessErrno.NOTSUP, "exec requires a sole process leader");
|
|
@@ -825,7 +836,10 @@ export class ProcessSupervisor {
|
|
|
825
836
|
return image.stopping;
|
|
826
837
|
const stop = async () => {
|
|
827
838
|
this.revokeImage(image);
|
|
828
|
-
this.
|
|
839
|
+
if (this.options.byteBackedGuestOnly?.(record.rootPid))
|
|
840
|
+
image.descriptors.dispose();
|
|
841
|
+
else
|
|
842
|
+
this.disposeDescriptors(image.descriptors);
|
|
829
843
|
await this.options.stopImage(record.pid, image.generation, () => this.replacementActive(record, replacement));
|
|
830
844
|
image.handle = undefined;
|
|
831
845
|
};
|
|
@@ -1244,7 +1258,14 @@ export class ProcessSupervisor {
|
|
|
1244
1258
|
for (const hook of hooks)
|
|
1245
1259
|
this.safely(hook);
|
|
1246
1260
|
this.revokeImage(record.image);
|
|
1247
|
-
this.disposeDescriptors(record.image.descriptors)
|
|
1261
|
+
this.disposeDescriptors(record.image.descriptors, (error) => {
|
|
1262
|
+
if (!this.options.byteBackedGuestOnly?.(record.rootPid)) {
|
|
1263
|
+
this.report(error);
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
fenceFailed = true;
|
|
1267
|
+
this.failRetirementFence(record, error);
|
|
1268
|
+
});
|
|
1248
1269
|
let quiescence = undefined;
|
|
1249
1270
|
try {
|
|
1250
1271
|
quiescence = this.options.quiesce?.(pid);
|
|
@@ -1519,11 +1540,14 @@ export class ProcessSupervisor {
|
|
|
1519
1540
|
if (this.table.job(job.id) === job)
|
|
1520
1541
|
this.reapJob(owner, job.id);
|
|
1521
1542
|
}
|
|
1522
|
-
disposeDescriptors(descriptors) {
|
|
1543
|
+
disposeDescriptors(descriptors, onError) {
|
|
1523
1544
|
try {
|
|
1524
1545
|
descriptors.dispose();
|
|
1525
1546
|
} catch (error) {
|
|
1526
|
-
|
|
1547
|
+
if (onError)
|
|
1548
|
+
onError(error);
|
|
1549
|
+
else
|
|
1550
|
+
this.report(error);
|
|
1527
1551
|
}
|
|
1528
1552
|
}
|
|
1529
1553
|
safely(operation) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { commandExecTransfer } from "./command-transfer.js";
|
|
1
2
|
import { captureNativeReturn } from "./observation.js";
|
|
2
3
|
import { EXEC_COMPLETION_BYTES, publishExecCompletion } from "./exec.js";
|
|
3
4
|
import { allocateSharedControl } from "../memory/control.js";
|
|
@@ -384,14 +385,55 @@ export class WorkerProcessBroker {
|
|
|
384
385
|
publishExecCompletion(message.completion, result, error);
|
|
385
386
|
wakeWorkerProcess(message.transport);
|
|
386
387
|
}
|
|
387
|
-
async discardExec(message, error) {
|
|
388
|
+
async discardExec(message, error, descriptors) {
|
|
388
389
|
try {
|
|
389
|
-
this.
|
|
390
|
-
|
|
390
|
+
if (this.currentExecTransfer(message.transport))
|
|
391
|
+
await this.cleanupTransfer(message.transport, () => this.releaseExec(message.transport, descriptors));
|
|
391
392
|
} finally {
|
|
392
393
|
this.completeExec(message, -spawnError(error).errno, error);
|
|
393
394
|
}
|
|
394
395
|
}
|
|
396
|
+
currentExecTransfer(transport) {
|
|
397
|
+
try {
|
|
398
|
+
const handoff = commandExecTransfer(transport);
|
|
399
|
+
return !handoff || controlLoad(handoff) === transport.generation;
|
|
400
|
+
} catch (error) {
|
|
401
|
+
if (!isCollectedProcess(error))
|
|
402
|
+
throw error;
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
confirmExec(transport) {
|
|
407
|
+
const handoff = commandExecTransfer(transport);
|
|
408
|
+
if (!handoff)
|
|
409
|
+
return;
|
|
410
|
+
controlCas(handoff, 0, transport.generation, 0);
|
|
411
|
+
wakeWorkerProcess(transport);
|
|
412
|
+
}
|
|
413
|
+
async releaseExec(transport, descriptors, codec) {
|
|
414
|
+
const errors = this.discardResources(transport, codec);
|
|
415
|
+
try {
|
|
416
|
+
descriptors?.dispose();
|
|
417
|
+
} catch (error) {
|
|
418
|
+
errors.push(error);
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
await this.options.discardExec?.(transport);
|
|
422
|
+
} catch (error) {
|
|
423
|
+
errors.push(error);
|
|
424
|
+
}
|
|
425
|
+
this.throwCleanupErrors(errors, "Exec transfer cleanup failed");
|
|
426
|
+
this.confirmExec(transport);
|
|
427
|
+
}
|
|
428
|
+
cleanupTransfer(transport, cleanup) {
|
|
429
|
+
return this.options.transferCleanup?.(transport, cleanup) ?? cleanup();
|
|
430
|
+
}
|
|
431
|
+
throwCleanupErrors(errors, message) {
|
|
432
|
+
if (errors.length === 1)
|
|
433
|
+
throw errors[0];
|
|
434
|
+
if (errors.length)
|
|
435
|
+
throw new AggregateError(errors, message);
|
|
436
|
+
}
|
|
395
437
|
async exec(message, senderPid) {
|
|
396
438
|
const transport = message.transport;
|
|
397
439
|
let descriptors;
|
|
@@ -406,6 +448,10 @@ export class WorkerProcessBroker {
|
|
|
406
448
|
if (state.pid !== sender.pid || state.parentPid === 0 || !this.options.canExec?.(sender))
|
|
407
449
|
throw new ProcessError(ProcessErrno.NOTSUP, "exec requires an owned worker image");
|
|
408
450
|
const supervisor = await this.ready();
|
|
451
|
+
this.options.beforeAdmit?.(transport);
|
|
452
|
+
const handoff = commandExecTransfer(transport);
|
|
453
|
+
if (handoff && controlLoad(handoff) !== transport.generation)
|
|
454
|
+
throw new ProcessError(ProcessErrno.INVAL, "exec transfer is not current");
|
|
409
455
|
codec = this.codec(transport);
|
|
410
456
|
descriptors = new DescriptorTable;
|
|
411
457
|
installWorkerBindings(transport, descriptors, codec);
|
|
@@ -417,23 +463,23 @@ export class WorkerProcessBroker {
|
|
|
417
463
|
controlCas(workerProcessState(transport).imageAdmission, 0, -(transport.generation - 1), transport.generation);
|
|
418
464
|
this.accesses.revoke(transport.pid, workerProcessDirectoryId(transport));
|
|
419
465
|
this.transports.set(transport.pid, transport);
|
|
466
|
+
this.confirmExec(transport);
|
|
420
467
|
},
|
|
421
|
-
discard: () =>
|
|
422
|
-
this.discardResources(transport, codec);
|
|
423
|
-
return this.options.discardExec?.(transport);
|
|
424
|
-
}
|
|
468
|
+
discard: (staged) => this.cleanupTransfer(transport, () => this.releaseExec(transport, staged, codec))
|
|
425
469
|
});
|
|
426
470
|
} catch (error) {
|
|
427
|
-
|
|
428
|
-
descriptors?.dispose();
|
|
429
|
-
} finally {
|
|
430
|
-
await this.discardExec(message, error);
|
|
431
|
-
}
|
|
471
|
+
await this.discardExec(message, error, descriptors);
|
|
432
472
|
}
|
|
433
473
|
}
|
|
434
|
-
|
|
435
|
-
if (
|
|
474
|
+
checkSpawnReservation(transport, request, state) {
|
|
475
|
+
if (transport.generation !== 1)
|
|
476
|
+
throw new ProcessError(ProcessErrno.INVAL, "spawn image generation must be one");
|
|
477
|
+
if (checkedProcessKind(request.processKind) !== state.processKind)
|
|
436
478
|
throw new ProcessError(ProcessErrno.INVAL, "spawn kind conflicts with published reservation");
|
|
479
|
+
if (request.job <= 0 && (checkedJobLifetime(request.job, request.jobLifetime) !== state.jobLifetime || state.jobOwner !== state.parentPid))
|
|
480
|
+
throw new ProcessError(ProcessErrno.INVAL, "job lifetime conflicts with shared reservation");
|
|
481
|
+
if (transport.cwd !== request.cwd)
|
|
482
|
+
throw new ProcessError(ProcessErrno.INVAL, "spawn cwd conflicts with process transport");
|
|
437
483
|
}
|
|
438
484
|
async spawn(transport, request) {
|
|
439
485
|
const state = workerProcessState(transport);
|
|
@@ -447,13 +493,7 @@ export class WorkerProcessBroker {
|
|
|
447
493
|
let reason = "admission-failed";
|
|
448
494
|
try {
|
|
449
495
|
codec = this.codec(transport);
|
|
450
|
-
|
|
451
|
-
throw new ProcessError(ProcessErrno.INVAL, "spawn image generation must be one");
|
|
452
|
-
this.checkSpawnKind(transport, request);
|
|
453
|
-
if (request.job <= 0 && (checkedJobLifetime(request.job, request.jobLifetime) !== state.jobLifetime || state.jobOwner !== state.parentPid))
|
|
454
|
-
throw new ProcessError(ProcessErrno.INVAL, "job lifetime conflicts with shared reservation");
|
|
455
|
-
if (transport.cwd !== request.cwd)
|
|
456
|
-
throw new ProcessError(ProcessErrno.INVAL, "spawn cwd conflicts with process transport");
|
|
496
|
+
this.checkSpawnReservation(transport, request, state);
|
|
457
497
|
if (existing)
|
|
458
498
|
throw new ProcessError(ProcessErrno.SRCH, "process directory conflicts with registered identity");
|
|
459
499
|
if (this.disposed) {
|
|
@@ -471,6 +511,7 @@ export class WorkerProcessBroker {
|
|
|
471
511
|
reason = "broker-disposed";
|
|
472
512
|
throw new ProcessError(ProcessErrno.SRCH, "process broker is disposed");
|
|
473
513
|
}
|
|
514
|
+
this.options.beforeAdmit?.(transport);
|
|
474
515
|
this.transports.set(state.pid, transport);
|
|
475
516
|
reason = "reservation-failed";
|
|
476
517
|
supervisor.reserveAdmittedSpawn(state.parentPid, { ...request, fds: [] }, {
|
|
@@ -515,7 +556,7 @@ export class WorkerProcessBroker {
|
|
|
515
556
|
this.pump();
|
|
516
557
|
} catch (error) {
|
|
517
558
|
this.trace(transport, "rejected", reason);
|
|
518
|
-
await this.rejectSpawn(transport, error, codec, admitted, existing);
|
|
559
|
+
await this.cleanupTransfer(transport, () => this.rejectSpawn(transport, error, codec, admitted, existing));
|
|
519
560
|
throw error;
|
|
520
561
|
}
|
|
521
562
|
}
|
|
@@ -678,9 +719,12 @@ export class WorkerProcessBroker {
|
|
|
678
719
|
async rejectSpawn(transport, error, codec, admitted, existing) {
|
|
679
720
|
const state = workerProcessState(transport);
|
|
680
721
|
if (admitted) {
|
|
681
|
-
|
|
722
|
+
const errors = this.discardResources(transport, codec);
|
|
723
|
+
if (errors.length)
|
|
682
724
|
this.failedCleanup.add(state.pid);
|
|
683
725
|
this.supervisor.failStart(state.pid, error);
|
|
726
|
+
if (transport.commandTransfer)
|
|
727
|
+
this.throwCleanupErrors(errors, "Spawn transfer cleanup failed");
|
|
684
728
|
return;
|
|
685
729
|
}
|
|
686
730
|
if (!existing) {
|
|
@@ -688,12 +732,23 @@ export class WorkerProcessBroker {
|
|
|
688
732
|
this.failedCleanup.add(state.pid);
|
|
689
733
|
}
|
|
690
734
|
claimOutcome(state.signals.outcome, startFailure(error), undefined, describeStartError(error));
|
|
691
|
-
const
|
|
735
|
+
const errors = this.discardResources(transport, codec);
|
|
736
|
+
const cleanup = { resourcesReleased: errors.length === 0, fenced: false };
|
|
692
737
|
if (!existing)
|
|
693
738
|
this.rejectedCleanup.set(state.pid, cleanup);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
739
|
+
if (transport.commandTransfer) {
|
|
740
|
+
try {
|
|
741
|
+
await this.options.quiesce?.(state.pid, transport);
|
|
742
|
+
} catch (error) {
|
|
743
|
+
errors.push(error);
|
|
744
|
+
}
|
|
745
|
+
this.throwCleanupErrors(errors, "Spawn transfer cleanup failed");
|
|
746
|
+
await this.options.retireRejected?.(transport);
|
|
747
|
+
} else {
|
|
748
|
+
const returned = this.options.nativeReturn?.(transport) ?? captureNativeReturn(transport);
|
|
749
|
+
await this.options.quiesce?.(state.pid, transport);
|
|
750
|
+
returned();
|
|
751
|
+
}
|
|
697
752
|
cleanup.fenced = true;
|
|
698
753
|
if (!cleanup.resourcesReleased)
|
|
699
754
|
return;
|
|
@@ -706,7 +761,7 @@ export class WorkerProcessBroker {
|
|
|
706
761
|
this.publishRetirement(state.job);
|
|
707
762
|
}
|
|
708
763
|
discardResources(transport, codec) {
|
|
709
|
-
|
|
764
|
+
const errors = [];
|
|
710
765
|
for (const token of transport.resources) {
|
|
711
766
|
try {
|
|
712
767
|
if (this.options.discard)
|
|
@@ -714,11 +769,11 @@ export class WorkerProcessBroker {
|
|
|
714
769
|
else
|
|
715
770
|
(codec ??= this.codec(transport)).discard(token);
|
|
716
771
|
} catch (error) {
|
|
717
|
-
|
|
772
|
+
errors.push(error);
|
|
718
773
|
this.report(error);
|
|
719
774
|
}
|
|
720
775
|
}
|
|
721
|
-
return
|
|
776
|
+
return errors;
|
|
722
777
|
}
|
|
723
778
|
dispose() {
|
|
724
779
|
if (this.disposed)
|
|
@@ -765,7 +820,7 @@ export class WorkerProcessBroker {
|
|
|
765
820
|
wakeWorkerProcess(transport);
|
|
766
821
|
continue;
|
|
767
822
|
}
|
|
768
|
-
const released = this.discardResources(transport);
|
|
823
|
+
const released = this.discardResources(transport).length === 0;
|
|
769
824
|
const gate = cleanup.get(pid);
|
|
770
825
|
if (gate && !released)
|
|
771
826
|
gate.complete = false;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
commandExecTransfer,
|
|
3
|
+
commandTransferFailed,
|
|
4
|
+
initializeCommandExec,
|
|
5
|
+
recordCommandTransferFailure
|
|
6
|
+
} from "./command-transfer.js";
|
|
1
7
|
import { captureNativeReturn } from "./observation.js";
|
|
2
8
|
import { EXEC_COMPLETION_BYTES } from "./exec.js";
|
|
3
9
|
import { allocateSharedControl } from "../memory/control.js";
|
|
@@ -24,7 +30,7 @@ import {
|
|
|
24
30
|
import { spawnError, startFailure } from "./start-failure.js";
|
|
25
31
|
import { describeStartError } from "./start-diagnostic.js";
|
|
26
32
|
import { MAX_ACCESS_REQUESTS } from "./access.js";
|
|
27
|
-
import { controlLoad, controlStore } from "./control.js";
|
|
33
|
+
import { controlCas, controlLoad, controlStore } from "./control.js";
|
|
28
34
|
import {
|
|
29
35
|
createDirectory,
|
|
30
36
|
beginDirectoryAdmission,
|
|
@@ -84,7 +90,7 @@ export function workerProcessPipeRegistry(transport) {
|
|
|
84
90
|
}
|
|
85
91
|
export const workerProcessState = directoryState;
|
|
86
92
|
function reserveRecord(transport, pid, parentPid, job, jobOwner, jobSlot, foreground = true, jobLifetime = "independent", cwd = "/", processKind = "process") {
|
|
87
|
-
|
|
93
|
+
const reserved = {
|
|
88
94
|
...reserveDirectoryProcess(transport, pid, parentPid, job, jobOwner, jobSlot, foreground, jobLifetime, processKind),
|
|
89
95
|
generation: 1,
|
|
90
96
|
identity: transport.identity,
|
|
@@ -92,6 +98,11 @@ function reserveRecord(transport, pid, parentPid, job, jobOwner, jobSlot, foregr
|
|
|
92
98
|
resources: [],
|
|
93
99
|
cwd
|
|
94
100
|
};
|
|
101
|
+
if (!transport.commandTransfer)
|
|
102
|
+
return reserved;
|
|
103
|
+
const scoped = { ...reserved, commandTransfer: transport.commandTransfer };
|
|
104
|
+
initializeCommandExec(scoped);
|
|
105
|
+
return scoped;
|
|
95
106
|
}
|
|
96
107
|
export function workerProcessDirectoryId(transport) {
|
|
97
108
|
const header = new Uint32ArrayConstructor(transport.directory, 0, 8);
|
|
@@ -185,7 +196,7 @@ export class WorkerProcessClient {
|
|
|
185
196
|
}
|
|
186
197
|
}
|
|
187
198
|
exec(pid, request) {
|
|
188
|
-
this.
|
|
199
|
+
this.admitTransfer();
|
|
189
200
|
if (workerProcessState(this.options.transport).processKind === "thread")
|
|
190
201
|
throw new ProcessError(ProcessErrno.NOTSUP, "thread members cannot exec");
|
|
191
202
|
if ("processKind" in request)
|
|
@@ -207,12 +218,17 @@ export class WorkerProcessClient {
|
|
|
207
218
|
bindings: [],
|
|
208
219
|
resources: []
|
|
209
220
|
};
|
|
221
|
+
const handoff = commandExecTransfer(staged);
|
|
222
|
+
let published = false;
|
|
210
223
|
try {
|
|
211
|
-
transfer = this.options.prepareExec(pid);
|
|
224
|
+
transfer = this.options.prepareExec(pid, (error) => recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error));
|
|
212
225
|
codec = transfer.codec ?? codec;
|
|
213
226
|
staged.data = transfer.data;
|
|
214
227
|
this.exportBindings(staged, snapshot, sources, references, codec);
|
|
215
|
-
this.
|
|
228
|
+
this.admitTransfer();
|
|
229
|
+
if (handoff && controlCas(handoff, 0, 0, staged.generation) !== 0)
|
|
230
|
+
throw new ProcessError(ProcessErrno.NOTSUP, "an exec transfer is already pending");
|
|
231
|
+
published = true;
|
|
216
232
|
this.options.postMessage({
|
|
217
233
|
type: "runtime-proc",
|
|
218
234
|
operation: "exec",
|
|
@@ -227,10 +243,11 @@ export class WorkerProcessClient {
|
|
|
227
243
|
completion
|
|
228
244
|
});
|
|
229
245
|
} catch (error) {
|
|
230
|
-
for (let index = 0;index < staged.resources.length; index++)
|
|
231
|
-
this.discardSpawnResource(codec, staged.resources[index]);
|
|
232
246
|
try {
|
|
233
|
-
|
|
247
|
+
if (!published || !handoff)
|
|
248
|
+
this.rollbackTransfer(codec, staged.resources, transfer, "exec", staged.generation);
|
|
249
|
+
else
|
|
250
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
234
251
|
} finally {
|
|
235
252
|
transfer?.release?.();
|
|
236
253
|
}
|
|
@@ -239,11 +256,13 @@ export class WorkerProcessClient {
|
|
|
239
256
|
try {
|
|
240
257
|
transfer?.commit?.();
|
|
241
258
|
} catch (error) {
|
|
259
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
242
260
|
this.report(error);
|
|
243
261
|
}
|
|
244
262
|
try {
|
|
245
263
|
transfer?.release?.();
|
|
246
264
|
} catch (error) {
|
|
265
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
247
266
|
this.report(error);
|
|
248
267
|
}
|
|
249
268
|
return completion;
|
|
@@ -266,6 +285,11 @@ export class WorkerProcessClient {
|
|
|
266
285
|
});
|
|
267
286
|
}
|
|
268
287
|
}
|
|
288
|
+
admitTransfer() {
|
|
289
|
+
this.admit();
|
|
290
|
+
if (commandTransferFailed(this.options.transport.commandTransfer))
|
|
291
|
+
throw new ProcessError(ProcessErrno.SRCH, "command transfer cleanup failed");
|
|
292
|
+
}
|
|
269
293
|
admit() {
|
|
270
294
|
if (readOutcome(this.signals.outcome)) {
|
|
271
295
|
this.retireAccesses();
|
|
@@ -366,14 +390,20 @@ export class WorkerProcessClient {
|
|
|
366
390
|
return sources;
|
|
367
391
|
}
|
|
368
392
|
reserveSpawn(parentPid, request) {
|
|
369
|
-
this.
|
|
393
|
+
this.admitTransfer();
|
|
370
394
|
if (parentPid !== this.pid)
|
|
371
395
|
throw new ProcessError(ProcessErrno.SRCH, "invalid spawning process");
|
|
372
|
-
const
|
|
396
|
+
const endTransfer = this.options.beginSpawnTransfer?.();
|
|
397
|
+
let release;
|
|
373
398
|
try {
|
|
399
|
+
release = beginDirectoryAdmission(this.options.transport.directory);
|
|
374
400
|
return this.reserveSpawnIntent(parentPid, request);
|
|
375
401
|
} finally {
|
|
376
|
-
|
|
402
|
+
try {
|
|
403
|
+
release?.();
|
|
404
|
+
} finally {
|
|
405
|
+
endTransfer?.();
|
|
406
|
+
}
|
|
377
407
|
}
|
|
378
408
|
}
|
|
379
409
|
reserveSpawnIntent(parentPid, request) {
|
|
@@ -405,7 +435,7 @@ export class WorkerProcessClient {
|
|
|
405
435
|
let codec = this.options.codec;
|
|
406
436
|
let publishing = false;
|
|
407
437
|
try {
|
|
408
|
-
transfer = this.options.prepareSpawn?.(pid);
|
|
438
|
+
transfer = this.options.prepareSpawn?.(pid, (error) => recordCommandTransferFailure(this.options.transport, "spawn", pid, error));
|
|
409
439
|
codec = transfer?.codec ?? codec;
|
|
410
440
|
child = reserveRecord(this.options.transport, pid, this.pid, job, existingJob?.ownerPid ?? this.pid, existingJob?.slot, selector === 0, jobLifetime, cwd, processKind);
|
|
411
441
|
child.data = transfer?.data;
|
|
@@ -423,20 +453,23 @@ export class WorkerProcessClient {
|
|
|
423
453
|
request: snapshot
|
|
424
454
|
});
|
|
425
455
|
} catch (error) {
|
|
426
|
-
if (publishing)
|
|
456
|
+
if (publishing) {
|
|
457
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
427
458
|
this.releaseUncertainSpawn(transfer);
|
|
428
|
-
else
|
|
429
|
-
this.rollbackSpawn(child, transfer, codec, error);
|
|
459
|
+
} else
|
|
460
|
+
this.rollbackSpawn(pid, child, transfer, codec, error);
|
|
430
461
|
throw error;
|
|
431
462
|
}
|
|
432
463
|
try {
|
|
433
464
|
transfer?.commit?.();
|
|
434
465
|
} catch (error) {
|
|
466
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
435
467
|
this.report(error);
|
|
436
468
|
} finally {
|
|
437
469
|
try {
|
|
438
470
|
transfer?.release?.();
|
|
439
471
|
} catch (error) {
|
|
472
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
440
473
|
this.report(error);
|
|
441
474
|
}
|
|
442
475
|
}
|
|
@@ -449,16 +482,28 @@ export class WorkerProcessClient {
|
|
|
449
482
|
this.report(error);
|
|
450
483
|
}
|
|
451
484
|
}
|
|
452
|
-
|
|
485
|
+
rollbackTransfer(codec, resources, transfer, kind, target) {
|
|
486
|
+
let cleaned = true;
|
|
487
|
+
const fail = (error) => {
|
|
488
|
+
cleaned = false;
|
|
489
|
+
recordCommandTransferFailure(this.options.transport, kind, target, error);
|
|
490
|
+
this.report(error);
|
|
491
|
+
};
|
|
492
|
+
for (let index = 0;index < resources.length; index++) {
|
|
493
|
+
try {
|
|
494
|
+
codec.discard(resources[index]);
|
|
495
|
+
} catch (error) {
|
|
496
|
+
fail(error);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
453
499
|
try {
|
|
454
|
-
|
|
455
|
-
return true;
|
|
500
|
+
transfer?.cancel?.();
|
|
456
501
|
} catch (error) {
|
|
457
|
-
|
|
458
|
-
return false;
|
|
502
|
+
fail(error);
|
|
459
503
|
}
|
|
504
|
+
return cleaned;
|
|
460
505
|
}
|
|
461
|
-
rollbackSpawn(child, transfer, codec, error) {
|
|
506
|
+
rollbackSpawn(pid, child, transfer, codec, error) {
|
|
462
507
|
let cleaned = true;
|
|
463
508
|
let state;
|
|
464
509
|
try {
|
|
@@ -466,16 +511,7 @@ export class WorkerProcessClient {
|
|
|
466
511
|
state = workerProcessState(child);
|
|
467
512
|
claimOutcome(state.signals.outcome, startFailure(error), undefined, describeStartError(error));
|
|
468
513
|
}
|
|
469
|
-
|
|
470
|
-
for (let index = 0;index < resources.length; index++)
|
|
471
|
-
if (!this.discardSpawnResource(codec, resources[index]))
|
|
472
|
-
cleaned = false;
|
|
473
|
-
try {
|
|
474
|
-
transfer?.cancel?.();
|
|
475
|
-
} catch (cleanupError) {
|
|
476
|
-
cleaned = false;
|
|
477
|
-
this.report(cleanupError);
|
|
478
|
-
}
|
|
514
|
+
cleaned = this.rollbackTransfer(codec, child?.resources ?? [], transfer, "spawn", pid);
|
|
479
515
|
if (child && state && cleaned) {
|
|
480
516
|
captureNativeReturn(child)();
|
|
481
517
|
controlStore(state.confirmed, 0, 1);
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
ProgramConflictError
|
|
4
4
|
} from "./index.js";
|
|
5
5
|
import { matchesProgram, packageIdentity, defineProgramPackage } from "./catalog.js";
|
|
6
|
+
export const preview1Launch = Symbol("Preview 1 package launch");
|
|
6
7
|
|
|
7
8
|
export class ProgramCollection {
|
|
8
9
|
runtime;
|
|
@@ -253,6 +254,7 @@ export class ProgramCollection {
|
|
|
253
254
|
}
|
|
254
255
|
}
|
|
255
256
|
const preview1Launcher = (context, service) => service.launchWorker(context, {
|
|
257
|
+
[preview1Launch]: true,
|
|
256
258
|
workerUrl: new URL("../browser/preview1-worker.js", import.meta.url).href,
|
|
257
259
|
request: {
|
|
258
260
|
module: context.module,
|