@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,422 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { BrowserProcessBroker } from "../browser/process-broker.js";
|
|
3
|
+
import { DescriptorTable } from "../core/descriptors.js";
|
|
4
|
+
import { createFilesystemIdentity, type FilesystemIdentity } from "../fs/protocol.js";
|
|
5
|
+
import { commandExecTransfer, commandTransferBytes } from "../proc/command-transfer.js";
|
|
6
|
+
import { controlLoad } from "../proc/control.js";
|
|
7
|
+
import {
|
|
8
|
+
directoryAdmissionVersion,
|
|
9
|
+
findDirectoryJob,
|
|
10
|
+
findDirectoryProcess,
|
|
11
|
+
} from "../proc/directory.js";
|
|
12
|
+
import { ProcessIdentityAllocator } from "../proc/identity.js";
|
|
13
|
+
import { readOutcome } from "../proc/outcome.js";
|
|
14
|
+
import { ExecutableRegistry } from "../proc/registry.js";
|
|
15
|
+
import { ProcessTable } from "../proc/table.js";
|
|
16
|
+
import type { ProcessContext, SpawnRequest } from "../proc/types.js";
|
|
17
|
+
import {
|
|
18
|
+
createWorkerProcessTransport,
|
|
19
|
+
WorkerProcessClient,
|
|
20
|
+
workerProcessState,
|
|
21
|
+
type WorkerDescriptorCodec,
|
|
22
|
+
type WorkerProcessClientOptions,
|
|
23
|
+
type WorkerProcessMessage,
|
|
24
|
+
type WorkerProcessTransport,
|
|
25
|
+
} from "../proc/worker-client.js";
|
|
26
|
+
import { testReservations } from "./fixtures/shared-memory-platform.js";
|
|
27
|
+
|
|
28
|
+
const ready = () => {};
|
|
29
|
+
const executable = new WebAssembly.Module(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]));
|
|
30
|
+
const request: SpawnRequest = {
|
|
31
|
+
executable: "/guest",
|
|
32
|
+
argv: ["guest"],
|
|
33
|
+
cwd: "/",
|
|
34
|
+
env: {},
|
|
35
|
+
job: -1,
|
|
36
|
+
fds: [],
|
|
37
|
+
};
|
|
38
|
+
const noDescriptors: WorkerDescriptorCodec = {
|
|
39
|
+
export() {
|
|
40
|
+
throw Error("unexpected descriptor export");
|
|
41
|
+
},
|
|
42
|
+
import() {
|
|
43
|
+
throw Error("unexpected descriptor import");
|
|
44
|
+
},
|
|
45
|
+
discard() {},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
interface FixtureOptions {
|
|
49
|
+
launch?: (context: ProcessContext) => void | Promise<void>;
|
|
50
|
+
quiesce?: (filesystem: FilesystemIdentity) => void | Promise<void>;
|
|
51
|
+
queued?: (root: WorkerProcessTransport, broker: BrowserProcessBroker) => void;
|
|
52
|
+
capacity?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function fixture(options: FixtureOptions = {}) {
|
|
56
|
+
const calls: number[] = [];
|
|
57
|
+
const diagnostics: unknown[] = [];
|
|
58
|
+
const identities = new ProcessIdentityAllocator();
|
|
59
|
+
const table = new ProcessTable(identities);
|
|
60
|
+
const registry = new ExecutableRegistry();
|
|
61
|
+
const launch = (context: ProcessContext) => {
|
|
62
|
+
calls.push(context.pid);
|
|
63
|
+
const pending = options.launch?.(context);
|
|
64
|
+
const handle = { terminate: ready };
|
|
65
|
+
return pending ? pending.then(() => handle) : handle;
|
|
66
|
+
};
|
|
67
|
+
registry.register("/guest", {
|
|
68
|
+
module: executable,
|
|
69
|
+
adapter: "test",
|
|
70
|
+
interruptible: true,
|
|
71
|
+
execution: { kind: "guest", launch },
|
|
72
|
+
});
|
|
73
|
+
const broker = new BrowserProcessBroker({
|
|
74
|
+
identities,
|
|
75
|
+
table,
|
|
76
|
+
registry,
|
|
77
|
+
launch,
|
|
78
|
+
reservations: testReservations(),
|
|
79
|
+
filesystem: {
|
|
80
|
+
connect() {
|
|
81
|
+
throw Error("this control never starts a Worker");
|
|
82
|
+
},
|
|
83
|
+
revoke: ready,
|
|
84
|
+
reap: ready,
|
|
85
|
+
async quiesce(filesystem) {
|
|
86
|
+
await options.quiesce?.(filesystem);
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
onProcessAdmission(event) {
|
|
90
|
+
if (event.stage === "queued") options.queued?.(root, broker);
|
|
91
|
+
},
|
|
92
|
+
onError(error) {
|
|
93
|
+
diagnostics.push(error);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
const transport = createWorkerProcessTransport({
|
|
97
|
+
identity: identities.buffer,
|
|
98
|
+
capacity: options.capacity ?? 6,
|
|
99
|
+
});
|
|
100
|
+
const root = {
|
|
101
|
+
...transport,
|
|
102
|
+
commandTransfer: new SharedArrayBuffer(commandTransferBytes(transport.directory)),
|
|
103
|
+
};
|
|
104
|
+
const filesystem = createFilesystemIdentity();
|
|
105
|
+
const supervisor = await broker.ready();
|
|
106
|
+
await broker.adoptCommandRoot(root, { memory: [], filesystem, stop: ready });
|
|
107
|
+
const notices: WorkerProcessMessage[] = [];
|
|
108
|
+
const descriptors = new DescriptorTable();
|
|
109
|
+
const client = (
|
|
110
|
+
transport: WorkerProcessTransport = root,
|
|
111
|
+
overrides: Partial<WorkerProcessClientOptions> = {},
|
|
112
|
+
) =>
|
|
113
|
+
new WorkerProcessClient({
|
|
114
|
+
transport,
|
|
115
|
+
descriptors,
|
|
116
|
+
codec: noDescriptors,
|
|
117
|
+
prepareSpawn: () => ({ data: { filesystem: createFilesystemIdentity(filesystem) } }),
|
|
118
|
+
postMessage: (message) => notices.push(message),
|
|
119
|
+
...overrides,
|
|
120
|
+
});
|
|
121
|
+
function spawn(source = client(), input = request) {
|
|
122
|
+
const pid = source.spawn(source.pid, input);
|
|
123
|
+
const message = notices.shift();
|
|
124
|
+
if (message?.operation !== "spawn") throw new Error("No published spawn");
|
|
125
|
+
return { pid, message, transport: message.transport };
|
|
126
|
+
}
|
|
127
|
+
function close() {
|
|
128
|
+
descriptors.dispose();
|
|
129
|
+
return broker.dispose().catch(() => {});
|
|
130
|
+
}
|
|
131
|
+
return { calls, diagnostics, root, broker, supervisor, spawn, close, client, descriptors };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function until(test: () => boolean) {
|
|
135
|
+
const abort = AbortSignal.timeout(5000);
|
|
136
|
+
while (!test()) {
|
|
137
|
+
if (abort.aborted) throw new Error("command scope did not settle");
|
|
138
|
+
await Bun.sleep(0);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const stage of ["before publication", "after publication", "queued admission"] as const) {
|
|
143
|
+
test(`stop while a spawn holds directory admission at ${stage}`, async () => {
|
|
144
|
+
const f = await fixture({
|
|
145
|
+
queued:
|
|
146
|
+
stage === "queued admission" ? (root, broker) => broker.stopCommandRoot(root) : undefined,
|
|
147
|
+
});
|
|
148
|
+
try {
|
|
149
|
+
const events: string[] = [];
|
|
150
|
+
let pending: Promise<void> | undefined;
|
|
151
|
+
let transferred: WorkerProcessTransport | undefined;
|
|
152
|
+
const source = f.client(f.root, {
|
|
153
|
+
beginSpawnTransfer() {
|
|
154
|
+
expect(() => directoryAdmissionVersion(f.root.directory)).not.toThrow();
|
|
155
|
+
events.push("enter");
|
|
156
|
+
return () => {
|
|
157
|
+
expect(() => directoryAdmissionVersion(f.root.directory)).not.toThrow();
|
|
158
|
+
events.push("leave");
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
prepareSpawn() {
|
|
162
|
+
expect(() => directoryAdmissionVersion(f.root.directory)).toThrow("unresolved");
|
|
163
|
+
if (stage === "before publication") f.broker.stopCommandRoot(f.root);
|
|
164
|
+
return { data: { filesystem: createFilesystemIdentity() } };
|
|
165
|
+
},
|
|
166
|
+
postMessage(message) {
|
|
167
|
+
if (message.operation !== "spawn") throw new Error("expected spawn");
|
|
168
|
+
transferred = message.transport;
|
|
169
|
+
expect(() => directoryAdmissionVersion(f.root.directory)).toThrow("unresolved");
|
|
170
|
+
pending = f.broker.runtimeMessage(message, f.root.pid).catch(() => {});
|
|
171
|
+
if (stage === "after publication") f.broker.stopCommandRoot(f.root);
|
|
172
|
+
expect(f.broker.checkCommandCleanup(f.root, false)).toBe(false);
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
source.spawn(source.pid, request);
|
|
176
|
+
if (!transferred) throw new Error("missing transfer");
|
|
177
|
+
const state = workerProcessState(transferred);
|
|
178
|
+
await pending;
|
|
179
|
+
await until(() => controlLoad(state.confirmed) === 1);
|
|
180
|
+
expect(readOutcome(state.signals.outcome)?.kind).toBe("signaled");
|
|
181
|
+
await until(() => f.broker.checkCommandCleanup(f.root, true));
|
|
182
|
+
await f.broker.retireCommandRoot(f.root);
|
|
183
|
+
expect(events).toEqual(["enter", "leave"]);
|
|
184
|
+
expect(f.calls).toEqual([]);
|
|
185
|
+
expect(Array.from(f.supervisor.table.values())).toEqual([]);
|
|
186
|
+
} finally {
|
|
187
|
+
await f.close();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const location of ["direct rejection", "queued rejection"] as const) {
|
|
193
|
+
test(`command cleanup retains the failed transfer from ${location}`, async () => {
|
|
194
|
+
const fail = new Error("unconfirmed rejected transfer");
|
|
195
|
+
let fenceEntered = false;
|
|
196
|
+
const f = await fixture({
|
|
197
|
+
queued:
|
|
198
|
+
location === "queued rejection"
|
|
199
|
+
? (root, broker) => broker.stopCommandRoot(root)
|
|
200
|
+
: undefined,
|
|
201
|
+
quiesce: () => {
|
|
202
|
+
fenceEntered = true;
|
|
203
|
+
throw fail;
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
try {
|
|
207
|
+
const child = f.spawn();
|
|
208
|
+
if (location === "direct rejection") f.broker.stopCommandRoot(f.root);
|
|
209
|
+
await f.broker.runtimeMessage(child.message, f.root.pid).catch(() => {});
|
|
210
|
+
await until(() => f.diagnostics.length > 0);
|
|
211
|
+
expect(fenceEntered).toBe(true);
|
|
212
|
+
expect(controlLoad(workerProcessState(child.transport).confirmed)).toBe(0);
|
|
213
|
+
expect(() => f.broker.checkCommandCleanup(f.root, true)).toThrow("transfer cleanup failed");
|
|
214
|
+
expect(f.calls).toEqual([]);
|
|
215
|
+
} finally {
|
|
216
|
+
await f.close();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
test("failed spawn rollback without a child row is observable before the producer leaves", async () => {
|
|
222
|
+
const f = await fixture({ capacity: 1 });
|
|
223
|
+
try {
|
|
224
|
+
let freed = false;
|
|
225
|
+
const source = f.client(f.root, {
|
|
226
|
+
beginSpawnTransfer: () => () => {
|
|
227
|
+
freed = true;
|
|
228
|
+
},
|
|
229
|
+
prepareSpawn: () => ({
|
|
230
|
+
cancel() {
|
|
231
|
+
throw new Error("cancel left retained data");
|
|
232
|
+
},
|
|
233
|
+
}),
|
|
234
|
+
});
|
|
235
|
+
expect(() => source.spawn(source.pid, request)).toThrow();
|
|
236
|
+
expect(freed).toBe(true);
|
|
237
|
+
expect(() => f.broker.checkCommandCleanup(f.root, true)).toThrow("Spawn transfer to process");
|
|
238
|
+
expect(f.calls).toEqual([]);
|
|
239
|
+
} finally {
|
|
240
|
+
await f.close();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("a collected process still holds its command until its late adapter handle returns", async () => {
|
|
245
|
+
const entered = Promise.withResolvers<ProcessContext>();
|
|
246
|
+
const release = Promise.withResolvers<void>();
|
|
247
|
+
const f = await fixture({
|
|
248
|
+
launch(context) {
|
|
249
|
+
context.started();
|
|
250
|
+
entered.resolve(context);
|
|
251
|
+
return release.promise;
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
try {
|
|
255
|
+
const child = f.spawn();
|
|
256
|
+
const { job } = workerProcessState(child.transport);
|
|
257
|
+
await f.broker.runtimeMessage(child.message, f.root.pid);
|
|
258
|
+
const context = await entered.promise;
|
|
259
|
+
context.exit(0);
|
|
260
|
+
await until(() => controlLoad(workerProcessState(child.transport).confirmed) === 1);
|
|
261
|
+
expect(f.supervisor.waitJob(f.root.pid, job)).toMatchObject({ kind: "exited", code: 0 });
|
|
262
|
+
expect(findDirectoryJob(f.root.directory, job)).toBeUndefined();
|
|
263
|
+
expect(f.broker.checkCommandCleanup(f.root, true)).toBe(false);
|
|
264
|
+
release.resolve();
|
|
265
|
+
await until(() => f.broker.checkCommandCleanup(f.root, true));
|
|
266
|
+
await f.broker.retireCommandRoot(f.root);
|
|
267
|
+
expect(f.supervisor.get(child.pid)).toBeUndefined();
|
|
268
|
+
} finally {
|
|
269
|
+
release.resolve();
|
|
270
|
+
await f.close();
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("descriptor rollback failure keeps the exact unpublished child unconfirmed", async () => {
|
|
275
|
+
const f = await fixture();
|
|
276
|
+
const cause = new Error("discard did not release the transferred description");
|
|
277
|
+
try {
|
|
278
|
+
const stream = () => ({
|
|
279
|
+
kind: "pipe" as const,
|
|
280
|
+
read: () => 0,
|
|
281
|
+
write: () => 0,
|
|
282
|
+
readiness: () => ({ readable: true, writable: true, hangup: false }),
|
|
283
|
+
});
|
|
284
|
+
for (const fd of [0, 1]) f.descriptors.openAt(fd, stream(), 0n, 0n);
|
|
285
|
+
const target = Promise.withResolvers<number>();
|
|
286
|
+
let published = false;
|
|
287
|
+
let exported = false;
|
|
288
|
+
const source = f.client(f.root, {
|
|
289
|
+
postMessage: () => {
|
|
290
|
+
published = true;
|
|
291
|
+
},
|
|
292
|
+
codec: {
|
|
293
|
+
...noDescriptors,
|
|
294
|
+
export(_descriptor, pid) {
|
|
295
|
+
if (exported) throw new Error("second descriptor export failed");
|
|
296
|
+
exported = true;
|
|
297
|
+
target.resolve(pid);
|
|
298
|
+
return { kind: "retained" };
|
|
299
|
+
},
|
|
300
|
+
discard() {
|
|
301
|
+
throw cause;
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
expect(() =>
|
|
306
|
+
source.spawn(source.pid, {
|
|
307
|
+
...request,
|
|
308
|
+
fds: [
|
|
309
|
+
{ parent: 0, child: 0 },
|
|
310
|
+
{ parent: 1, child: 1 },
|
|
311
|
+
],
|
|
312
|
+
}),
|
|
313
|
+
).toThrow("second descriptor export failed");
|
|
314
|
+
expect(published).toBe(false);
|
|
315
|
+
const pid = await target.promise;
|
|
316
|
+
const state = findDirectoryProcess(f.root, pid);
|
|
317
|
+
expect(state).toBeDefined();
|
|
318
|
+
expect(state && controlLoad(state.confirmed)).toBe(0);
|
|
319
|
+
expect(() => f.broker.checkCommandCleanup(f.root, true)).toThrow(
|
|
320
|
+
`Spawn transfer to process ${pid}`,
|
|
321
|
+
);
|
|
322
|
+
} finally {
|
|
323
|
+
await f.close();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("a staged exec rejection holds the source and reports the exact failure", async () => {
|
|
328
|
+
const staged = createFilesystemIdentity();
|
|
329
|
+
const fence = Promise.withResolvers<void>();
|
|
330
|
+
const fenced = Promise.withResolvers<void>();
|
|
331
|
+
const failure = new Error("staged filesystem release failed");
|
|
332
|
+
const f = await fixture({
|
|
333
|
+
launch: (context) => {
|
|
334
|
+
context.started();
|
|
335
|
+
},
|
|
336
|
+
quiesce: (identity) => {
|
|
337
|
+
if (identity !== staged) return;
|
|
338
|
+
fenced.resolve();
|
|
339
|
+
return fence.promise;
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
try {
|
|
343
|
+
const child = f.spawn();
|
|
344
|
+
await f.broker.runtimeMessage(child.message, f.root.pid);
|
|
345
|
+
await f.supervisor.start(child.pid);
|
|
346
|
+
const stagedMessage =
|
|
347
|
+
Promise.withResolvers<Extract<WorkerProcessMessage, { operation: "exec" }>>();
|
|
348
|
+
const source = f.client(child.transport, {
|
|
349
|
+
prepareExec: () => ({ data: { filesystem: staged } }),
|
|
350
|
+
registerRevocation: () => ready,
|
|
351
|
+
postMessage(message) {
|
|
352
|
+
if (message.operation !== "exec") throw new Error("expected exec");
|
|
353
|
+
stagedMessage.resolve(message);
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
source.exec(child.pid, request);
|
|
357
|
+
const message = await stagedMessage.promise;
|
|
358
|
+
const state = workerProcessState(child.transport);
|
|
359
|
+
const handoff = commandExecTransfer(child.transport);
|
|
360
|
+
if (!handoff) throw new Error("missing scoped exec handoff");
|
|
361
|
+
expect(controlLoad(handoff)).toBe(2);
|
|
362
|
+
f.broker.rejectRuntimeMessage(message, child.pid);
|
|
363
|
+
await fenced.promise;
|
|
364
|
+
f.broker.stopCommandRoot(f.root);
|
|
365
|
+
expect(f.broker.checkCommandCleanup(f.root, true)).toBe(false);
|
|
366
|
+
expect(controlLoad(state.confirmed)).toBe(0);
|
|
367
|
+
fence.reject(failure);
|
|
368
|
+
await until(() => f.diagnostics.includes(failure));
|
|
369
|
+
expect(() => f.broker.checkCommandCleanup(f.root, true)).toThrow("transfer cleanup failed");
|
|
370
|
+
expect(controlLoad(handoff)).toBe(2);
|
|
371
|
+
expect(controlLoad(state.confirmed)).toBe(0);
|
|
372
|
+
} finally {
|
|
373
|
+
fence.resolve();
|
|
374
|
+
await f.close();
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test("the leader can exit while an independent child's real retirement fences remain pending", async () => {
|
|
379
|
+
const contexts = new Map<number, ProcessContext>();
|
|
380
|
+
const release = Promise.withResolvers<void>();
|
|
381
|
+
let held: FilesystemIdentity | undefined;
|
|
382
|
+
const f = await fixture({
|
|
383
|
+
launch(context) {
|
|
384
|
+
contexts.set(context.pid, context);
|
|
385
|
+
context.started();
|
|
386
|
+
},
|
|
387
|
+
quiesce(identity) {
|
|
388
|
+
if (identity === held) return release.promise;
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
try {
|
|
392
|
+
const leader = f.spawn();
|
|
393
|
+
await f.broker.runtimeMessage(leader.message, f.root.pid);
|
|
394
|
+
await f.supervisor.start(leader.pid);
|
|
395
|
+
held = createFilesystemIdentity();
|
|
396
|
+
const child = f.spawn(
|
|
397
|
+
f.client(leader.transport, {
|
|
398
|
+
prepareSpawn: () => ({ data: { filesystem: held } }),
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
await f.broker.runtimeMessage(child.message, leader.pid);
|
|
402
|
+
await f.supervisor.start(child.pid);
|
|
403
|
+
const parent = contexts.get(leader.pid);
|
|
404
|
+
const descendant = contexts.get(child.pid);
|
|
405
|
+
if (!parent || !descendant) throw new Error("missing process adapter");
|
|
406
|
+
const outcome = workerProcessState(leader.transport).jobOutcome;
|
|
407
|
+
parent.exit(17);
|
|
408
|
+
await until(() => readOutcome(outcome)?.code === 17);
|
|
409
|
+
expect(f.broker.checkCommandCleanup(f.root, true)).toBe(false);
|
|
410
|
+
f.broker.stopCommandRoot(f.root);
|
|
411
|
+
expect(f.broker.checkCommandCleanup(f.root, true)).toBe(false);
|
|
412
|
+
const childState = workerProcessState(child.transport);
|
|
413
|
+
expect(readOutcome(childState.signals.outcome)).toMatchObject({ kind: "signaled", code: 9 });
|
|
414
|
+
expect(controlLoad(childState.confirmed)).toBe(0);
|
|
415
|
+
release.resolve();
|
|
416
|
+
await until(() => f.broker.checkCommandCleanup(f.root, true));
|
|
417
|
+
await f.broker.retireCommandRoot(f.root);
|
|
418
|
+
} finally {
|
|
419
|
+
release.resolve();
|
|
420
|
+
await f.close();
|
|
421
|
+
}
|
|
422
|
+
});
|
|
@@ -6,6 +6,7 @@ import * as protocol from "../fs/protocol.ts";
|
|
|
6
6
|
import * as native from "../proc/intrinsics.ts";
|
|
7
7
|
import { localPublication } from "../fs/publication-internal.ts";
|
|
8
8
|
import { mutationIntent } from "../fs/publication.ts";
|
|
9
|
+
import { takeResolveObservation } from "../fs/resolve-observation.ts";
|
|
9
10
|
import { poisonProcessIntrinsics, type IntrinsicsReport } from "./fixtures/process-intrinsics.ts";
|
|
10
11
|
|
|
11
12
|
const clientSource = new Bun.Transpiler({ loader: "ts" }).transformSync(
|
|
@@ -84,6 +85,7 @@ function fixture(overrides: Partial<typeof native> = {}) {
|
|
|
84
85
|
allocateSharedControl: (length: number) => new captures.SharedArrayBufferConstructor(length),
|
|
85
86
|
localPublication,
|
|
86
87
|
mutationIntent,
|
|
88
|
+
takeResolveObservation,
|
|
87
89
|
...protocol,
|
|
88
90
|
...requests,
|
|
89
91
|
...captures,
|
|
@@ -7,6 +7,7 @@ import * as shared from "../fs/shared.ts";
|
|
|
7
7
|
import * as native from "../proc/intrinsics.ts";
|
|
8
8
|
import { localPublication } from "../fs/publication-internal.ts";
|
|
9
9
|
import { mutationIntent } from "../fs/publication.ts";
|
|
10
|
+
import { takeResolveObservation } from "../fs/resolve-observation.ts";
|
|
10
11
|
import { CAPABILITY_HEADER, CAPABILITY_WORDS } from "../fs/protocol";
|
|
11
12
|
import { afterEach, expect, test } from "bun:test";
|
|
12
13
|
import {
|
|
@@ -45,6 +46,7 @@ function isolatedClient() {
|
|
|
45
46
|
const bindings = {
|
|
46
47
|
localPublication,
|
|
47
48
|
mutationIntent,
|
|
49
|
+
takeResolveObservation,
|
|
48
50
|
...protocol,
|
|
49
51
|
...shared,
|
|
50
52
|
...native,
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { FilesystemClient } from "../fs/client.js";
|
|
3
|
+
import { MemoryBackend } from "../fs/backend.js";
|
|
4
|
+
import { FilesystemError, createFilesystemConnection, revokeFilesystem } from "../fs/protocol.js";
|
|
5
|
+
import { FilesystemService, installFilesystemWorker } from "../fs/service.js";
|
|
6
|
+
import { registerResolveObservation, takeResolveObservation } from "../fs/resolve-observation.js";
|
|
7
|
+
import { ACKNOWLEDGED, OperationCells, type ResultName } from "../browser/operations/cells.js";
|
|
8
|
+
import { OperationOwner } from "../browser/operations/owner.js";
|
|
9
|
+
|
|
10
|
+
function expectCancellation(error: unknown): void {
|
|
11
|
+
expect(error).toBeInstanceOf(FilesystemError);
|
|
12
|
+
expect(error).toHaveProperty("constructor", FilesystemError);
|
|
13
|
+
expect(error).toHaveProperty("errno", 11);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test("public completion methods throw the exported FilesystemError on revocation", async () => {
|
|
17
|
+
const pair = createFilesystemConnection();
|
|
18
|
+
const client = new FilesystemClient(pair.client);
|
|
19
|
+
try {
|
|
20
|
+
const waiting = client.submit("resolve", [0, "waiting"]);
|
|
21
|
+
const valuing = client.submit("resolve", [0, "valuing"]);
|
|
22
|
+
const releasing = client.submit("resolve", [0, "releasing"]);
|
|
23
|
+
const failures = [
|
|
24
|
+
valuing.value(),
|
|
25
|
+
Promise.resolve().then(() => waiting.wait()),
|
|
26
|
+
Promise.resolve().then(() => releasing.releaseReady()),
|
|
27
|
+
].map((pending) => pending.catch((error: unknown) => error));
|
|
28
|
+
revokeFilesystem(pair.client);
|
|
29
|
+
try {
|
|
30
|
+
for (const failure of await Promise.all(failures)) expectCancellation(failure);
|
|
31
|
+
} finally {
|
|
32
|
+
waiting.release();
|
|
33
|
+
valuing.release();
|
|
34
|
+
}
|
|
35
|
+
} finally {
|
|
36
|
+
client.dispose();
|
|
37
|
+
pair.server.port.close();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function cancelSubmittedResolves(client: FilesystemClient): void {
|
|
42
|
+
const submit = client.submit.bind(client);
|
|
43
|
+
client.submit = (...args) => {
|
|
44
|
+
const completion = submit(...args);
|
|
45
|
+
completion.cancel();
|
|
46
|
+
return completion;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
test("public client construction cannot register a callback for synchronous resolve", () => {
|
|
51
|
+
const pair = createFilesystemConnection();
|
|
52
|
+
let entered = 0;
|
|
53
|
+
const client: FilesystemClient = Reflect.construct(FilesystemClient, [
|
|
54
|
+
pair.client,
|
|
55
|
+
undefined,
|
|
56
|
+
{
|
|
57
|
+
entered() {
|
|
58
|
+
entered++;
|
|
59
|
+
return false;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
]);
|
|
63
|
+
try {
|
|
64
|
+
cancelSubmittedResolves(client);
|
|
65
|
+
expect(client.resolve(0, "cold")).toBe(11);
|
|
66
|
+
expect(entered).toBe(0);
|
|
67
|
+
} finally {
|
|
68
|
+
client.dispose();
|
|
69
|
+
pair.server.port.close();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("private resolve registration is consumed by the exact client connection", () => {
|
|
74
|
+
const pair = createFilesystemConnection();
|
|
75
|
+
const results: ResultName[] = [];
|
|
76
|
+
const began: number[] = [];
|
|
77
|
+
let entered = false;
|
|
78
|
+
registerResolveObservation(pair.client, {
|
|
79
|
+
entered: () => entered,
|
|
80
|
+
writer: {
|
|
81
|
+
begin: (id) => began.push(id),
|
|
82
|
+
settle: (result) => results.push(result),
|
|
83
|
+
submissionFailed: (result) => results.push(result),
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const unrelated = takeResolveObservation({ ...pair.client });
|
|
87
|
+
const client = new FilesystemClient(pair.client);
|
|
88
|
+
try {
|
|
89
|
+
expect(unrelated).toBeUndefined();
|
|
90
|
+
expect(takeResolveObservation(pair.client)).toBeUndefined();
|
|
91
|
+
cancelSubmittedResolves(client);
|
|
92
|
+
expect(client.resolve(0, "pre-entry")).toBe(11);
|
|
93
|
+
expect(began).toEqual([]);
|
|
94
|
+
expect(results).toEqual([]);
|
|
95
|
+
entered = true;
|
|
96
|
+
expect(client.resolve(0, "entered")).toBe(11);
|
|
97
|
+
expect(began).toHaveLength(1);
|
|
98
|
+
expect(results).toEqual(["filesystemCancelled"]);
|
|
99
|
+
} finally {
|
|
100
|
+
client.dispose();
|
|
101
|
+
pair.server.port.close();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("the service Worker records only its bound connection and hides the binding from its factory", async () => {
|
|
106
|
+
const operations = new OperationOwner(
|
|
107
|
+
(size) => new SharedArrayBuffer(size),
|
|
108
|
+
() => true,
|
|
109
|
+
);
|
|
110
|
+
const binding = operations.enroll(1, 1)!;
|
|
111
|
+
const cells = new OperationCells(binding);
|
|
112
|
+
const observed = createFilesystemConnection();
|
|
113
|
+
const ordinary = createFilesystemConnection(observed.client);
|
|
114
|
+
const connected = [Promise.withResolvers<void>(), Promise.withResolvers<void>()];
|
|
115
|
+
const disposed = Promise.withResolvers<void>();
|
|
116
|
+
let factoryOptions: unknown;
|
|
117
|
+
let ready = 0;
|
|
118
|
+
const scope: Parameters<typeof installFilesystemWorker>[0] = {
|
|
119
|
+
onmessage: null,
|
|
120
|
+
postMessage(message) {
|
|
121
|
+
if (typeof message !== "object" || message === null || !("type" in message)) return;
|
|
122
|
+
if (message.type === "filesystem-state") connected[ready++]!.resolve();
|
|
123
|
+
if (message.type === "filesystem-disposed") disposed.resolve();
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
installFilesystemWorker(scope, (options: unknown) => {
|
|
127
|
+
factoryOptions = options;
|
|
128
|
+
return new MemoryBackend();
|
|
129
|
+
});
|
|
130
|
+
const send = (data: unknown) => scope.onmessage!(new MessageEvent("message", { data }));
|
|
131
|
+
const client = new FilesystemClient(observed.client);
|
|
132
|
+
const other = new FilesystemClient(ordinary.client);
|
|
133
|
+
try {
|
|
134
|
+
send({ connect: observed.server, operations: binding });
|
|
135
|
+
await connected[0]!.promise;
|
|
136
|
+
send({ connect: ordinary.server });
|
|
137
|
+
await connected[1]!.promise;
|
|
138
|
+
expect(factoryOptions).toEqual({ connect: observed.server });
|
|
139
|
+
const root = await other.asyncCall("preopen", ["/"]);
|
|
140
|
+
expect(await other.asyncCall("resolve", [root, "missing"])).toMatchObject({
|
|
141
|
+
parent: root,
|
|
142
|
+
name: "missing",
|
|
143
|
+
});
|
|
144
|
+
expect(cells.read(ACKNOWLEDGED)).toBe(0);
|
|
145
|
+
const directory = await client.asyncCall("preopen", ["/"]);
|
|
146
|
+
const request = client.submit("resolve", [directory, "missing"]);
|
|
147
|
+
try {
|
|
148
|
+
expect(await request.value()).toMatchObject({ parent: directory, name: "missing" });
|
|
149
|
+
expect(cells.read(ACKNOWLEDGED)).toBe(request.id);
|
|
150
|
+
} finally {
|
|
151
|
+
request.release();
|
|
152
|
+
}
|
|
153
|
+
} finally {
|
|
154
|
+
client.dispose();
|
|
155
|
+
other.dispose();
|
|
156
|
+
send({ dispose: true });
|
|
157
|
+
try {
|
|
158
|
+
await disposed.promise;
|
|
159
|
+
} finally {
|
|
160
|
+
observed.server.port.close();
|
|
161
|
+
ordinary.server.port.close();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("public service connect cannot register a callback for resolve receipt", async () => {
|
|
167
|
+
const pair = createFilesystemConnection();
|
|
168
|
+
const client = new FilesystemClient(pair.client);
|
|
169
|
+
const owner = new FilesystemService(new MemoryBackend());
|
|
170
|
+
const accepted: number[] = [];
|
|
171
|
+
try {
|
|
172
|
+
Reflect.apply(owner.connect, owner, [pair.server, (id: number) => accepted.push(id)]);
|
|
173
|
+
await client.asyncCall("writeFile", ["/input"], new Uint8Array([65]));
|
|
174
|
+
const root = await client.asyncCall("preopen", ["/"]);
|
|
175
|
+
expect(await client.asyncCall("resolve", [root, "input"])).toMatchObject({
|
|
176
|
+
parent: root,
|
|
177
|
+
node: expect.any(Number),
|
|
178
|
+
});
|
|
179
|
+
expect(accepted).toEqual([]);
|
|
180
|
+
} finally {
|
|
181
|
+
client.dispose();
|
|
182
|
+
await owner.shutdown();
|
|
183
|
+
pair.server.port.close();
|
|
184
|
+
}
|
|
185
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { bytes, declareSharedMemoryPlatform } from "../../memory/reservations.js";
|
|
3
|
+
import { startCommand } from "../../proc/start-command.js";
|
|
4
|
+
import { createRuntime } from "../../runtime.js";
|
|
5
|
+
import { compileCommandGuests } from "./command-guests.js";
|
|
6
|
+
|
|
7
|
+
const runtime = createRuntime({
|
|
8
|
+
filesystem: { mode: "memory", workerURL: new URL("../../browser/fs-worker.ts", import.meta.url) },
|
|
9
|
+
sharedMemory: declareSharedMemoryPlatform({
|
|
10
|
+
kind: "bounded",
|
|
11
|
+
budgetBytes: bytes(1780 * 1024 * 1024),
|
|
12
|
+
counts: { fixedBuffer: true, sharedMemory: true, privateMemory: true },
|
|
13
|
+
}),
|
|
14
|
+
});
|
|
15
|
+
const workerUrl = new URL("../../browser/preview1-worker.ts", import.meta.url).href;
|
|
16
|
+
try {
|
|
17
|
+
const source = (await compileCommandGuests()).get("exit130");
|
|
18
|
+
assert.ok(source, "Missing exit fixture");
|
|
19
|
+
runtime.registerAdapter("generic", (context, service) =>
|
|
20
|
+
service.launchWorker(context, {
|
|
21
|
+
workerUrl,
|
|
22
|
+
request: { module: context.module, args: context.request.argv, env: context.request.env },
|
|
23
|
+
}),
|
|
24
|
+
);
|
|
25
|
+
runtime.registerProgram("/fixture", { adapter: "generic", source });
|
|
26
|
+
await runtime.filesystem.writeFile("/fixture", "");
|
|
27
|
+
const result = await startCommand(runtime, "/fixture", { filesystem: "runtime-root" }).result;
|
|
28
|
+
assert.deepEqual(result.exit, { kind: "exited", code: 130, status: 130 });
|
|
29
|
+
} finally {
|
|
30
|
+
await runtime.dispose();
|
|
31
|
+
}
|