@waffle-os/runtime 0.0.148 → 0.0.150
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/diagnostic.ts +234 -2
- package/browser/process-broker.ts +6 -3
- package/browser/run-command.ts +18 -7
- package/browser/worker.ts +34 -5
- package/core/open-file.ts +126 -52
- package/dist/browser/diagnostic.js +150 -1
- package/dist/browser/process-broker.js +3 -2
- package/dist/browser/run-command.js +8 -2
- package/dist/browser/worker.js +24 -6
- package/dist/core/open-file.js +112 -50
- package/dist/fs/client.js +290 -46
- package/dist/fs/protocol.js +7 -4
- package/dist/fs/service.js +77 -16
- package/dist/fs/shared.js +6 -6
- package/dist/proc/abi.js +132 -10
- package/dist/proc/command-stdio.js +81 -33
- package/dist/proc/registry.js +8 -8
- package/dist/proc/start-command.js +10 -0
- package/dist/proc/supervisor.js +4 -4
- package/dist/thread/service.js +85 -10
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +298 -44
- package/fs/protocol.ts +9 -4
- package/fs/service.ts +99 -19
- package/fs/shared.ts +7 -7
- package/package.json +1 -1
- package/proc/abi.ts +160 -13
- package/proc/command-stdio.ts +98 -41
- package/proc/registry.ts +13 -19
- package/proc/start-command.ts +29 -0
- package/proc/supervisor.ts +8 -6
- package/thread/service.ts +125 -7
- package/tooling/program/program-transformer.json +1 -1
package/proc/command-stdio.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { atomicsWaitAsync } from "./intrinsics.js";
|
|
2
2
|
import type { DescriptorTable, Resource } from "../core/descriptors.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
claimFileOffset,
|
|
5
|
+
withFileOffset,
|
|
6
|
+
shareFileResource,
|
|
7
|
+
type AsyncFileOffsetLease,
|
|
8
|
+
} from "../core/open-file.js";
|
|
9
|
+
import { FilesystemClient, type FilesystemCompletion } from "../fs/client.js";
|
|
5
10
|
import { errno, type FilesystemConnection } from "../fs/protocol.js";
|
|
6
11
|
import { CommandStdioError, type CommandInput, type CommandOutput } from "./command.js";
|
|
7
12
|
|
|
@@ -116,42 +121,101 @@ export class CommandStdio {
|
|
|
116
121
|
}
|
|
117
122
|
}
|
|
118
123
|
|
|
119
|
-
private
|
|
124
|
+
private commitAsyncWrite(
|
|
125
|
+
lease: AsyncFileOffsetLease,
|
|
126
|
+
result: { written: number; offset: number } | number,
|
|
127
|
+
): number {
|
|
128
|
+
if (lease.append) {
|
|
129
|
+
const appended = result as { written: number; offset: number };
|
|
130
|
+
lease.commit(appended.offset);
|
|
131
|
+
return appended.written;
|
|
132
|
+
}
|
|
133
|
+
const written = result as number;
|
|
134
|
+
if (written > 0) lease.commit(lease.position + written);
|
|
135
|
+
return written;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private deferAsyncWrite(
|
|
139
|
+
filesystem: FilesystemClient,
|
|
140
|
+
completion: FilesystemCompletion,
|
|
141
|
+
lease: AsyncFileOffsetLease,
|
|
142
|
+
): void {
|
|
143
|
+
filesystem.trackDeferredWrite(completion, () => {
|
|
144
|
+
try {
|
|
145
|
+
const settled = completion.tryConsumeCommitted();
|
|
146
|
+
if (settled !== undefined) this.commitAsyncWrite(lease, settled);
|
|
147
|
+
} catch {
|
|
148
|
+
// Aborted or failed write: leave offset unchanged before releasing the lease.
|
|
149
|
+
} finally {
|
|
150
|
+
completion.release();
|
|
151
|
+
lease.release();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private async writeAsyncFileLease(
|
|
120
157
|
filesystem: FilesystemClient,
|
|
121
158
|
resource: Extract<Resource, { kind: "file" }>,
|
|
122
|
-
lease:
|
|
159
|
+
lease: AsyncFileOffsetLease,
|
|
123
160
|
bytes: Uint8Array,
|
|
124
|
-
|
|
161
|
+
onDefer: () => void,
|
|
125
162
|
): Promise<number> {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
163
|
+
const completion = lease.append
|
|
164
|
+
? filesystem.submit("append", [resource.handle], bytes)
|
|
165
|
+
: filesystem.submit("write", [resource.handle, lease.position], bytes);
|
|
166
|
+
let handedOff = false;
|
|
167
|
+
try {
|
|
168
|
+
const result = await completion.value();
|
|
169
|
+
return this.commitAsyncWrite(lease, result);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
const committed = completion.tryConsumeCommitted();
|
|
172
|
+
if (committed !== undefined) {
|
|
173
|
+
this.commitAsyncWrite(lease, committed);
|
|
174
|
+
} else {
|
|
175
|
+
handedOff = true;
|
|
176
|
+
onDefer();
|
|
177
|
+
this.deferAsyncWrite(filesystem, completion, lease);
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
} finally {
|
|
181
|
+
if (!handedOff) completion.release();
|
|
135
182
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async runLeasedTransfer(
|
|
186
|
+
filesystem: FilesystemClient,
|
|
187
|
+
resource: Extract<Resource, { kind: "file" }>,
|
|
188
|
+
lease: AsyncFileOffsetLease,
|
|
189
|
+
bytes: Uint8Array,
|
|
190
|
+
writing: boolean,
|
|
191
|
+
): Promise<number> {
|
|
192
|
+
let releaseLease = true;
|
|
193
|
+
const defer = () => {
|
|
194
|
+
releaseLease = false;
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
if (writing) {
|
|
198
|
+
const written = await this.writeAsyncFileLease(filesystem, resource, lease, bytes, defer);
|
|
199
|
+
lease.release();
|
|
200
|
+
releaseLease = false;
|
|
201
|
+
this.check();
|
|
202
|
+
return written;
|
|
203
|
+
}
|
|
204
|
+
const read: Uint8Array = await filesystem.asyncCall("read", [
|
|
205
|
+
resource.handle,
|
|
206
|
+
bytes.length,
|
|
207
|
+
lease.position,
|
|
208
|
+
]);
|
|
209
|
+
bytes.set(read, 0);
|
|
210
|
+
if (read.length > 0) lease.commit(lease.position + read.length);
|
|
143
211
|
this.check();
|
|
144
|
-
return
|
|
212
|
+
return read.length;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (error instanceof CommandStdioError) throw error;
|
|
215
|
+
throw stdioError(-errno(error));
|
|
216
|
+
} finally {
|
|
217
|
+
if (releaseLease) lease.release();
|
|
145
218
|
}
|
|
146
|
-
const read: Uint8Array = await filesystem.asyncCall("read", [
|
|
147
|
-
resource.handle,
|
|
148
|
-
bytes.length,
|
|
149
|
-
lease.position,
|
|
150
|
-
]);
|
|
151
|
-
bytes.set(read, 0);
|
|
152
|
-
if (read.length > 0) lease.commit(lease.position + read.length);
|
|
153
|
-
this.check();
|
|
154
|
-
return read.length;
|
|
155
219
|
}
|
|
156
220
|
|
|
157
221
|
private async transferAsyncFile(
|
|
@@ -165,21 +229,13 @@ export class CommandStdio {
|
|
|
165
229
|
shareFileResource(resource, filesystem.connection);
|
|
166
230
|
for (;;) {
|
|
167
231
|
this.check();
|
|
168
|
-
const claim = claimFileOffset(resource);
|
|
232
|
+
const claim = claimFileOffset(resource, writing);
|
|
169
233
|
if (claim.kind === "failed") throw stdioError(-claim.errno);
|
|
170
234
|
if (claim.kind === "busy") {
|
|
171
235
|
await this.wait([claim.word], [claim.observed]);
|
|
172
236
|
continue;
|
|
173
237
|
}
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
return await this.runAsyncFileLease(filesystem, resource, lease, bytes, writing);
|
|
177
|
-
} catch (error) {
|
|
178
|
-
if (error instanceof CommandStdioError) throw error;
|
|
179
|
-
throw stdioError(-errno(error));
|
|
180
|
-
} finally {
|
|
181
|
-
lease.release();
|
|
182
|
-
}
|
|
238
|
+
return await this.runLeasedTransfer(filesystem, resource, claim.lease, bytes, writing);
|
|
183
239
|
}
|
|
184
240
|
}
|
|
185
241
|
|
|
@@ -250,6 +306,7 @@ export class CommandStdio {
|
|
|
250
306
|
|
|
251
307
|
async settle(): Promise<void> {
|
|
252
308
|
while (this.pending.size) await Promise.allSettled(Array.from(this.pending));
|
|
309
|
+
if (this.filesystem?.hasDeferredWrites()) await this.filesystem.settleDeferredWrites();
|
|
253
310
|
await this.filesystem?.stopResidentPublication();
|
|
254
311
|
this.filesystem?.dispose();
|
|
255
312
|
}
|
package/proc/registry.ts
CHANGED
|
@@ -130,10 +130,13 @@ export class ExecutableNotRunnable extends Error {
|
|
|
130
130
|
this.name = "ExecutableNotRunnable";
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
+
/** Guest programs a command tree may run: only those admitted from Wasm bytes, or compiled
|
|
134
|
+
* cooperative modules as well. Neither admits a host command or a registered script. */
|
|
135
|
+
export type CommandPrograms = "byte-backed" | "guest";
|
|
133
136
|
/** Generic names -> compiled module + adapter, with one compilation per module source.
|
|
134
137
|
* Instances and used workers are deliberately not cached.
|
|
135
138
|
*/
|
|
136
|
-
type AdmissionMode = "access" | "execute" |
|
|
139
|
+
type AdmissionMode = "access" | "execute" | CommandPrograms;
|
|
137
140
|
|
|
138
141
|
export class ExecutableRegistry {
|
|
139
142
|
private readonly entries = new Map<string, RegisteredExecutable | ScriptExecutable>();
|
|
@@ -181,15 +184,9 @@ export class ExecutableRegistry {
|
|
|
181
184
|
cwd = "/",
|
|
182
185
|
assertActive: () => void = () => {},
|
|
183
186
|
argv?: readonly string[],
|
|
184
|
-
|
|
187
|
+
commandPrograms?: CommandPrograms,
|
|
185
188
|
): Promise<ResolvedExecutable> {
|
|
186
|
-
const admitted = await this.admit(
|
|
187
|
-
name,
|
|
188
|
-
cwd,
|
|
189
|
-
assertActive,
|
|
190
|
-
argv,
|
|
191
|
-
byteBackedGuestOnly ? "byte-backed-guest" : "execute",
|
|
192
|
-
);
|
|
189
|
+
const admitted = await this.admit(name, cwd, assertActive, argv, commandPrograms ?? "execute");
|
|
193
190
|
const entry = admitted.entry;
|
|
194
191
|
assertActive();
|
|
195
192
|
if ("command" in entry) return this.resolveCommand(entry, admitted.argv, assertActive);
|
|
@@ -337,12 +334,8 @@ export class ExecutableRegistry {
|
|
|
337
334
|
);
|
|
338
335
|
}
|
|
339
336
|
const resolved = file.path === predicted ? registered : this.entries.get(file.path);
|
|
340
|
-
if (mode === "byte-backed
|
|
341
|
-
return this.
|
|
342
|
-
file.kind === "file" ? resolved : undefined,
|
|
343
|
-
file.path,
|
|
344
|
-
argv,
|
|
345
|
-
);
|
|
337
|
+
if (mode === "byte-backed" || mode === "guest")
|
|
338
|
+
return this.admitGuest(file.kind === "file" ? resolved : undefined, file.path, argv, mode);
|
|
346
339
|
const admitted =
|
|
347
340
|
resolved &&
|
|
348
341
|
!(
|
|
@@ -415,7 +408,7 @@ export class ExecutableRegistry {
|
|
|
415
408
|
symlinks: number,
|
|
416
409
|
scriptName: string,
|
|
417
410
|
): Promise<AdmittedExecutable> {
|
|
418
|
-
if (mode === "byte-backed
|
|
411
|
+
if (mode === "byte-backed" || mode === "guest") return this.admitGuest(entry, path, argv, mode);
|
|
419
412
|
if (!entry) throw new ExecutableNotFound(path);
|
|
420
413
|
if ("script" in entry)
|
|
421
414
|
return this.admitRegisteredScript(
|
|
@@ -434,16 +427,17 @@ export class ExecutableRegistry {
|
|
|
434
427
|
return { entry, ...(argv ? { argv } : {}) };
|
|
435
428
|
}
|
|
436
429
|
|
|
437
|
-
private
|
|
430
|
+
private admitGuest(
|
|
438
431
|
entry: RegisteredExecutable | ScriptExecutable | undefined,
|
|
439
432
|
path: string,
|
|
440
|
-
argv
|
|
433
|
+
argv: readonly string[] | undefined,
|
|
434
|
+
programs: CommandPrograms,
|
|
441
435
|
): AdmittedExecutable {
|
|
442
436
|
if (
|
|
443
437
|
!entry ||
|
|
444
438
|
"script" in entry ||
|
|
445
439
|
"command" in entry ||
|
|
446
|
-
!entry.interruptible ||
|
|
440
|
+
(programs === "byte-backed" && !entry.interruptible) ||
|
|
447
441
|
entry.execution?.kind !== "guest"
|
|
448
442
|
)
|
|
449
443
|
throw new ExecutableNotRunnable(path, false);
|
package/proc/start-command.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { Runtime } from "../runtime.js";
|
|
2
|
+
import type { CommandPrograms } from "./registry.js";
|
|
2
3
|
import type { ProcessExit, ProcessSignal } from "./types.js";
|
|
3
4
|
|
|
5
|
+
export type { CommandPrograms } from "./registry.js";
|
|
6
|
+
/** What happens to the rest of the tree when the command's first process ends. */
|
|
7
|
+
export type CommandLeaderExit = "join" | "stop";
|
|
8
|
+
|
|
4
9
|
export interface CommandOutputOptions {
|
|
5
10
|
readonly limitBytes?: number;
|
|
6
11
|
readonly onChunk?: (bytes: Uint8Array) => undefined;
|
|
@@ -8,6 +13,19 @@ export interface CommandOutputOptions {
|
|
|
8
13
|
|
|
9
14
|
export interface CommandOptions {
|
|
10
15
|
readonly filesystem: "runtime-root";
|
|
16
|
+
/**
|
|
17
|
+
* Which guest programs the command and its descendants may start or replace their image
|
|
18
|
+
* with. `"byte-backed"`, the default, admits only programs registered from Wasm bytes.
|
|
19
|
+
* `"guest"` also admits compiled modules registered with `cooperative: true`. Waffle cannot
|
|
20
|
+
* interrupt those: a stop takes effect when the module next polls, or when the browser ends
|
|
21
|
+
* its Worker. Host commands and registered scripts are refused either way.
|
|
22
|
+
*/
|
|
23
|
+
readonly programs?: CommandPrograms;
|
|
24
|
+
/**
|
|
25
|
+
* `"join"`, the default, lets descendants that outlive the first process finish. `"stop"`
|
|
26
|
+
* stops them as `kill()` would once the first process has its outcome, which the result keeps.
|
|
27
|
+
*/
|
|
28
|
+
readonly onLeaderExit?: CommandLeaderExit;
|
|
11
29
|
readonly args?: readonly string[];
|
|
12
30
|
readonly cwd?: string;
|
|
13
31
|
readonly env?: Readonly<Record<string, string>>;
|
|
@@ -62,11 +80,20 @@ function output(value: CommandOutputOptions | "closed" | undefined) {
|
|
|
62
80
|
return { limitBytes, onChunk };
|
|
63
81
|
}
|
|
64
82
|
|
|
83
|
+
function policies({ programs = "byte-backed", onLeaderExit = "join" }: CommandOptions) {
|
|
84
|
+
if (programs !== "byte-backed" && programs !== "guest")
|
|
85
|
+
throw new TypeError('Command programs must be "byte-backed" or "guest"');
|
|
86
|
+
if (onLeaderExit !== "join" && onLeaderExit !== "stop")
|
|
87
|
+
throw new TypeError('Command onLeaderExit must be "join" or "stop"');
|
|
88
|
+
return { programs, onLeaderExit };
|
|
89
|
+
}
|
|
90
|
+
|
|
65
91
|
function snapshot(executable: string, options: CommandOptions): CommandOptions {
|
|
66
92
|
if (options?.filesystem !== "runtime-root")
|
|
67
93
|
throw new TypeError('Commands require filesystem: "runtime-root"');
|
|
68
94
|
if (!path(executable)) throw new TypeError("Command executable must be an absolute path");
|
|
69
95
|
const { args, cwd = "/", env, stdin, stdout, stderr, deadlineMs } = options;
|
|
96
|
+
const { programs, onLeaderExit } = policies(options);
|
|
70
97
|
if (!path(cwd)) throw new TypeError("Command cwd must be an absolute path");
|
|
71
98
|
if (args !== undefined && !Array.isArray(args)) throw new TypeError("Invalid command args");
|
|
72
99
|
const argv = Array.from(args ?? []);
|
|
@@ -92,6 +119,8 @@ function snapshot(executable: string, options: CommandOptions): CommandOptions {
|
|
|
92
119
|
throw new RangeError("Command deadline must be between 1 and 2147483647 ms");
|
|
93
120
|
return {
|
|
94
121
|
filesystem: "runtime-root",
|
|
122
|
+
programs,
|
|
123
|
+
onLeaderExit,
|
|
95
124
|
args: argv,
|
|
96
125
|
cwd,
|
|
97
126
|
env: Object.fromEntries(entries),
|
package/proc/supervisor.ts
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
OpenDescription,
|
|
35
35
|
type Descriptor,
|
|
36
36
|
} from "../core/descriptors.js";
|
|
37
|
-
import { ExecutableRegistry, type ResolvedExecutable } from "./registry.js";
|
|
37
|
+
import { ExecutableRegistry, type CommandPrograms, type ResolvedExecutable } from "./registry.js";
|
|
38
38
|
import {
|
|
39
39
|
ProcessTable,
|
|
40
40
|
type ProcessJob,
|
|
@@ -106,7 +106,9 @@ interface ProcessAccesses {
|
|
|
106
106
|
|
|
107
107
|
export interface SupervisorOptions {
|
|
108
108
|
registry: ExecutableRegistry;
|
|
109
|
-
|
|
109
|
+
/** Defined for a page-owned command root: the programs its tree may run. Such a root also
|
|
110
|
+
* joins its descendants' cleanup, so a descriptor disposal failure fails its fence. */
|
|
111
|
+
commandPrograms?(rootPid: number | undefined): CommandPrograms | undefined;
|
|
110
112
|
launch(context: ProcessContext): ProcessHandle | Promise<ProcessHandle>;
|
|
111
113
|
launchCommand?(context: CommandProcessContext): ProcessHandle | Promise<ProcessHandle>;
|
|
112
114
|
/** Emit while inherited output is owned; an asynchronous sink must stop when active is false.
|
|
@@ -848,7 +850,7 @@ export class ProcessSupervisor {
|
|
|
848
850
|
throw new Error("process exited before executable admission completed");
|
|
849
851
|
},
|
|
850
852
|
image.request!.argv,
|
|
851
|
-
this.options.
|
|
853
|
+
this.options.commandPrograms?.(record.rootPid),
|
|
852
854
|
);
|
|
853
855
|
if (record.result || record.revoked) return;
|
|
854
856
|
const settled = readOutcome(record.outcome);
|
|
@@ -1019,7 +1021,7 @@ export class ProcessSupervisor {
|
|
|
1019
1021
|
replacement.request.cwd,
|
|
1020
1022
|
active,
|
|
1021
1023
|
replacement.request.argv,
|
|
1022
|
-
this.options.
|
|
1024
|
+
this.options.commandPrograms?.(record.rootPid),
|
|
1023
1025
|
);
|
|
1024
1026
|
active();
|
|
1025
1027
|
if (record.processKind === "thread")
|
|
@@ -1099,7 +1101,7 @@ export class ProcessSupervisor {
|
|
|
1099
1101
|
if (image.stopping) return image.stopping;
|
|
1100
1102
|
const stop = async () => {
|
|
1101
1103
|
this.revokeImage(image);
|
|
1102
|
-
if (this.options.
|
|
1104
|
+
if (this.options.commandPrograms?.(record.rootPid)) image.descriptors.dispose();
|
|
1103
1105
|
else this.disposeDescriptors(image.descriptors);
|
|
1104
1106
|
await this.options.stopImage!(record.pid, image.generation, () =>
|
|
1105
1107
|
this.replacementActive(record, replacement),
|
|
@@ -1558,7 +1560,7 @@ export class ProcessSupervisor {
|
|
|
1558
1560
|
for (const hook of hooks) this.safely(hook);
|
|
1559
1561
|
this.revokeImage(record.image);
|
|
1560
1562
|
this.disposeDescriptors(record.image.descriptors, (error) => {
|
|
1561
|
-
if (!this.options.
|
|
1563
|
+
if (!this.options.commandPrograms?.(record.rootPid)) {
|
|
1562
1564
|
this.report(error);
|
|
1563
1565
|
return;
|
|
1564
1566
|
}
|
package/thread/service.ts
CHANGED
|
@@ -272,12 +272,28 @@ export class ThreadService {
|
|
|
272
272
|
await Promise.race([previous?.catch(() => {}), this.cancelled.promise]);
|
|
273
273
|
this.live();
|
|
274
274
|
const lease = await this.offset(resource);
|
|
275
|
+
const leaseState: { current: "held" | "settled" | "deferred" } = { current: "held" };
|
|
275
276
|
try {
|
|
276
|
-
return await this.file(operation, resource, lease.position, lease)
|
|
277
|
+
return await this.file(operation, resource, lease.position, lease, (next) => {
|
|
278
|
+
leaseState.current = next;
|
|
279
|
+
});
|
|
277
280
|
} finally {
|
|
278
281
|
// Revocation can reject value() while remote preparation still owns this operation.
|
|
279
|
-
if (this.phase !== "open") await this.
|
|
280
|
-
lease.release();
|
|
282
|
+
if (this.phase !== "open") await this.retireSequential(operation, lease, leaseState.current);
|
|
283
|
+
if (leaseState.current !== "deferred") lease.release();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private async retireSequential(
|
|
288
|
+
operation: Operation,
|
|
289
|
+
lease: AsyncFileOffsetLease,
|
|
290
|
+
leaseState: "held" | "settled" | "deferred",
|
|
291
|
+
): Promise<void> {
|
|
292
|
+
try {
|
|
293
|
+
await this.retire(operation);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (leaseState === "settled") lease.release();
|
|
296
|
+
throw error;
|
|
281
297
|
}
|
|
282
298
|
}
|
|
283
299
|
|
|
@@ -296,6 +312,7 @@ export class ThreadService {
|
|
|
296
312
|
resource: FileResource,
|
|
297
313
|
position: number,
|
|
298
314
|
lease?: AsyncFileOffsetLease,
|
|
315
|
+
onLeaseState?: (state: "settled" | "deferred") => void,
|
|
299
316
|
): Promise<ThreadResult> {
|
|
300
317
|
this.live();
|
|
301
318
|
const request = operation.request;
|
|
@@ -311,6 +328,7 @@ export class ThreadService {
|
|
|
311
328
|
this.position(next);
|
|
312
329
|
this.live();
|
|
313
330
|
lease!.commit(next);
|
|
331
|
+
onLeaseState?.("settled");
|
|
314
332
|
return { kind: "ok", value: next };
|
|
315
333
|
}
|
|
316
334
|
if (request.kind !== "read" && request.kind !== "write") throw new FilesystemError(58);
|
|
@@ -325,6 +343,7 @@ export class ThreadService {
|
|
|
325
343
|
position,
|
|
326
344
|
]);
|
|
327
345
|
lease?.commit(position + bytes.length);
|
|
346
|
+
if (lease) onLeaseState?.("settled");
|
|
328
347
|
this.live();
|
|
329
348
|
this.scatter(request.spans, bytes);
|
|
330
349
|
return { kind: "ok", value: bytes.length };
|
|
@@ -336,19 +355,45 @@ export class ThreadService {
|
|
|
336
355
|
append ? "append" : "write",
|
|
337
356
|
[resource.handle, position],
|
|
338
357
|
data,
|
|
358
|
+
lease
|
|
359
|
+
? {
|
|
360
|
+
lease,
|
|
361
|
+
commit: (committed) => {
|
|
362
|
+
const committedWritten: number = append ? committed.written : committed;
|
|
363
|
+
lease.commit(append ? committed.offset : position + committedWritten);
|
|
364
|
+
},
|
|
365
|
+
onState: onLeaseState,
|
|
366
|
+
}
|
|
367
|
+
: undefined,
|
|
339
368
|
);
|
|
340
369
|
const written: number = append ? result.written : result;
|
|
341
|
-
lease?.commit(append ? result.offset : position + written);
|
|
342
370
|
this.live();
|
|
343
371
|
return { kind: "ok", value: written };
|
|
344
372
|
}
|
|
345
373
|
|
|
346
|
-
private async request(
|
|
374
|
+
private async request(
|
|
375
|
+
operation: Operation,
|
|
376
|
+
name: string,
|
|
377
|
+
args: unknown[],
|
|
378
|
+
data?: Uint8Array,
|
|
379
|
+
writeLease?: {
|
|
380
|
+
lease: AsyncFileOffsetLease;
|
|
381
|
+
commit: (value: any) => void;
|
|
382
|
+
onState?: (state: "settled" | "deferred") => void;
|
|
383
|
+
},
|
|
384
|
+
) {
|
|
347
385
|
this.live();
|
|
348
386
|
const completion = this.options.filesystem.submit(name, args, data);
|
|
349
387
|
operation.completions.add(completion);
|
|
388
|
+
let committedApplied = false;
|
|
350
389
|
try {
|
|
351
|
-
|
|
390
|
+
const value = await completion.value();
|
|
391
|
+
if (writeLease) {
|
|
392
|
+
committedApplied = true;
|
|
393
|
+
writeLease.commit(value);
|
|
394
|
+
writeLease.onState?.("settled");
|
|
395
|
+
}
|
|
396
|
+
return value;
|
|
352
397
|
} catch (error) {
|
|
353
398
|
if (
|
|
354
399
|
error instanceof FilesystemTransportError ||
|
|
@@ -357,10 +402,83 @@ export class ThreadService {
|
|
|
357
402
|
this.cancelAll();
|
|
358
403
|
throw error;
|
|
359
404
|
} finally {
|
|
360
|
-
|
|
405
|
+
await this.finishRequest(operation, completion, committedApplied, writeLease);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private async finishRequest(
|
|
410
|
+
operation: Operation,
|
|
411
|
+
completion: FilesystemCompletion,
|
|
412
|
+
committedApplied: boolean,
|
|
413
|
+
writeLease?: {
|
|
414
|
+
lease: AsyncFileOffsetLease;
|
|
415
|
+
commit: (value: any) => void;
|
|
416
|
+
onState?: (state: "settled" | "deferred") => void;
|
|
417
|
+
},
|
|
418
|
+
): Promise<void> {
|
|
419
|
+
const retiredError = this.phase !== "open" ? await this.tryRetire(operation) : undefined;
|
|
420
|
+
if (writeLease && !committedApplied) {
|
|
421
|
+
this.settleWriteLease(operation, completion, writeLease, retiredError === undefined);
|
|
422
|
+
} else if (retiredError === undefined || committedApplied || completion.backendSettled) {
|
|
361
423
|
completion.release();
|
|
362
424
|
operation.completions.delete(completion);
|
|
363
425
|
}
|
|
426
|
+
if (retiredError !== undefined) throw retiredError;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private settleWriteLease(
|
|
430
|
+
operation: Operation,
|
|
431
|
+
completion: FilesystemCompletion,
|
|
432
|
+
writeLease: {
|
|
433
|
+
lease: AsyncFileOffsetLease;
|
|
434
|
+
commit: (value: any) => void;
|
|
435
|
+
onState?: (state: "settled" | "deferred") => void;
|
|
436
|
+
},
|
|
437
|
+
retiredCleanly: boolean,
|
|
438
|
+
): void {
|
|
439
|
+
// Check monotonic backendSettled BEFORE tryConsumeCommitted so a Pending->Complete
|
|
440
|
+
// transition can never release the completion without applying the committed offset.
|
|
441
|
+
if (retiredCleanly || completion.backendSettled) {
|
|
442
|
+
this.consumeCommitted(completion, writeLease.commit);
|
|
443
|
+
writeLease.onState?.("settled");
|
|
444
|
+
completion.release();
|
|
445
|
+
operation.completions.delete(completion);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
writeLease.onState?.("deferred");
|
|
449
|
+
this.options.filesystem.trackDeferredWrite(completion, () => {
|
|
450
|
+
try {
|
|
451
|
+
this.consumeCommitted(completion, writeLease.commit);
|
|
452
|
+
} finally {
|
|
453
|
+
completion.release();
|
|
454
|
+
operation.completions.delete(completion);
|
|
455
|
+
writeLease.lease.release();
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private async tryRetire(operation: Operation): Promise<unknown> {
|
|
461
|
+
try {
|
|
462
|
+
await this.retire(operation);
|
|
463
|
+
return undefined;
|
|
464
|
+
} catch (error) {
|
|
465
|
+
return error;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
private consumeCommitted(
|
|
470
|
+
completion: FilesystemCompletion,
|
|
471
|
+
onCommitted: (value: any) => void,
|
|
472
|
+
): boolean {
|
|
473
|
+
try {
|
|
474
|
+
const committed = completion.tryConsumeCommitted();
|
|
475
|
+
if (committed === undefined) return false;
|
|
476
|
+
onCommitted(committed);
|
|
477
|
+
return true;
|
|
478
|
+
} catch {
|
|
479
|
+
// Aborted or failed write: leave offset unchanged before releasing the completion.
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
364
482
|
}
|
|
365
483
|
|
|
366
484
|
private async stream(operation: Operation): Promise<ThreadResult> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commit":"
|
|
1
|
+
{"commit":"f2b1143915c41d86a9ef930b7f9d50e5228b1a05","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
|