@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,377 @@
|
|
|
1
|
+
import { allocateSharedControl } from "../memory/control.js";
|
|
2
|
+
import { commandTransferBytes } from "../proc/command-transfer.js";
|
|
3
|
+
import { DescriptorTable } from "../core/descriptors.js";
|
|
4
|
+
import { commandDisposals, commandOwners } from "./command-owner.js";
|
|
5
|
+
import { controlLoad } from "../proc/control.js";
|
|
6
|
+
import { directoryJob } from "../proc/directory.js";
|
|
7
|
+
import { atomicsWaitAsync } from "../proc/intrinsics.js";
|
|
8
|
+
import { readOutcome } from "../proc/outcome.js";
|
|
9
|
+
import { checkedSignal } from "../proc/types.js";
|
|
10
|
+
import {
|
|
11
|
+
createWorkerProcessTransport,
|
|
12
|
+
wakeWorkerProcess,
|
|
13
|
+
WorkerProcessClient,
|
|
14
|
+
workerProcessState
|
|
15
|
+
} from "../proc/worker-client.js";
|
|
16
|
+
import { createRuntimeTerminalCodec } from "./runtime-terminals.js";
|
|
17
|
+
import { CommandInput } from "./command-input.js";
|
|
18
|
+
const READ = 1n << 1n;
|
|
19
|
+
const WRITE = 1n << 6n;
|
|
20
|
+
const POLL = 1n << 27n;
|
|
21
|
+
const FD_FILESTAT_GET = 1n << 21n;
|
|
22
|
+
const OUTPUT_BATCH = 32;
|
|
23
|
+
|
|
24
|
+
class Capture {
|
|
25
|
+
bytes = new Uint8Array(0);
|
|
26
|
+
limit;
|
|
27
|
+
size = 0;
|
|
28
|
+
truncated = false;
|
|
29
|
+
onChunk;
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.limit = options === "closed" ? 0 : options?.limitBytes ?? 64 * 1024;
|
|
32
|
+
this.onChunk = options === "closed" ? undefined : options?.onChunk;
|
|
33
|
+
}
|
|
34
|
+
append(data) {
|
|
35
|
+
const length = Math.min(data.length, this.limit - this.size);
|
|
36
|
+
if (length) {
|
|
37
|
+
const size = this.size + length;
|
|
38
|
+
if (size > this.bytes.length)
|
|
39
|
+
this.grow(size);
|
|
40
|
+
this.bytes.set(data.subarray(0, length), this.size);
|
|
41
|
+
this.size = size;
|
|
42
|
+
}
|
|
43
|
+
if (length < data.length)
|
|
44
|
+
this.truncated = true;
|
|
45
|
+
try {
|
|
46
|
+
const onChunk = this.onChunk;
|
|
47
|
+
if (!onChunk)
|
|
48
|
+
return;
|
|
49
|
+
const returned = onChunk(data.slice());
|
|
50
|
+
if (returned === undefined)
|
|
51
|
+
return;
|
|
52
|
+
if (returned instanceof Promise)
|
|
53
|
+
returned.catch(() => {});
|
|
54
|
+
throw new TypeError("Command output callbacks must return undefined synchronously");
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.onChunk = undefined;
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
grow(size) {
|
|
61
|
+
const capacity = Math.min(this.limit, Math.max(size, 4096, 2 * this.bytes.length));
|
|
62
|
+
const bytes = new Uint8Array(capacity);
|
|
63
|
+
bytes.set(this.bytes);
|
|
64
|
+
this.bytes = bytes;
|
|
65
|
+
}
|
|
66
|
+
result() {
|
|
67
|
+
const bytes = this.bytes.length === this.size ? this.bytes : this.bytes.slice(0, this.size);
|
|
68
|
+
this.bytes = new Uint8Array(0);
|
|
69
|
+
return { bytes, truncated: this.truncated };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class Command {
|
|
74
|
+
runtime;
|
|
75
|
+
executable;
|
|
76
|
+
options;
|
|
77
|
+
stopped;
|
|
78
|
+
stdout;
|
|
79
|
+
stderr;
|
|
80
|
+
descriptors = new DescriptorTable;
|
|
81
|
+
leader = Promise.withResolvers();
|
|
82
|
+
abortSetup = Promise.withResolvers();
|
|
83
|
+
disposeSignal;
|
|
84
|
+
root;
|
|
85
|
+
owner;
|
|
86
|
+
input;
|
|
87
|
+
outputTask;
|
|
88
|
+
outputBatch = OUTPUT_BATCH;
|
|
89
|
+
ioFailure;
|
|
90
|
+
settled = false;
|
|
91
|
+
constructor(runtime, executable, options, stopped) {
|
|
92
|
+
this.runtime = runtime;
|
|
93
|
+
this.executable = executable;
|
|
94
|
+
this.options = options;
|
|
95
|
+
this.stopped = stopped;
|
|
96
|
+
this.stdout = new Capture(options.stdout);
|
|
97
|
+
this.stderr = new Capture(options.stderr);
|
|
98
|
+
let disposing = commandDisposals.get(runtime);
|
|
99
|
+
if (!disposing)
|
|
100
|
+
commandDisposals.set(runtime, disposing = new AbortController);
|
|
101
|
+
this.disposeSignal = disposing.signal;
|
|
102
|
+
}
|
|
103
|
+
stop = () => {
|
|
104
|
+
this.abortSetup.resolve(undefined);
|
|
105
|
+
if (!this.root || this.runtime.disposed)
|
|
106
|
+
return;
|
|
107
|
+
this.stopInput();
|
|
108
|
+
try {
|
|
109
|
+
this.owner?.stopCommandRoot(this.root);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
this.ioFailure ??= { error };
|
|
112
|
+
wakeWorkerProcess(this.root);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
dispose = () => {
|
|
116
|
+
this.abortSetup.resolve(undefined);
|
|
117
|
+
if (this.root)
|
|
118
|
+
wakeWorkerProcess(this.root);
|
|
119
|
+
this.stopInput();
|
|
120
|
+
};
|
|
121
|
+
failIO = (error) => {
|
|
122
|
+
this.ioFailure ??= { error };
|
|
123
|
+
this.stop();
|
|
124
|
+
};
|
|
125
|
+
stopInput() {
|
|
126
|
+
this.input?.stop().catch(this.failIO);
|
|
127
|
+
}
|
|
128
|
+
async signal(signal) {
|
|
129
|
+
if (this.settled)
|
|
130
|
+
return;
|
|
131
|
+
const sig = checkedSignal(signal);
|
|
132
|
+
const leader = await this.leader.promise;
|
|
133
|
+
if (!leader || this.settled || this.runtime.disposed || !this.root)
|
|
134
|
+
return;
|
|
135
|
+
if (leader.state.outcome.valid() && controlLoad(leader.state.pending) > 0)
|
|
136
|
+
await this.owner?.killJob(leader.job, sig);
|
|
137
|
+
}
|
|
138
|
+
async execute() {
|
|
139
|
+
this.stopped.addEventListener("abort", this.stop, { once: true });
|
|
140
|
+
this.disposeSignal.addEventListener("abort", this.dispose, { once: true });
|
|
141
|
+
try {
|
|
142
|
+
const exit = await this.run();
|
|
143
|
+
if (this.runtime.disposed)
|
|
144
|
+
await this.disposed();
|
|
145
|
+
if (this.ioFailure)
|
|
146
|
+
throw this.ioFailure.error;
|
|
147
|
+
return { exit, stdout: this.stdout.result(), stderr: this.stderr.result() };
|
|
148
|
+
} finally {
|
|
149
|
+
this.settled = true;
|
|
150
|
+
this.leader.resolve(undefined);
|
|
151
|
+
this.stopped.removeEventListener("abort", this.stop);
|
|
152
|
+
this.disposeSignal.removeEventListener("abort", this.dispose);
|
|
153
|
+
clearTimeout(this.outputTask);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async disposed() {
|
|
157
|
+
await this.runtime.dispose();
|
|
158
|
+
throw new Error("Runtime was disposed while running a command");
|
|
159
|
+
}
|
|
160
|
+
async run() {
|
|
161
|
+
if (this.runtime.disposed)
|
|
162
|
+
return this.disposed();
|
|
163
|
+
if (this.stopped.aborted)
|
|
164
|
+
return null;
|
|
165
|
+
const service = await Promise.race([this.runtime.processes(), this.abortSetup.promise]);
|
|
166
|
+
if (this.runtime.disposed)
|
|
167
|
+
return this.disposed();
|
|
168
|
+
if (!service || this.stopped.aborted)
|
|
169
|
+
return null;
|
|
170
|
+
const owner = commandOwners.get(service);
|
|
171
|
+
if (!owner)
|
|
172
|
+
throw new Error("Process service has no command owner");
|
|
173
|
+
await Promise.race([owner.ready(), this.abortSetup.promise]);
|
|
174
|
+
if (this.runtime.disposed)
|
|
175
|
+
return this.disposed();
|
|
176
|
+
if (this.stopped.aborted)
|
|
177
|
+
return null;
|
|
178
|
+
return this.runInRoot(owner);
|
|
179
|
+
}
|
|
180
|
+
async runInRoot(owner) {
|
|
181
|
+
const transport = createWorkerProcessTransport({
|
|
182
|
+
identity: this.runtime.identities.buffer,
|
|
183
|
+
foreground: false
|
|
184
|
+
});
|
|
185
|
+
const root = {
|
|
186
|
+
...transport,
|
|
187
|
+
commandTransfer: allocateSharedControl(commandTransferBytes(transport.directory))
|
|
188
|
+
};
|
|
189
|
+
const filesystem = this.runtime.filesystem.createIdentity();
|
|
190
|
+
this.owner = owner;
|
|
191
|
+
this.root = root;
|
|
192
|
+
let adopted = false;
|
|
193
|
+
let leader;
|
|
194
|
+
try {
|
|
195
|
+
await owner.adoptCommandRoot(root, {
|
|
196
|
+
filesystem,
|
|
197
|
+
memory: [],
|
|
198
|
+
tryOutput: (fd, bytes) => this.deliver(fd, bytes),
|
|
199
|
+
stop: () => this.input?.stop()
|
|
200
|
+
});
|
|
201
|
+
adopted = true;
|
|
202
|
+
if (this.runtime.disposed)
|
|
203
|
+
return this.disposed();
|
|
204
|
+
if (!this.stopped.aborted)
|
|
205
|
+
leader = this.spawn(owner, root);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (this.runtime.disposed)
|
|
208
|
+
return this.disposed();
|
|
209
|
+
if (!adopted) {
|
|
210
|
+
this.runtime.filesystem.revoke(filesystem);
|
|
211
|
+
this.runtime.filesystem.reap(filesystem);
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
this.failIO(error);
|
|
215
|
+
} finally {
|
|
216
|
+
try {
|
|
217
|
+
this.descriptors.dispose();
|
|
218
|
+
} catch (error) {
|
|
219
|
+
this.failIO(error);
|
|
220
|
+
}
|
|
221
|
+
this.leader.resolve(leader);
|
|
222
|
+
}
|
|
223
|
+
if (this.stopped.aborted || this.ioFailure)
|
|
224
|
+
this.stop();
|
|
225
|
+
try {
|
|
226
|
+
const result = await this.join(owner, root, leader);
|
|
227
|
+
if (this.runtime.disposed)
|
|
228
|
+
return this.disposed();
|
|
229
|
+
await this.input?.stop();
|
|
230
|
+
await owner.retireCommandRoot(root);
|
|
231
|
+
return result;
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (this.runtime.disposed)
|
|
234
|
+
return this.disposed();
|
|
235
|
+
owner.stopCommandRoot(root);
|
|
236
|
+
throw error;
|
|
237
|
+
} finally {
|
|
238
|
+
await this.input?.stop();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
spawn(owner, root) {
|
|
242
|
+
let leader;
|
|
243
|
+
let writer;
|
|
244
|
+
const post = (message) => {
|
|
245
|
+
if (message.operation === "spawn") {
|
|
246
|
+
const { job, jobSlot } = workerProcessState(message.transport);
|
|
247
|
+
leader = { job, state: directoryJob(root.directory, jobSlot, job) };
|
|
248
|
+
}
|
|
249
|
+
owner.runtimeMessage(message, root.pid).catch((error) => {
|
|
250
|
+
if (message.operation !== "spawn")
|
|
251
|
+
this.failIO(error);
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
const pipes = owner.commandPipeHost(root, post);
|
|
255
|
+
const stdin = this.options.stdin;
|
|
256
|
+
try {
|
|
257
|
+
if (stdin !== undefined && stdin !== "closed") {
|
|
258
|
+
const [reader, input] = pipes.createPipe();
|
|
259
|
+
writer = input;
|
|
260
|
+
this.descriptors.openAt(0, reader, READ | POLL | FD_FILESTAT_GET, 0n);
|
|
261
|
+
}
|
|
262
|
+
this.openTerminals(stdin);
|
|
263
|
+
const codec = createRuntimeTerminalCodec({
|
|
264
|
+
descriptors: this.descriptors,
|
|
265
|
+
fallback: pipes.codec()
|
|
266
|
+
});
|
|
267
|
+
const client = new WorkerProcessClient({
|
|
268
|
+
transport: root,
|
|
269
|
+
descriptors: this.descriptors,
|
|
270
|
+
codec,
|
|
271
|
+
postMessage: post,
|
|
272
|
+
prepareSpawn: (pid, cleanupFailed) => this.prepareChild(pipes, pid, cleanupFailed)
|
|
273
|
+
});
|
|
274
|
+
client.spawn(root.pid, {
|
|
275
|
+
executable: this.executable,
|
|
276
|
+
argv: [this.executable].concat(this.options.args ?? []),
|
|
277
|
+
env: { ...this.options.env },
|
|
278
|
+
cwd: this.options.cwd ?? "/",
|
|
279
|
+
job: -1,
|
|
280
|
+
jobLifetime: "caller-fatal",
|
|
281
|
+
fds: this.descriptors.inheritable().map((fd) => ({ parent: fd, child: fd }))
|
|
282
|
+
});
|
|
283
|
+
if (writer && stdin !== undefined && stdin !== "closed")
|
|
284
|
+
this.input = new CommandInput(writer, stdin, this.failIO);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
writer?.close();
|
|
287
|
+
if (!leader)
|
|
288
|
+
throw error;
|
|
289
|
+
this.failIO(error);
|
|
290
|
+
}
|
|
291
|
+
if (!leader)
|
|
292
|
+
throw new Error("Command leader was not published");
|
|
293
|
+
return leader;
|
|
294
|
+
}
|
|
295
|
+
prepareChild(pipes, pid, cleanupFailed) {
|
|
296
|
+
const filesystem = this.runtime.filesystem.createIdentity();
|
|
297
|
+
let enrolled = false;
|
|
298
|
+
const cancel = () => {
|
|
299
|
+
this.runtime.filesystem.revoke(filesystem);
|
|
300
|
+
this.runtime.filesystem.reap(filesystem);
|
|
301
|
+
if (enrolled)
|
|
302
|
+
pipes.registry.collectProcess(pid);
|
|
303
|
+
};
|
|
304
|
+
try {
|
|
305
|
+
pipes.registry.registerProcess(pid);
|
|
306
|
+
enrolled = true;
|
|
307
|
+
return { data: { filesystem, pipeRegistry: pipes.registry.handle() }, cancel };
|
|
308
|
+
} catch (error) {
|
|
309
|
+
try {
|
|
310
|
+
cancel();
|
|
311
|
+
} catch (cleanup) {
|
|
312
|
+
cleanupFailed?.(cleanup);
|
|
313
|
+
}
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
openTerminals(stdin) {
|
|
318
|
+
for (const fd of [0, 1, 2]) {
|
|
319
|
+
if (fd === 0 && stdin !== undefined || fd === 1 && this.options.stdout === "closed" || fd === 2 && this.options.stderr === "closed")
|
|
320
|
+
continue;
|
|
321
|
+
this.descriptors.openAt(fd, {
|
|
322
|
+
kind: "terminal",
|
|
323
|
+
read: () => 0,
|
|
324
|
+
write: () => 0,
|
|
325
|
+
readiness: () => ({ readable: fd === 0, writable: fd !== 0, hangup: fd === 0 })
|
|
326
|
+
}, (fd === 0 ? READ : WRITE) | POLL | FD_FILESTAT_GET, 0n);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
deliver(fd, bytes) {
|
|
330
|
+
if (this.outputBatch === 0) {
|
|
331
|
+
this.outputTask ??= setTimeout(() => {
|
|
332
|
+
this.outputTask = undefined;
|
|
333
|
+
this.outputBatch = OUTPUT_BATCH;
|
|
334
|
+
if (!this.root || this.runtime.disposed)
|
|
335
|
+
return;
|
|
336
|
+
this.owner?.outputDrained(this.root.pid);
|
|
337
|
+
wakeWorkerProcess(this.root);
|
|
338
|
+
}, 0);
|
|
339
|
+
return 0;
|
|
340
|
+
}
|
|
341
|
+
this.outputBatch--;
|
|
342
|
+
if (this.settled)
|
|
343
|
+
return bytes.length;
|
|
344
|
+
try {
|
|
345
|
+
(fd === 1 ? this.stdout : this.stderr).append(bytes);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
this.failIO(error);
|
|
348
|
+
}
|
|
349
|
+
return bytes.length;
|
|
350
|
+
}
|
|
351
|
+
async join(owner, root, leader) {
|
|
352
|
+
const wake = workerProcessState(root).wake;
|
|
353
|
+
let exit = null;
|
|
354
|
+
for (;; ) {
|
|
355
|
+
const expected = Atomics.load(wake, 0);
|
|
356
|
+
if (this.runtime.disposed)
|
|
357
|
+
return this.disposed();
|
|
358
|
+
if (this.stopped.aborted || this.ioFailure)
|
|
359
|
+
this.stopInput();
|
|
360
|
+
if (leader && !exit)
|
|
361
|
+
exit = readOutcome(leader.state.outcome);
|
|
362
|
+
if (owner.checkCommandCleanup(root, !leader || exit !== null)) {
|
|
363
|
+
if (!leader || exit)
|
|
364
|
+
return exit;
|
|
365
|
+
throw new Error("Command leader retired without an outcome");
|
|
366
|
+
}
|
|
367
|
+
await atomicsWaitAsync(wake, 0, expected).value;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
export function runCommand(runtime, executable, options, stopped) {
|
|
372
|
+
const command = new Command(runtime, executable, options, stopped);
|
|
373
|
+
return {
|
|
374
|
+
result: command.execute(),
|
|
375
|
+
signal: (signal) => command.signal(signal)
|
|
376
|
+
};
|
|
377
|
+
}
|
|
@@ -385,6 +385,12 @@ export class SharedFilesystem {
|
|
|
385
385
|
return this.client.fork();
|
|
386
386
|
}
|
|
387
387
|
connect(identity, headroom = null) {
|
|
388
|
+
return this.#connect(identity, headroom);
|
|
389
|
+
}
|
|
390
|
+
connectObserved(identity, headroom, operations) {
|
|
391
|
+
return this.#connect(identity, headroom ?? null, operations);
|
|
392
|
+
}
|
|
393
|
+
#connect(identity, headroom, operations) {
|
|
388
394
|
this.assertLive();
|
|
389
395
|
const current = {
|
|
390
396
|
...identity,
|
|
@@ -392,9 +398,8 @@ export class SharedFilesystem {
|
|
|
392
398
|
};
|
|
393
399
|
const connection = connectFilesystemIdentity(current, this.requestPool, undefined, headroom);
|
|
394
400
|
try {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
]);
|
|
401
|
+
const message = operations ? { connect: connection.server, operations } : { connect: connection.server };
|
|
402
|
+
this.postServiceConnection(connection.server, message, [connection.server.port]);
|
|
398
403
|
} catch (error) {
|
|
399
404
|
cleanupAfterFailure([
|
|
400
405
|
() => connection.client.port.close(),
|
|
@@ -25,7 +25,10 @@ const round = Math.round;
|
|
|
25
25
|
const startProgress = {
|
|
26
26
|
"runtime-loaded": 2,
|
|
27
27
|
"group-joined": 3,
|
|
28
|
-
"modules-loaded": 4
|
|
28
|
+
"modules-loaded": 4,
|
|
29
|
+
"installing-bindings": 5,
|
|
30
|
+
"bindings-installed": 6,
|
|
31
|
+
instantiating: 7
|
|
29
32
|
};
|
|
30
33
|
export function readStartProgress(value) {
|
|
31
34
|
if (value >= -1n)
|
|
@@ -39,6 +42,12 @@ export function readStartProgress(value) {
|
|
|
39
42
|
return { step: "group-joined", at };
|
|
40
43
|
if (code === startProgress["modules-loaded"])
|
|
41
44
|
return { step: "modules-loaded", at };
|
|
45
|
+
if (code === startProgress["installing-bindings"])
|
|
46
|
+
return { step: "installing-bindings", at };
|
|
47
|
+
if (code === startProgress["bindings-installed"])
|
|
48
|
+
return { step: "bindings-installed", at };
|
|
49
|
+
if (code === startProgress.instantiating)
|
|
50
|
+
return { step: "instantiating", at };
|
|
42
51
|
return;
|
|
43
52
|
}
|
|
44
53
|
export function startStep(sampler, step) {
|
package/dist/browser/worker.js
CHANGED
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
reapFilesystem,
|
|
40
40
|
sharedRevoked
|
|
41
41
|
} from "../fs/index.js";
|
|
42
|
+
import { registerResolveObservation } from "../fs/resolve-observation.js";
|
|
42
43
|
import { Wasi, WasiExit } from "../core/index.js";
|
|
43
44
|
import { guardedStream } from "./guarded-stream.js";
|
|
44
45
|
import {
|
|
@@ -71,7 +72,8 @@ import {
|
|
|
71
72
|
typedArraySubarray,
|
|
72
73
|
wasmMemoryBuffer,
|
|
73
74
|
listenAbort,
|
|
74
|
-
unlistenAbort
|
|
75
|
+
unlistenAbort,
|
|
76
|
+
isInstance
|
|
75
77
|
} from "../proc/intrinsics.js";
|
|
76
78
|
function workerFinishResult(result, started, generation = 1) {
|
|
77
79
|
const ownCode = "code" in result ? result.code : started && generation === 1 ? 1 : 126;
|
|
@@ -304,20 +306,28 @@ function requestProcessObserver(request, executor) {
|
|
|
304
306
|
validateObservationOutcome(observation, outcomeWord(executor.outcome));
|
|
305
307
|
return captureProcessObserver(observation);
|
|
306
308
|
}
|
|
307
|
-
export
|
|
309
|
+
export function bootstrap(req, executor, host = globalThis) {
|
|
310
|
+
return bootstrapWorker(req, executor, host);
|
|
311
|
+
}
|
|
312
|
+
export function bootstrapObservedPreview1(envelope) {
|
|
313
|
+
return bootstrapWorker(envelope[0], preview1Executor, globalThis, envelope[1]);
|
|
314
|
+
}
|
|
315
|
+
async function bootstrapWorker(req, executor, host, operations) {
|
|
308
316
|
const hooks = captureWorkerHost(host);
|
|
309
317
|
if (!req.controlGrant)
|
|
310
318
|
throw new TypeError("Worker bootstrap requires a shared control grant");
|
|
311
319
|
const allocateShared = createSharedControlAllocator(req.controlGrant, (buffer) => hooks.postToHost({ type: "shared-control-allocation", buffer }));
|
|
312
320
|
const restoreAllocator = installSharedControlAllocator(allocateShared);
|
|
313
321
|
try {
|
|
314
|
-
await bootstrapAllocated(req, executor, host, hooks, allocateShared);
|
|
322
|
+
await bootstrapAllocated(req, executor, host, hooks, allocateShared, operations);
|
|
315
323
|
} finally {
|
|
316
324
|
restoreAllocator();
|
|
317
325
|
}
|
|
318
326
|
}
|
|
319
|
-
async function bootstrapAllocated(req, executor, host, { postToHost, addListener, removeListener }, allocateShared) {
|
|
327
|
+
async function bootstrapAllocated(req, executor, host, { postToHost, addListener, removeListener }, allocateShared, operations) {
|
|
320
328
|
postToHost({ type: "phase", name: "worker-start", duration: 0 });
|
|
329
|
+
let started = false;
|
|
330
|
+
const recording = operations ? await captureOperations(operations, req.filesystem, () => started) : undefined;
|
|
321
331
|
const fs = new FilesystemClient(req.filesystem);
|
|
322
332
|
const { cancellation, notifications, startupCancelled, cancelStartup } = startupControl(req.notifications, fs);
|
|
323
333
|
let runtimeProcess = null;
|
|
@@ -427,7 +437,6 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
427
437
|
let groupMember = noGroup;
|
|
428
438
|
let releaseStartupStderr;
|
|
429
439
|
let done = false;
|
|
430
|
-
let started = false;
|
|
431
440
|
let socketNetwork;
|
|
432
441
|
const releaseNetworkPort = captureNetworkPort(req.network);
|
|
433
442
|
const releaseGraphicsPort = captureNetworkPort(req.graphics);
|
|
@@ -894,7 +903,7 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
894
903
|
listenAbort(cancellation.signal, hook);
|
|
895
904
|
return () => unlistenAbort(cancellation.signal, hook);
|
|
896
905
|
},
|
|
897
|
-
prepareExec: () => {
|
|
906
|
+
prepareExec: (_pid, cleanupFailed) => {
|
|
898
907
|
checkActive();
|
|
899
908
|
const end = fs.beginTransfer();
|
|
900
909
|
let filesystem;
|
|
@@ -919,15 +928,17 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
919
928
|
} catch (error) {
|
|
920
929
|
try {
|
|
921
930
|
cancel();
|
|
931
|
+
} catch (cleanup) {
|
|
932
|
+
cleanupFailed?.(cleanup);
|
|
922
933
|
} finally {
|
|
923
934
|
end();
|
|
924
935
|
}
|
|
925
936
|
throw error;
|
|
926
937
|
}
|
|
927
938
|
},
|
|
928
|
-
|
|
939
|
+
beginSpawnTransfer: () => fs.beginTransfer(),
|
|
940
|
+
prepareSpawn: (pid, cleanupFailed) => {
|
|
929
941
|
checkActive();
|
|
930
|
-
const end = fs.beginTransfer();
|
|
931
942
|
let filesystem;
|
|
932
943
|
let enrolled = false;
|
|
933
944
|
const cancel = () => {
|
|
@@ -949,14 +960,13 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
949
960
|
data: { ...data, filesystem, pipeRegistry: pipes.registry.handle() },
|
|
950
961
|
codec: createRuntimeTerminalCodec({
|
|
951
962
|
fallback: pipes.codec(createFilesystemDescriptorCodec(fs, filesystem))
|
|
952
|
-
})
|
|
953
|
-
release: end
|
|
963
|
+
})
|
|
954
964
|
};
|
|
955
965
|
} catch (error) {
|
|
956
966
|
try {
|
|
957
967
|
cancel();
|
|
958
|
-
}
|
|
959
|
-
|
|
968
|
+
} catch (cleanup) {
|
|
969
|
+
cleanupFailed?.(cleanup);
|
|
960
970
|
}
|
|
961
971
|
throw error;
|
|
962
972
|
}
|
|
@@ -967,7 +977,15 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
967
977
|
if (done)
|
|
968
978
|
return;
|
|
969
979
|
adoptStdio(wasi.descriptors, discardedStdio);
|
|
980
|
+
startStep(memorySampler, "installing-bindings");
|
|
970
981
|
runtimeProcess.installBindings();
|
|
982
|
+
startStep(memorySampler, "bindings-installed");
|
|
983
|
+
const { observedPoll, wait } = processPollWait(recording, {
|
|
984
|
+
entered: () => started,
|
|
985
|
+
memory: () => boundMemory,
|
|
986
|
+
descriptors,
|
|
987
|
+
checkActive
|
|
988
|
+
});
|
|
971
989
|
process = new ProcessABI({
|
|
972
990
|
memory: () => {
|
|
973
991
|
checkActive();
|
|
@@ -984,21 +1002,9 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
984
1002
|
wake: runtimeProcess.wake,
|
|
985
1003
|
futexReady: groupMember.watchReady,
|
|
986
1004
|
createPipe: () => runtimeProcess.createPipe(),
|
|
987
|
-
wait
|
|
988
|
-
checkActive();
|
|
989
|
-
assertTerminalWaitAllowed();
|
|
990
|
-
controlWait(word, 0, expected, ms);
|
|
991
|
-
checkActive();
|
|
992
|
-
}
|
|
1005
|
+
wait
|
|
993
1006
|
});
|
|
994
|
-
|
|
995
|
-
for (let index = 0;index < entries.length; index++) {
|
|
996
|
-
const entry = entries[index], name = entry[0], call = entry[1];
|
|
997
|
-
processImports[name] = (...args) => {
|
|
998
|
-
checkActive();
|
|
999
|
-
return call(...args);
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1007
|
+
installProcessImports(processImports, process.imports, checkActive, observedPoll);
|
|
1002
1008
|
} else {
|
|
1003
1009
|
executor.beforeBindings?.(bindingsContext);
|
|
1004
1010
|
if (done)
|
|
@@ -1034,13 +1040,15 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
1034
1040
|
throw new TypeError("executor imports hook cannot replace imported memory");
|
|
1035
1041
|
}
|
|
1036
1042
|
bindImportedMemory(req.module, linked, bindMemory);
|
|
1043
|
+
const instanceImports = executor.instantiate ? linked : memorySampler?.imports(req.module, linked) ?? linked;
|
|
1044
|
+
startStep(memorySampler, "instantiating");
|
|
1037
1045
|
const instance = executor.instantiate ? await Promise.race([
|
|
1038
|
-
executor.instantiate(
|
|
1046
|
+
executor.instantiate(instanceImports, { ...bindingsContext, bindMemory }),
|
|
1039
1047
|
startupCancelled.then(() => {
|
|
1040
1048
|
checkActive();
|
|
1041
1049
|
throw new Error("startup cancelled");
|
|
1042
1050
|
})
|
|
1043
|
-
]) : new WebAssembly.Instance(req.module,
|
|
1051
|
+
]) : new WebAssembly.Instance(req.module, instanceImports);
|
|
1044
1052
|
if (done)
|
|
1045
1053
|
return;
|
|
1046
1054
|
checkActive();
|
|
@@ -1077,6 +1085,53 @@ async function bootstrapAllocated(req, executor, host, { postToHost, addListener
|
|
|
1077
1085
|
}
|
|
1078
1086
|
}
|
|
1079
1087
|
}
|
|
1088
|
+
async function captureOperations(binding, filesystem, entered) {
|
|
1089
|
+
try {
|
|
1090
|
+
const { createOperationWriters, observePipePoll } = await import("./operations/guest.js");
|
|
1091
|
+
const { pipe, resolve } = createOperationWriters(binding);
|
|
1092
|
+
registerResolveObservation(filesystem, { entered, writer: resolve });
|
|
1093
|
+
return { observePipePoll, writer: pipe };
|
|
1094
|
+
} catch {
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
function processPollWait(recording, options) {
|
|
1099
|
+
const observedPoll = recording && setupPipeObservation(recording, options);
|
|
1100
|
+
const { checkActive } = options;
|
|
1101
|
+
return {
|
|
1102
|
+
observedPoll,
|
|
1103
|
+
wait: observedPoll?.wait ?? ((ms, word, expected) => {
|
|
1104
|
+
checkActive();
|
|
1105
|
+
assertTerminalWaitAllowed();
|
|
1106
|
+
controlWait(word, 0, expected, ms);
|
|
1107
|
+
checkActive();
|
|
1108
|
+
})
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
function setupPipeObservation(recording, options) {
|
|
1112
|
+
const { observePipePoll, writer } = recording;
|
|
1113
|
+
try {
|
|
1114
|
+
return observePipePoll({
|
|
1115
|
+
...options,
|
|
1116
|
+
writer,
|
|
1117
|
+
stopped: (error) => isInstance(error, WasiExit) || isInstance(error, RuntimeWorkerSignal)
|
|
1118
|
+
});
|
|
1119
|
+
} catch {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
function installProcessImports(target, imports, checkActive, observedPoll) {
|
|
1124
|
+
const entries = objectEntries(imports);
|
|
1125
|
+
for (let index = 0;index < entries.length; index++) {
|
|
1126
|
+
const entry = entries[index];
|
|
1127
|
+
const name = entry[0];
|
|
1128
|
+
const call = name === "poll" && observedPoll ? observedPoll.wrap(entry[1]) : entry[1];
|
|
1129
|
+
target[name] = (...args) => {
|
|
1130
|
+
checkActive();
|
|
1131
|
+
return call(...args);
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1080
1135
|
function socketWait(checkActive) {
|
|
1081
1136
|
return (word, expected) => {
|
|
1082
1137
|
checkActive();
|