@waffle-os/runtime 0.0.156 → 0.0.157
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/browser/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
package/browser/shared-fs.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { validateMachineId } from "../core/devices.js";
|
|
2
|
+
import { FILESYSTEM_REQUEST_LIMITS, type FilesystemRequestPool } from "../fs/request-memory.js";
|
|
2
3
|
import {
|
|
3
4
|
localMemory,
|
|
4
5
|
localPublication,
|
|
@@ -69,6 +70,8 @@ export type SharedFilesystemOptions = VfsSelection & {
|
|
|
69
70
|
[localMemory]?: MemoryBudget;
|
|
70
71
|
workerURL?: string | URL;
|
|
71
72
|
name?: string;
|
|
73
|
+
/** Optional explicit 32-char lowercase hex machine-id(5) served via virtual `/etc/machine-id`. */
|
|
74
|
+
machineId?: string;
|
|
72
75
|
/** Called when startup enters or leaves the queued ownership state. */
|
|
73
76
|
onStateChange?: SharedFilesystemStateObserver;
|
|
74
77
|
};
|
|
@@ -89,12 +92,32 @@ export interface RemoveCondition {
|
|
|
89
92
|
/** Remove only a regular file whose bytes equal these. */
|
|
90
93
|
readonly ifContent: Uint8Array;
|
|
91
94
|
}
|
|
95
|
+
export interface StatOptions {
|
|
96
|
+
/** Follow a final symbolic link. Defaults to true, matching POSIX `stat`; false matches `lstat`. */
|
|
97
|
+
readonly follow?: boolean;
|
|
98
|
+
}
|
|
99
|
+
/** Metadata for a single path without reading file bytes or enumerating directory children. */
|
|
100
|
+
export interface PathStat {
|
|
101
|
+
readonly kind: "file" | "dir" | "socket" | "symlink";
|
|
102
|
+
readonly size: number;
|
|
103
|
+
readonly ino: number;
|
|
104
|
+
/** Times in whole milliseconds since 1970. */
|
|
105
|
+
readonly mtime: number;
|
|
106
|
+
readonly ctime: number;
|
|
107
|
+
readonly atime: number;
|
|
108
|
+
readonly mode: number;
|
|
109
|
+
readonly nlink: number;
|
|
110
|
+
readonly target?: string;
|
|
111
|
+
}
|
|
92
112
|
export type EntryMetadata = LeafEntry | (EntryStat & { kind: "dir" });
|
|
93
113
|
export type Entry = LeafEntry | (EntryStat & { kind: "dir"; children: Map<string, EntryMetadata> });
|
|
94
114
|
export const WORKSPACE_TAKEN_OVER =
|
|
95
115
|
"This workspace was opened in another tab. Reload to continue here.";
|
|
96
116
|
export const UNSAVED_CHANGES_LOST =
|
|
97
117
|
"Unsaved changes in this tab weren't kept. Copy them before reloading.";
|
|
118
|
+
/** Keep concurrent host requests well below the transport outstanding quota so overflow `take`
|
|
119
|
+
* follow-ups (up to one per active call) and concurrent workspace/watch traffic never exhaust it. */
|
|
120
|
+
const MAX_CONCURRENT_REQUESTS = Math.max(1, Math.floor(FILESYSTEM_REQUEST_LIMITS.outstanding / 4));
|
|
98
121
|
function workerError(data: unknown): Error | undefined {
|
|
99
122
|
if (
|
|
100
123
|
!data ||
|
|
@@ -133,7 +156,9 @@ export class SharedFilesystem {
|
|
|
133
156
|
readonly workspace: WorkspaceVfs | undefined;
|
|
134
157
|
private worker: Worker;
|
|
135
158
|
private writes: Promise<unknown> = Promise.resolve();
|
|
136
|
-
private
|
|
159
|
+
private activeRequests = 0;
|
|
160
|
+
private requestWaiters: (((acquired: boolean) => void) | undefined)[] = [];
|
|
161
|
+
private requestWaiterHead = 0;
|
|
137
162
|
private socketAttachments = new Set<(error: Error) => void>();
|
|
138
163
|
private fatal: Error | undefined;
|
|
139
164
|
private _state: SharedFilesystemState = Object.freeze({ status: "opening" });
|
|
@@ -153,6 +178,8 @@ export class SharedFilesystem {
|
|
|
153
178
|
onStartupStage?: RuntimeStartupObserver,
|
|
154
179
|
reservations?: RuntimeReservations,
|
|
155
180
|
) {
|
|
181
|
+
const explicitMachineId =
|
|
182
|
+
options.machineId !== undefined ? validateMachineId(options.machineId) : undefined;
|
|
156
183
|
const projectionBudget = options[localMemory];
|
|
157
184
|
if (projectionBudget !== undefined && !(projectionBudget instanceof MemoryBudget))
|
|
158
185
|
throw new TypeError("localMemory requires a same-realm MemoryBudget");
|
|
@@ -218,6 +245,7 @@ export class SharedFilesystem {
|
|
|
218
245
|
this.client = client = new FilesystemClient(connection.client, {
|
|
219
246
|
connect: (identity) => this.connect(identity),
|
|
220
247
|
});
|
|
248
|
+
if (explicitMachineId !== undefined) client.shared.setMachineId(explicitMachineId);
|
|
221
249
|
this.workspace = options.workspace ? createWorkspaceVfs(client, projectionBudget) : undefined;
|
|
222
250
|
worker.addEventListener("message", (event) => this.receiveMessage(event, onStartupStage));
|
|
223
251
|
worker.addEventListener("error", (event) => {
|
|
@@ -234,6 +262,7 @@ export class SharedFilesystem {
|
|
|
234
262
|
const message: {
|
|
235
263
|
connect: FilesystemConnection;
|
|
236
264
|
name?: string;
|
|
265
|
+
machineId?: string;
|
|
237
266
|
mode: VfsDescriptor["mode"];
|
|
238
267
|
implementation: VfsDescriptor["implementation"];
|
|
239
268
|
whenBusy?: "takeover" | "wait" | "attach";
|
|
@@ -241,6 +270,7 @@ export class SharedFilesystem {
|
|
|
241
270
|
} = {
|
|
242
271
|
connect: connection.server,
|
|
243
272
|
name: options.name,
|
|
273
|
+
machineId: explicitMachineId,
|
|
244
274
|
mode: this.vfs.mode,
|
|
245
275
|
implementation: this.vfs.implementation,
|
|
246
276
|
whenBusy: selection.whenBusy,
|
|
@@ -302,6 +332,10 @@ export class SharedFilesystem {
|
|
|
302
332
|
get state(): SharedFilesystemState {
|
|
303
333
|
return this._state;
|
|
304
334
|
}
|
|
335
|
+
/** Stable 32-char lowercase hex machine-id(5) served via virtual `/etc/machine-id`. */
|
|
336
|
+
get machineId(): string | undefined {
|
|
337
|
+
return this.client.machineId;
|
|
338
|
+
}
|
|
305
339
|
/** True when the filesystem worker/storage ownership could not be confirmed released. */
|
|
306
340
|
ownershipRetained(): boolean {
|
|
307
341
|
return this._ownershipRetained;
|
|
@@ -328,6 +362,10 @@ export class SharedFilesystem {
|
|
|
328
362
|
}
|
|
329
363
|
if (this.stopped || (this.disposal && !this.joinedReady())) return;
|
|
330
364
|
if (event.data?.type === "filesystem-ownership-lost") {
|
|
365
|
+
if (this.vfs.multiTab === "shared-host") {
|
|
366
|
+
this.loseOwnership();
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
331
369
|
this.ownershipLost = true;
|
|
332
370
|
this.fail(new Error(`${WORKSPACE_TAKEN_OVER} ${UNSAVED_CHANGES_LOST}`));
|
|
333
371
|
this.onOwnershipLost?.();
|
|
@@ -363,6 +401,22 @@ export class SharedFilesystem {
|
|
|
363
401
|
/* State observers cannot own filesystem startup or teardown. */
|
|
364
402
|
}
|
|
365
403
|
}
|
|
404
|
+
private canCleanDisposeAfterOwnershipLoss(): boolean {
|
|
405
|
+
return this.ownershipLost && this.vfs.multiTab === "shared-host" && !this._ownershipRetained;
|
|
406
|
+
}
|
|
407
|
+
private loseOwnership(): void {
|
|
408
|
+
this.ownershipLost = true;
|
|
409
|
+
const error = new Error(`${WORKSPACE_TAKEN_OVER} ${UNSAVED_CHANGES_LOST}`);
|
|
410
|
+
for (const cancel of this.socketAttachments) cancel(error);
|
|
411
|
+
this.fatal ??= error;
|
|
412
|
+
this.abortQueuedRequests();
|
|
413
|
+
if (!this.stopped) {
|
|
414
|
+
revokeFilesystemService(this.client.connection);
|
|
415
|
+
if (!this.joinedReady()) this.stop();
|
|
416
|
+
}
|
|
417
|
+
if (!this.disposal) this.setState("failed", this.fatal);
|
|
418
|
+
this.onOwnershipLost?.();
|
|
419
|
+
}
|
|
366
420
|
private fail(error: Error, released?: boolean): void {
|
|
367
421
|
for (const cancel of this.socketAttachments) cancel(error);
|
|
368
422
|
if (this.stopped) return;
|
|
@@ -408,6 +462,7 @@ export class SharedFilesystem {
|
|
|
408
462
|
private stopClient(): void {
|
|
409
463
|
if (this.clientStopped) return;
|
|
410
464
|
this.clientStopped = true;
|
|
465
|
+
this.abortQueuedRequests();
|
|
411
466
|
void this.workspace?.close().catch(() => {});
|
|
412
467
|
this.client.dispose();
|
|
413
468
|
}
|
|
@@ -593,6 +648,74 @@ export class SharedFilesystem {
|
|
|
593
648
|
await waitForFilesystemQuiescence(connection);
|
|
594
649
|
this.reap(connection);
|
|
595
650
|
}
|
|
651
|
+
private requestAbortError(): Error | undefined {
|
|
652
|
+
if (this.fatal) return this.fatal;
|
|
653
|
+
if (this.disposal || this.stopped || this.clientStopped) return new FilesystemError(11);
|
|
654
|
+
const transport = this.client.transportError();
|
|
655
|
+
if (transport) return transport;
|
|
656
|
+
if (isRevoked(this.client.connection)) return new FilesystemError(11);
|
|
657
|
+
return undefined;
|
|
658
|
+
}
|
|
659
|
+
private dequeueRequestWaiter(): ((acquired: boolean) => void) | undefined {
|
|
660
|
+
if (!this.requestWaiters || this.requestWaiterHead >= this.requestWaiters.length)
|
|
661
|
+
return undefined;
|
|
662
|
+
const waiter = this.requestWaiters[this.requestWaiterHead];
|
|
663
|
+
this.requestWaiters[this.requestWaiterHead] = undefined;
|
|
664
|
+
this.requestWaiterHead++;
|
|
665
|
+
if (this.requestWaiterHead >= this.requestWaiters.length) {
|
|
666
|
+
this.requestWaiters.length = 0;
|
|
667
|
+
this.requestWaiterHead = 0;
|
|
668
|
+
} else if (
|
|
669
|
+
this.requestWaiterHead >= 64 &&
|
|
670
|
+
this.requestWaiterHead * 2 >= this.requestWaiters.length
|
|
671
|
+
) {
|
|
672
|
+
this.requestWaiters = this.requestWaiters.slice(this.requestWaiterHead);
|
|
673
|
+
this.requestWaiterHead = 0;
|
|
674
|
+
}
|
|
675
|
+
return waiter;
|
|
676
|
+
}
|
|
677
|
+
private abortQueuedRequests(): void {
|
|
678
|
+
if (!this.requestWaiters || this.requestWaiterHead >= this.requestWaiters.length) return;
|
|
679
|
+
const waiters = this.requestWaiters;
|
|
680
|
+
const start = this.requestWaiterHead;
|
|
681
|
+
this.requestWaiters = [];
|
|
682
|
+
this.requestWaiterHead = 0;
|
|
683
|
+
for (let index = start; index < waiters.length; index++) {
|
|
684
|
+
waiters[index]?.(false);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
private acquireRequestSlot(): Promise<void> | undefined {
|
|
688
|
+
const aborted = this.requestAbortError();
|
|
689
|
+
if (aborted) throw aborted;
|
|
690
|
+
if (this.activeRequests < MAX_CONCURRENT_REQUESTS) {
|
|
691
|
+
this.activeRequests++;
|
|
692
|
+
return undefined;
|
|
693
|
+
}
|
|
694
|
+
return this.waitForRequestSlot();
|
|
695
|
+
}
|
|
696
|
+
private async waitForRequestSlot(): Promise<void> {
|
|
697
|
+
const acquired = await new Promise<boolean>((resolve) => {
|
|
698
|
+
this.requestWaiters.push(resolve);
|
|
699
|
+
});
|
|
700
|
+
if (!acquired) throw this.requestAbortError() ?? new FilesystemError(11);
|
|
701
|
+
const aborted = this.requestAbortError();
|
|
702
|
+
if (!aborted) return;
|
|
703
|
+
this.releaseRequestSlot();
|
|
704
|
+
throw aborted;
|
|
705
|
+
}
|
|
706
|
+
private releaseRequestSlot(): void {
|
|
707
|
+
if (this.requestAbortError()) {
|
|
708
|
+
this.activeRequests--;
|
|
709
|
+
this.abortQueuedRequests();
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
const next = this.dequeueRequestWaiter();
|
|
713
|
+
if (next) {
|
|
714
|
+
next(true);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
this.activeRequests--;
|
|
718
|
+
}
|
|
596
719
|
private async request(
|
|
597
720
|
operation: string,
|
|
598
721
|
args: unknown[] = [],
|
|
@@ -600,6 +723,8 @@ export class SharedFilesystem {
|
|
|
600
723
|
publication?: PublicationAdmission,
|
|
601
724
|
): Promise<any> {
|
|
602
725
|
this.assertLive();
|
|
726
|
+
const waiting = this.acquireRequestSlot();
|
|
727
|
+
if (waiting) await waiting;
|
|
603
728
|
try {
|
|
604
729
|
return await this.client.asyncCall(operation, args, bytes, publication);
|
|
605
730
|
} catch (error) {
|
|
@@ -607,6 +732,8 @@ export class SharedFilesystem {
|
|
|
607
732
|
// actual failure, such as unavailable or corrupt browser storage.
|
|
608
733
|
this.assertLive();
|
|
609
734
|
throw error;
|
|
735
|
+
} finally {
|
|
736
|
+
this.releaseRequestSlot();
|
|
610
737
|
}
|
|
611
738
|
}
|
|
612
739
|
private mutate(operation: string, args: unknown[], bytes?: Uint8Array): Promise<any> {
|
|
@@ -624,21 +751,14 @@ export class SharedFilesystem {
|
|
|
624
751
|
.finally(() => {
|
|
625
752
|
if (publication) this.client.cancelPublication(publication);
|
|
626
753
|
});
|
|
627
|
-
// Keep ordering after an error
|
|
628
|
-
this.writes = result.catch((
|
|
629
|
-
this.failure ??= error;
|
|
630
|
-
});
|
|
754
|
+
// Keep ordering after an error; the returned promise delivers the failure to its caller.
|
|
755
|
+
this.writes = result.catch(() => {});
|
|
631
756
|
return result;
|
|
632
757
|
}
|
|
633
758
|
/** Wait for this page's queued mutations, without waiting for background persistence. */
|
|
634
759
|
async drain(): Promise<void> {
|
|
635
760
|
await this.writes;
|
|
636
761
|
this.assertLive();
|
|
637
|
-
if (this.failure !== undefined) {
|
|
638
|
-
const error = this.failure;
|
|
639
|
-
this.failure = undefined;
|
|
640
|
-
throw error;
|
|
641
|
-
}
|
|
642
762
|
}
|
|
643
763
|
/** A save is durable only once both data and metadata have reached storage. */
|
|
644
764
|
async flush(path?: string): Promise<void> {
|
|
@@ -647,10 +767,14 @@ export class SharedFilesystem {
|
|
|
647
767
|
}
|
|
648
768
|
/** Walk the actual tree before canonicalizing; metadata only, without reading file contents. */
|
|
649
769
|
async resolvePath(path: string): Promise<ResolvedEntry | undefined> {
|
|
650
|
-
|
|
651
|
-
await this.writes;
|
|
770
|
+
await this.drain();
|
|
652
771
|
return this.request("resolvePath", [path]);
|
|
653
772
|
}
|
|
773
|
+
/** Read metadata for a single path without reading file bytes or listing directory children. */
|
|
774
|
+
async stat(path: string, options?: StatOptions): Promise<PathStat | undefined> {
|
|
775
|
+
await this.drain();
|
|
776
|
+
return this.request("statPath", [path, options?.follow !== false]);
|
|
777
|
+
}
|
|
654
778
|
async lookup(path: string): Promise<Entry | undefined> {
|
|
655
779
|
await this.drain();
|
|
656
780
|
return this.request("lookup", [path]);
|
|
@@ -717,6 +841,7 @@ export class SharedFilesystem {
|
|
|
717
841
|
// A worker failure can own this rejection before the caller asks for disposal.
|
|
718
842
|
void this.disposal.catch(() => {});
|
|
719
843
|
revokeFilesystemService(this.client.connection);
|
|
844
|
+
this.abortQueuedRequests();
|
|
720
845
|
const quiescence = waitForFilesystemQuiescence(this.client.connection);
|
|
721
846
|
void quiescence.catch((reason) => {
|
|
722
847
|
this._ownershipRetained = true;
|
|
@@ -778,7 +903,8 @@ export class SharedFilesystem {
|
|
|
778
903
|
void this.disposal.catch(() => {});
|
|
779
904
|
return this.disposal;
|
|
780
905
|
}
|
|
781
|
-
if (this.joinedReady() && !this.stopped)
|
|
906
|
+
if (this.joinedReady() && !this.stopped)
|
|
907
|
+
return this.disposeReady(this.canCleanDisposeAfterOwnershipLoss() ? undefined : this.fatal);
|
|
782
908
|
let finish!: (error?: Error, released?: boolean) => void;
|
|
783
909
|
this.disposal = new Promise<void>((resolve, reject) => {
|
|
784
910
|
finish = (error, released) => {
|
|
@@ -806,6 +932,7 @@ export class SharedFilesystem {
|
|
|
806
932
|
this.finishDisposal = finish;
|
|
807
933
|
this.setState("disposed");
|
|
808
934
|
revokeFilesystemService(this.client.connection);
|
|
935
|
+
this.abortQueuedRequests();
|
|
809
936
|
if (this.stopped || this.startupComplete || !this.cancellableStartup) finish();
|
|
810
937
|
else {
|
|
811
938
|
// Wake pending operations now; the worker is retained only to abort/close startup.
|
package/browser/worker.ts
CHANGED
|
@@ -239,6 +239,12 @@ export type RuntimeWorkerState = ReturnType<typeof workerProcessState>;
|
|
|
239
239
|
function logicalProcessIdentity(state: RuntimeWorkerState | null) {
|
|
240
240
|
return state?.processIdentity ?? null;
|
|
241
241
|
}
|
|
242
|
+
function wasiProcessPid(
|
|
243
|
+
state: RuntimeWorkerState | null,
|
|
244
|
+
identity: RuntimeProcessIdentity | null,
|
|
245
|
+
): number | undefined {
|
|
246
|
+
return logicalProcessIdentity(state)?.pid ?? identity?.pid;
|
|
247
|
+
}
|
|
242
248
|
|
|
243
249
|
export interface WorkerSetupContext<R extends RuntimeWorkerRequest = RuntimeWorkerRequest> {
|
|
244
250
|
request: R;
|
|
@@ -527,10 +533,26 @@ function closeDiscardedStdio(discarded: readonly Stream[], tableOwned: ReadonlyS
|
|
|
527
533
|
if (errors.length) throw new AggregateError(errors, "stdio release failed");
|
|
528
534
|
}
|
|
529
535
|
|
|
530
|
-
|
|
536
|
+
type PosixTeardownMode =
|
|
537
|
+
| { readonly kind: "process-exit" }
|
|
538
|
+
| { readonly kind: "thread-member" }
|
|
539
|
+
| { readonly kind: "exec"; readonly keptFds: ReadonlySet<number> };
|
|
540
|
+
|
|
541
|
+
function disposeProcessDescriptors(
|
|
542
|
+
wasi: Wasi,
|
|
543
|
+
discarded: readonly Stream[],
|
|
544
|
+
posixMode: PosixTeardownMode = { kind: "process-exit" },
|
|
545
|
+
): void {
|
|
531
546
|
const tableOwned = tableOwnedStdio(wasi.descriptors, discarded);
|
|
532
547
|
const errors: unknown[] = [];
|
|
533
548
|
wasi.dropWatches();
|
|
549
|
+
try {
|
|
550
|
+
if (posixMode.kind === "exec") wasi.releaseUninheritedPosixLocks(posixMode.keptFds);
|
|
551
|
+
else if (posixMode.kind === "thread-member") wasi.suppressPosixClose();
|
|
552
|
+
else wasi.releasePosixLocks();
|
|
553
|
+
} catch (error) {
|
|
554
|
+
errors.push(error);
|
|
555
|
+
}
|
|
534
556
|
try {
|
|
535
557
|
wasi.descriptors.dispose();
|
|
536
558
|
} catch (error) {
|
|
@@ -647,6 +669,11 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
647
669
|
fs,
|
|
648
670
|
);
|
|
649
671
|
let runtimeProcess: WorkerProcessClient | null = null;
|
|
672
|
+
let isThreadMember = false;
|
|
673
|
+
let hasSpawnedThread = false;
|
|
674
|
+
let execHandoff:
|
|
675
|
+
| { readonly successorOwner: number; readonly keptFds: ReadonlySet<number> }
|
|
676
|
+
| undefined;
|
|
650
677
|
let process: ProcessABI | undefined;
|
|
651
678
|
let processSignalExit: typeof import("../proc/abi.js").ProcessSignalExit | undefined;
|
|
652
679
|
let discardedStdio: Stream[] = [];
|
|
@@ -876,7 +903,19 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
876
903
|
}
|
|
877
904
|
const descriptorsReleased = attempt(() => {
|
|
878
905
|
if (wasi) {
|
|
879
|
-
|
|
906
|
+
if (replacing && execHandoff) {
|
|
907
|
+
if (fs.locks.hasActivePosixLocks?.()) {
|
|
908
|
+
fs.locks.handoffPosix?.(execHandoff.successorOwner);
|
|
909
|
+
}
|
|
910
|
+
disposeProcessDescriptors(wasi, discardedStdio, {
|
|
911
|
+
kind: "exec",
|
|
912
|
+
keptFds: execHandoff.keptFds,
|
|
913
|
+
});
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
const posixMode: PosixTeardownMode =
|
|
917
|
+
isThreadMember || hasSpawnedThread ? { kind: "thread-member" } : { kind: "process-exit" };
|
|
918
|
+
disposeProcessDescriptors(wasi, discardedStdio, posixMode);
|
|
880
919
|
return;
|
|
881
920
|
}
|
|
882
921
|
const streams = ownedStreams(ownedStdio);
|
|
@@ -1040,6 +1079,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1040
1079
|
runtimeState?.signals.interrupt ??
|
|
1041
1080
|
(executor.interrupt ? new Int32ArrayConstructor(executor.interrupt) : undefined);
|
|
1042
1081
|
if (runtimeState) {
|
|
1082
|
+
isThreadMember = runtimeState.processKind === "thread";
|
|
1043
1083
|
outcome = runtimeState.signals.outcome;
|
|
1044
1084
|
requestedSignal = runtimeState.signals.requestedSignal;
|
|
1045
1085
|
}
|
|
@@ -1190,6 +1230,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1190
1230
|
const hostCallFd = executor.hostCallFd;
|
|
1191
1231
|
const reservedFds = bindingTargets(req.runtimeProcess, req.network, hostCallFd);
|
|
1192
1232
|
wasi = new Wasi({
|
|
1233
|
+
pid: wasiProcessPid(runtimeState, identity),
|
|
1193
1234
|
hostCallFd,
|
|
1194
1235
|
args: req.args,
|
|
1195
1236
|
env,
|
|
@@ -1220,6 +1261,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1220
1261
|
}
|
|
1221
1262
|
checkActive();
|
|
1222
1263
|
},
|
|
1264
|
+
checkActive,
|
|
1223
1265
|
interrupt,
|
|
1224
1266
|
interruptPending: runtimeState
|
|
1225
1267
|
? () => signalPending(runtimeState.signals)
|
|
@@ -1287,12 +1329,16 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1287
1329
|
checkActive();
|
|
1288
1330
|
return pipes.createPipe();
|
|
1289
1331
|
},
|
|
1332
|
+
createPty: (cols, rows) => {
|
|
1333
|
+
checkActive();
|
|
1334
|
+
return pipes.createPty(cols, rows);
|
|
1335
|
+
},
|
|
1290
1336
|
revokeProcess: (pid) => pipes.revokeProcess(pid),
|
|
1291
1337
|
registerRevocation: (hook) => {
|
|
1292
1338
|
listenAbort(cancellation.signal, hook);
|
|
1293
1339
|
return () => unlistenAbort(cancellation.signal, hook);
|
|
1294
1340
|
},
|
|
1295
|
-
prepareExec: (_pid, cleanupFailed) => {
|
|
1341
|
+
prepareExec: (_pid, cleanupFailed, keptFds) => {
|
|
1296
1342
|
checkActive();
|
|
1297
1343
|
const end = fs.beginTransfer();
|
|
1298
1344
|
let filesystem: ReturnType<typeof createProcessFilesystemIdentity> | undefined;
|
|
@@ -1304,9 +1350,18 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1304
1350
|
try {
|
|
1305
1351
|
const data = executor.spawnData?.();
|
|
1306
1352
|
checkActive();
|
|
1307
|
-
|
|
1353
|
+
const posixOwner = fs.connection.posixOwner ?? fs.connection.owner;
|
|
1354
|
+
filesystem = createProcessFilesystemIdentity(fs.connection, posixOwner);
|
|
1355
|
+
const keptSet = new SetConstructor<number>();
|
|
1356
|
+
if (keptFds) {
|
|
1357
|
+
for (let i = 0; i < keptFds.length; i++) setAdd(keptSet, keptFds[i]!);
|
|
1358
|
+
}
|
|
1359
|
+
const successorOwner = filesystem.owner;
|
|
1308
1360
|
return {
|
|
1309
1361
|
cancel,
|
|
1362
|
+
commit: () => {
|
|
1363
|
+
execHandoff = { successorOwner, keptFds: keptSet };
|
|
1364
|
+
},
|
|
1310
1365
|
release: end,
|
|
1311
1366
|
data: { ...data, filesystem, pipeRegistry: pipes.registry.handle() },
|
|
1312
1367
|
codec: createRuntimeTerminalCodec({
|
|
@@ -1325,7 +1380,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1325
1380
|
}
|
|
1326
1381
|
},
|
|
1327
1382
|
beginSpawnTransfer: () => fs.beginTransfer(),
|
|
1328
|
-
prepareSpawn: (pid, cleanupFailed) => {
|
|
1383
|
+
prepareSpawn: (pid, cleanupFailed, processKind) => {
|
|
1329
1384
|
checkActive();
|
|
1330
1385
|
let filesystem: ReturnType<typeof createProcessFilesystemIdentity> | undefined;
|
|
1331
1386
|
let enrolled = false;
|
|
@@ -1341,9 +1396,20 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1341
1396
|
checkActive();
|
|
1342
1397
|
pipes.registry.registerProcess(pid);
|
|
1343
1398
|
enrolled = true;
|
|
1344
|
-
|
|
1399
|
+
const threadSpawn = processKind === "thread";
|
|
1400
|
+
const posixOwner = threadSpawn
|
|
1401
|
+
? (fs.connection.posixOwner ?? fs.connection.owner)
|
|
1402
|
+
: undefined;
|
|
1403
|
+
filesystem = createProcessFilesystemIdentity(fs.connection, posixOwner);
|
|
1404
|
+
const childOwner = filesystem.owner;
|
|
1345
1405
|
return {
|
|
1346
1406
|
cancel,
|
|
1407
|
+
commit: threadSpawn
|
|
1408
|
+
? () => {
|
|
1409
|
+
hasSpawnedThread = true;
|
|
1410
|
+
if (fs.locks.hasActivePosixLocks?.()) fs.locks.handoffPosix?.(childOwner);
|
|
1411
|
+
}
|
|
1412
|
+
: undefined,
|
|
1347
1413
|
data: { ...data, filesystem, pipeRegistry: pipes.registry.handle() },
|
|
1348
1414
|
codec: createRuntimeTerminalCodec({
|
|
1349
1415
|
fallback: pipes.codec(createFilesystemDescriptorCodec(fs, filesystem)),
|
|
@@ -1388,6 +1454,7 @@ async function bootstrapAllocated<R extends RuntimeWorkerRequest>(
|
|
|
1388
1454
|
wake: runtimeProcess.wake,
|
|
1389
1455
|
futexReady: groupMember.watchReady,
|
|
1390
1456
|
createPipe: () => runtimeProcess!.createPipe(),
|
|
1457
|
+
createPty: (cols, rows) => runtimeProcess!.createPty(cols, rows),
|
|
1391
1458
|
wait,
|
|
1392
1459
|
});
|
|
1393
1460
|
installProcessImports(processImports, process.imports, checkActive, observedPoll);
|
package/core/descriptors.ts
CHANGED
|
@@ -17,12 +17,20 @@ export interface Readiness {
|
|
|
17
17
|
nbytes?: number;
|
|
18
18
|
error?: number;
|
|
19
19
|
}
|
|
20
|
+
export interface VirtualFileState {
|
|
21
|
+
readonly ino: number;
|
|
22
|
+
readonly machineId: string;
|
|
23
|
+
readonly data: Uint8Array;
|
|
24
|
+
offset: number;
|
|
25
|
+
}
|
|
20
26
|
/** Pipe and terminal services supply I/O and level-triggered readiness, never guest pointers.
|
|
21
27
|
* I/O returns a byte count or negative WASI errno. Preview1 adapters may block inside read/write.
|
|
22
28
|
*/
|
|
23
29
|
export interface Stream {
|
|
24
30
|
kind: "pipe" | "terminal" | "device" | "socket";
|
|
25
|
-
device?: "null";
|
|
31
|
+
device?: "null" | "machine-id";
|
|
32
|
+
/** Present on read-only virtual regular files backed by a Stream (such as `/etc/machine-id`). */
|
|
33
|
+
readonly virtualFile?: VirtualFileState;
|
|
26
34
|
/** Supported fdflags share open-description state across duplication and endpoint transfer. */
|
|
27
35
|
fdflags?: number;
|
|
28
36
|
read(chunks: readonly Uint8Array[]): number;
|
|
@@ -94,6 +102,11 @@ export class DescriptorTable {
|
|
|
94
102
|
private entries = new MapConstructor<number, Descriptor>();
|
|
95
103
|
private next = 0;
|
|
96
104
|
private cleanupFailures: unknown[] = [];
|
|
105
|
+
#onClose?: (entry: Descriptor) => void;
|
|
106
|
+
/** Host-internal hook invoked before dropping a closed descriptor's open description. */
|
|
107
|
+
setOnClose(callback: ((entry: Descriptor) => void) | undefined): void {
|
|
108
|
+
this.#onClose = callback;
|
|
109
|
+
}
|
|
97
110
|
/** Deliver a failed transferred-resource release to this table's teardown owner once. */
|
|
98
111
|
recordCleanupFailure(error: unknown): void {
|
|
99
112
|
arrayAppend(this.cleanupFailures, error);
|
|
@@ -101,6 +114,9 @@ export class DescriptorTable {
|
|
|
101
114
|
get(fd: number): Descriptor | undefined {
|
|
102
115
|
return mapGet(this.entries, fd);
|
|
103
116
|
}
|
|
117
|
+
forEach(callback: (entry: Descriptor, fd: number) => void): void {
|
|
118
|
+
mapForEach(this.entries, callback);
|
|
119
|
+
}
|
|
104
120
|
hasResource(resource: Resource): boolean {
|
|
105
121
|
let found = false;
|
|
106
122
|
mapForEach(this.entries, (entry) => {
|
|
@@ -136,7 +152,7 @@ export class DescriptorTable {
|
|
|
136
152
|
case "terminal":
|
|
137
153
|
return 2;
|
|
138
154
|
case "device":
|
|
139
|
-
return 0;
|
|
155
|
+
return entry.resource.virtualFile ? 3 : 0;
|
|
140
156
|
case "file":
|
|
141
157
|
return 3;
|
|
142
158
|
case "directory":
|
|
@@ -242,6 +258,7 @@ export class DescriptorTable {
|
|
|
242
258
|
if (!entry) return false;
|
|
243
259
|
mapDelete(this.entries, fd);
|
|
244
260
|
this.next = Math.min(this.next, fd);
|
|
261
|
+
this.#onClose?.(entry);
|
|
245
262
|
entry.description.drop();
|
|
246
263
|
return true;
|
|
247
264
|
}
|