@waffle-os/runtime 0.0.149 → 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/worker.ts +34 -5
- package/core/open-file.ts +126 -52
- package/dist/browser/diagnostic.js +150 -1
- 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/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/thread/service.ts +125 -7
- package/tooling/program/program-transformer.json +1 -1
package/fs/protocol.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { FilesystemSection } from "./section.js";
|
|
|
9
9
|
export { FilesystemSection, isRevoked } from "./section.js";
|
|
10
10
|
import { workspacePublication, type WorkspaceConfiguration } from "./workspace-protocol.js";
|
|
11
11
|
import {
|
|
12
|
+
atomicsAdd,
|
|
13
|
+
atomicsNotify,
|
|
12
14
|
BigIntConstructor,
|
|
13
15
|
bigintToString,
|
|
14
16
|
Int32ArrayConstructor,
|
|
@@ -16,6 +18,7 @@ import {
|
|
|
16
18
|
objectKeys,
|
|
17
19
|
stringIncludes,
|
|
18
20
|
SharedArrayBufferConstructor,
|
|
21
|
+
typedArrayBuffer,
|
|
19
22
|
Uint8ArrayConstructor,
|
|
20
23
|
objectDefineProperty,
|
|
21
24
|
} from "../proc/intrinsics.js";
|
|
@@ -456,9 +459,9 @@ export function reapFilesystem(connection: FilesystemIdentity): void {
|
|
|
456
459
|
wake(connection);
|
|
457
460
|
}
|
|
458
461
|
export function wake(connection: FilesystemIdentity): void {
|
|
459
|
-
const signal = new
|
|
460
|
-
|
|
461
|
-
|
|
462
|
+
const signal = new Int32ArrayConstructor(connection.signal);
|
|
463
|
+
atomicsAdd(signal, 0, 1);
|
|
464
|
+
atomicsNotify(signal, 0);
|
|
462
465
|
}
|
|
463
466
|
const encoder = new TextEncoder();
|
|
464
467
|
const decoder = new TextDecoder();
|
|
@@ -496,7 +499,9 @@ function decodable(buffer: ArrayBufferLike): boolean {
|
|
|
496
499
|
}
|
|
497
500
|
export function unpack(bytes: Uint8Array): any {
|
|
498
501
|
// TextDecoder rejects views over shared or resizable backing, so only those are copied.
|
|
499
|
-
const text = decoder.decode(
|
|
502
|
+
const text = decoder.decode(
|
|
503
|
+
decodable(typedArrayBuffer(bytes)) ? bytes : new Uint8ArrayConstructor(bytes),
|
|
504
|
+
);
|
|
500
505
|
const value: unknown = JSON.parse(text);
|
|
501
506
|
// A tag is an own object key beginning with "$", which JSON writes as `"$` or as the escape
|
|
502
507
|
// for U+0024. Without either, no object is tagged and the parsed tree is already the answer.
|
package/fs/service.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
retainResponseBytes,
|
|
9
9
|
captureRequestMemory,
|
|
10
10
|
closeRequestMemory,
|
|
11
|
+
requestMemoryRetired,
|
|
11
12
|
retireRequestMemory,
|
|
12
13
|
requestTransportError,
|
|
13
14
|
requestLengths,
|
|
@@ -148,6 +149,8 @@ interface Session {
|
|
|
148
149
|
stages: Map<number, SessionStage>;
|
|
149
150
|
retainedRequests: Map<number, FilesystemRequestBuffer>;
|
|
150
151
|
pending: Map<number, FilesystemRequestBuffer>;
|
|
152
|
+
/** Scalar write/append requests already dispatched to backend execution. */
|
|
153
|
+
inFlightWrites: Set<number>;
|
|
151
154
|
/** Transferred inputs by request id. Removed only when that request's work settles. */
|
|
152
155
|
privateInputs: Map<number, Uint8Array>;
|
|
153
156
|
preparingHandles: Map<number, number>;
|
|
@@ -161,6 +164,10 @@ interface Session {
|
|
|
161
164
|
reader?: WorkspaceReadAuthority;
|
|
162
165
|
readerClosing?: Promise<void>;
|
|
163
166
|
}
|
|
167
|
+
function isCommittedScalarWrite(operation: string, state: State): boolean {
|
|
168
|
+
return (operation === "write" || operation === "append") && state === State.Complete;
|
|
169
|
+
}
|
|
170
|
+
|
|
164
171
|
function sessionRevoked(session: Session): boolean {
|
|
165
172
|
return isRevoked(session.connection) || !!requestTransportError(session.requests);
|
|
166
173
|
}
|
|
@@ -296,6 +303,7 @@ export class FilesystemService {
|
|
|
296
303
|
stages: new Map(),
|
|
297
304
|
retainedRequests: new Map(),
|
|
298
305
|
pending: new Map(),
|
|
306
|
+
inFlightWrites: new Set(),
|
|
299
307
|
privateInputs: new Map(),
|
|
300
308
|
preparingHandles: new Map(),
|
|
301
309
|
preparations: new Set(),
|
|
@@ -1189,11 +1197,11 @@ export class FilesystemService {
|
|
|
1189
1197
|
try {
|
|
1190
1198
|
this.finishSocketOperation(session, operation, result, args);
|
|
1191
1199
|
const pending = session.pending.get(id);
|
|
1200
|
+
const scalarWrite = operation === "write" || operation === "append";
|
|
1192
1201
|
if (
|
|
1193
|
-
sessionRevoked(session) ||
|
|
1194
|
-
!this.sessions.has(session) ||
|
|
1195
1202
|
!pending ||
|
|
1196
|
-
|
|
1203
|
+
(!scalarWrite &&
|
|
1204
|
+
(sessionRevoked(session) || !this.sessions.has(session) || requestCancelled(pending)))
|
|
1197
1205
|
)
|
|
1198
1206
|
throw new FilesystemError(11);
|
|
1199
1207
|
if (operation === "preopen" || (operation === "create" && typeof result === "number"))
|
|
@@ -1295,6 +1303,7 @@ export class FilesystemService {
|
|
|
1295
1303
|
let opened = false;
|
|
1296
1304
|
let preparing = false;
|
|
1297
1305
|
let staging = false;
|
|
1306
|
+
let committedWrite = false;
|
|
1298
1307
|
let admission: PublicationAdmission | undefined;
|
|
1299
1308
|
let dirtyRetry: number | undefined;
|
|
1300
1309
|
const epoch = session.epoch;
|
|
@@ -1340,6 +1349,7 @@ export class FilesystemService {
|
|
|
1340
1349
|
prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid())
|
|
1341
1350
|
? prepared.read
|
|
1342
1351
|
: undefined;
|
|
1352
|
+
if (operation === "write" || operation === "append") session.inFlightWrites.add(id);
|
|
1343
1353
|
const value = preparedRead
|
|
1344
1354
|
? this.finishResult(session, id, operation, preparedRead.value)
|
|
1345
1355
|
: local && operation !== "flush" && operation !== "sync"
|
|
@@ -1348,6 +1358,7 @@ export class FilesystemService {
|
|
|
1348
1358
|
this.announceSegments(this.backend.takeSharedSegments?.() ?? [], this.sessions);
|
|
1349
1359
|
state = this.writeResponse(session, id, cells, response, value, bytes);
|
|
1350
1360
|
staging = operation === "stageWrite";
|
|
1361
|
+
committedWrite = isCommittedScalarWrite(operation, state);
|
|
1351
1362
|
} catch (error) {
|
|
1352
1363
|
this.discardResponse(session, id);
|
|
1353
1364
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
@@ -1373,23 +1384,20 @@ export class FilesystemService {
|
|
|
1373
1384
|
this.cancelPublication(session, admission);
|
|
1374
1385
|
this.releasePrepared(session, prepared);
|
|
1375
1386
|
cells[5] = Atomics.load(epoch, 0);
|
|
1376
|
-
|
|
1377
|
-
this.discardResponse(session, id);
|
|
1378
|
-
state = State.Cancelled;
|
|
1379
|
-
}
|
|
1380
|
-
// Complete is what lets the client commit a stage. Never publish it for a stage the
|
|
1381
|
-
// owner no longer holds, or that commit would have nothing to apply.
|
|
1382
|
-
if (staging && !session.stages.has(id)) state = State.Cancelled;
|
|
1387
|
+
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1383
1388
|
this.settleWalk(session, id, state === State.Complete);
|
|
1384
1389
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1385
1390
|
Atomics.store(cells, 0, state);
|
|
1386
1391
|
Atomics.notify(cells, 0);
|
|
1387
1392
|
wake(session.connection);
|
|
1393
|
+
session.inFlightWrites.delete(id);
|
|
1388
1394
|
session.pending.delete(id);
|
|
1389
1395
|
session.privateInputs.delete(id);
|
|
1390
1396
|
this.retainRequestReply(session, id, buffer);
|
|
1391
1397
|
session.connection.port.postMessage({ completed: id });
|
|
1392
1398
|
this.sweep();
|
|
1399
|
+
} else {
|
|
1400
|
+
session.inFlightWrites.delete(id);
|
|
1393
1401
|
}
|
|
1394
1402
|
}
|
|
1395
1403
|
if (dirtyRetry !== undefined && prepared) {
|
|
@@ -1400,6 +1408,25 @@ export class FilesystemService {
|
|
|
1400
1408
|
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1401
1409
|
}
|
|
1402
1410
|
}
|
|
1411
|
+
private settleCancelledState(
|
|
1412
|
+
session: Session,
|
|
1413
|
+
id: number,
|
|
1414
|
+
buffer: FilesystemRequestBuffer,
|
|
1415
|
+
state: State,
|
|
1416
|
+
staging: boolean,
|
|
1417
|
+
committedWrite: boolean,
|
|
1418
|
+
): State {
|
|
1419
|
+
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
1420
|
+
this.discardResponse(session, id);
|
|
1421
|
+
// Preserve inline committed write/append results so shared-offset leases observe
|
|
1422
|
+
// an already-landed mutation before releasing the offset lock.
|
|
1423
|
+
if (!committedWrite) state = State.Cancelled;
|
|
1424
|
+
}
|
|
1425
|
+
// Complete is what lets the client commit a stage. Never publish it for a stage the
|
|
1426
|
+
// owner no longer holds, or that commit would have nothing to apply.
|
|
1427
|
+
if (staging && !session.stages.has(id)) return State.Cancelled;
|
|
1428
|
+
return state;
|
|
1429
|
+
}
|
|
1403
1430
|
private holdResponseMemory(session: Session, id: number, release: () => void): void {
|
|
1404
1431
|
const prior = session.responseMemory.get(id);
|
|
1405
1432
|
session.responseMemory.set(id, () => {
|
|
@@ -1904,6 +1931,16 @@ export class FilesystemService {
|
|
|
1904
1931
|
this.backend.shared.publication?.releasePin(id);
|
|
1905
1932
|
session.publicationPins?.clear();
|
|
1906
1933
|
}
|
|
1934
|
+
private cancelPendingRequests(session: Session): void {
|
|
1935
|
+
for (const [id, buffer] of session.pending) {
|
|
1936
|
+
const cells = requestCells(buffer);
|
|
1937
|
+
Atomics.store(cells, 6, 1);
|
|
1938
|
+
Atomics.store(cells, 0, State.Cancelled);
|
|
1939
|
+
Atomics.notify(cells, 0);
|
|
1940
|
+
session.connection.port.postMessage({ completed: id });
|
|
1941
|
+
}
|
|
1942
|
+
session.pending.clear();
|
|
1943
|
+
}
|
|
1907
1944
|
private releaseSessionPins(session: Session): void {
|
|
1908
1945
|
this.cancelSessionWatchResponses(session);
|
|
1909
1946
|
this.watchService?.closeOwner(session.connection.owner);
|
|
@@ -1940,18 +1977,14 @@ export class FilesystemService {
|
|
|
1940
1977
|
);
|
|
1941
1978
|
return;
|
|
1942
1979
|
}
|
|
1980
|
+
// Keep the session open until every already-dispatched write/append publishes its
|
|
1981
|
+
// authoritative commit or abort state; request()'s finally calls sweep() on completion.
|
|
1982
|
+
if (session.inFlightWrites.size) return;
|
|
1943
1983
|
if (ownRevocation === 2) this.backend.revokeOwner?.(session.connection.owner, session.epoch);
|
|
1944
1984
|
// A discard can be lost when a port closes. Retirement settles what remains, before this
|
|
1945
1985
|
// session's file pins and request blocks go: a committed write is applied, never dropped.
|
|
1946
1986
|
for (const id of Array.from(session.stages.keys())) this.settleStage(session, id);
|
|
1947
|
-
|
|
1948
|
-
const cells = requestCells(buffer);
|
|
1949
|
-
Atomics.store(cells, 6, 1);
|
|
1950
|
-
Atomics.store(cells, 0, State.Cancelled);
|
|
1951
|
-
Atomics.notify(cells, 0);
|
|
1952
|
-
session.connection.port.postMessage({ completed: id });
|
|
1953
|
-
}
|
|
1954
|
-
session.pending.clear();
|
|
1987
|
+
this.cancelPendingRequests(session);
|
|
1955
1988
|
wake(session.connection);
|
|
1956
1989
|
for (const id of session.walkTransactions.keys()) this.settleWalk(session, id, false);
|
|
1957
1990
|
session.walks?.dispose();
|
|
@@ -2118,7 +2151,54 @@ export class FilesystemService {
|
|
|
2118
2151
|
}
|
|
2119
2152
|
private async shutdownFence(session: Session): Promise<void> {
|
|
2120
2153
|
revokeFilesystemService(session.connection);
|
|
2121
|
-
|
|
2154
|
+
const quiesced = waitForFilesystemQuiescence(session.connection);
|
|
2155
|
+
let done = false;
|
|
2156
|
+
void quiesced.finally(() => {
|
|
2157
|
+
done = true;
|
|
2158
|
+
wake(session.connection);
|
|
2159
|
+
});
|
|
2160
|
+
const signal = new Int32Array(session.connection.signal);
|
|
2161
|
+
while (!done) {
|
|
2162
|
+
const observed = Atomics.load(signal, 0);
|
|
2163
|
+
if (requestTransportError(session.requests)) {
|
|
2164
|
+
session.transportFailed = true;
|
|
2165
|
+
await this.drainFailedSessionRequests(session);
|
|
2166
|
+
break;
|
|
2167
|
+
}
|
|
2168
|
+
const waiting = atomicsWaitAsync(signal, 0, observed);
|
|
2169
|
+
if (waiting.async) await waiting.value;
|
|
2170
|
+
}
|
|
2171
|
+
await quiesced;
|
|
2172
|
+
}
|
|
2173
|
+
private async drainFailedSessionRequests(session: Session): Promise<void> {
|
|
2174
|
+
// A transport error stops receive() from accepting new descriptors. Drain every already-
|
|
2175
|
+
// accepted request (including flush/sync barriers kept outside this.queue) and preparation
|
|
2176
|
+
// first, then retire transport memory so undelivered client writes can release their leases.
|
|
2177
|
+
await waitForRequestQuiescence(session.connection, session.requests);
|
|
2178
|
+
for (;;) {
|
|
2179
|
+
const queue = this.queue;
|
|
2180
|
+
await queue;
|
|
2181
|
+
const pending = Array.from(this.runningRequests).filter(
|
|
2182
|
+
(done) => this.work.get(done) === session,
|
|
2183
|
+
);
|
|
2184
|
+
if (pending.length) await Promise.allSettled(pending);
|
|
2185
|
+
if (session.preparations.size) await Promise.allSettled(Array.from(session.preparations));
|
|
2186
|
+
const stillRunning = Array.from(this.runningRequests).some(
|
|
2187
|
+
(done) => this.work.get(done) === session,
|
|
2188
|
+
);
|
|
2189
|
+
if (
|
|
2190
|
+
queue === this.queue &&
|
|
2191
|
+
!stillRunning &&
|
|
2192
|
+
!session.preparations.size &&
|
|
2193
|
+
!session.inFlightWrites.size
|
|
2194
|
+
)
|
|
2195
|
+
break;
|
|
2196
|
+
}
|
|
2197
|
+
this.cancelPendingRequests(session);
|
|
2198
|
+
const failure = requestTransportError(session.requests);
|
|
2199
|
+
if (failure?.reason !== "stale" && !requestMemoryRetired(session.requests))
|
|
2200
|
+
retireRequestMemory(session.requests);
|
|
2201
|
+
wake(session.connection);
|
|
2122
2202
|
}
|
|
2123
2203
|
private async drainShutdown(owner: ShutdownOwner, fences: Promise<void>[]): Promise<void> {
|
|
2124
2204
|
const failures: unknown[] = [];
|
package/fs/shared.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
type FileId,
|
|
16
16
|
type FileVersion,
|
|
17
17
|
} from "./file-identity.js";
|
|
18
|
-
import { typedArraySet } from "../proc/intrinsics.js";
|
|
18
|
+
import { Int32ArrayConstructor, atomicsLoad, typedArraySet } from "../proc/intrinsics.js";
|
|
19
19
|
import {
|
|
20
20
|
PublicationDomain,
|
|
21
21
|
type PublicationAdmission,
|
|
@@ -289,10 +289,10 @@ export class SharedFilesystemState {
|
|
|
289
289
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
290
290
|
}
|
|
291
291
|
isDirectDisabled(): boolean {
|
|
292
|
-
return (
|
|
292
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
293
293
|
}
|
|
294
294
|
isClientCacheDisabled(): boolean {
|
|
295
|
-
return (
|
|
295
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_CLIENT_CACHE) !== 0;
|
|
296
296
|
}
|
|
297
297
|
/** Reset all per-mount filesystem state in place across Host remount/demotion while preserving
|
|
298
298
|
* tab-lifetime header invariants (`MAGIC`, `maxSegments`, owner/probe words, `WATCH_OFFSET`,
|
|
@@ -2491,13 +2491,13 @@ export function sharedRevoked(connection: {
|
|
|
2491
2491
|
ownerStates: SharedArrayBuffer;
|
|
2492
2492
|
publication?: PublicationConnection;
|
|
2493
2493
|
}): () => boolean {
|
|
2494
|
-
const words = [new
|
|
2495
|
-
connection.revocations.map((buffer) => new
|
|
2494
|
+
const words = [new Int32ArrayConstructor(connection.ownerStates, 0, 1)].concat(
|
|
2495
|
+
connection.revocations.map((buffer) => new Int32ArrayConstructor(buffer)),
|
|
2496
2496
|
);
|
|
2497
2497
|
if (connection.publication)
|
|
2498
|
-
words.push(new
|
|
2498
|
+
words.push(new Int32ArrayConstructor(connection.publication.buffers.control, 0, 1));
|
|
2499
2499
|
return () => {
|
|
2500
|
-
for (let i = 0; i < words.length; i++) if (
|
|
2500
|
+
for (let i = 0; i < words.length; i++) if (atomicsLoad(words[i]!, 0) !== 0) return true;
|
|
2501
2501
|
return false;
|
|
2502
2502
|
};
|
|
2503
2503
|
}
|
package/package.json
CHANGED
package/proc/abi.ts
CHANGED
|
@@ -34,7 +34,12 @@ import {
|
|
|
34
34
|
type Stream,
|
|
35
35
|
} from "../core/descriptors.js";
|
|
36
36
|
import type { FileSystem } from "../core/filesystem.js";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
tryClaimFileOffset,
|
|
39
|
+
withFileOffset,
|
|
40
|
+
type AsyncFileOffsetLease,
|
|
41
|
+
} from "../core/open-file.js";
|
|
42
|
+
import type { DirectDisabledIOControl, FilesystemCompletion } from "../fs/client.js";
|
|
38
43
|
import { PipeController } from "./pipes.js";
|
|
39
44
|
import { readOutcome } from "./outcome.js";
|
|
40
45
|
import { WasiExit } from "../core/wasi.js";
|
|
@@ -101,6 +106,16 @@ export interface ProcessService {
|
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
interface NonblockingFileSystem extends FileSystem {
|
|
109
|
+
readonly signal?: Int32Array;
|
|
110
|
+
readonly directDisabled?: boolean;
|
|
111
|
+
directDisabledIO?(
|
|
112
|
+
operation: "read" | "write" | "append",
|
|
113
|
+
handle: number,
|
|
114
|
+
bytes: Uint8Array,
|
|
115
|
+
offset: number,
|
|
116
|
+
control: DirectDisabledIOControl,
|
|
117
|
+
): { written: number; offset: number } | number;
|
|
118
|
+
trackDeferredWrite?(completion: FilesystemCompletion, onSettled: () => void): void;
|
|
104
119
|
tryRead?(handle: number, chunks: readonly Uint8Array[], offset: number): number;
|
|
105
120
|
tryWrite?(handle: number, chunks: readonly Uint8Array[], offset: number): number;
|
|
106
121
|
tryAppend?(
|
|
@@ -108,6 +123,7 @@ interface NonblockingFileSystem extends FileSystem {
|
|
|
108
123
|
chunks: readonly Uint8Array[],
|
|
109
124
|
): { written: number; offset: number } | number;
|
|
110
125
|
ioReadiness?(handle: number, writing: boolean): Readiness;
|
|
126
|
+
ioPending?(handle: number, writing: boolean): boolean;
|
|
111
127
|
readiness?(request: number): { ready: boolean } | undefined;
|
|
112
128
|
}
|
|
113
129
|
interface NonblockingStream extends Stream {
|
|
@@ -204,9 +220,16 @@ function sameWord(first: Int32Array | undefined, second: Int32Array): boolean {
|
|
|
204
220
|
);
|
|
205
221
|
}
|
|
206
222
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
223
|
+
function validWakeWord(word: Int32Array | undefined): Int32Array | undefined {
|
|
224
|
+
return word && isSharedArrayBuffer(typedArrayBuffer(word)) && typedArrayLength(word) === 1
|
|
225
|
+
? word
|
|
226
|
+
: undefined;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Guest adapter for the runtime_proc WASM namespace. Only poll, blocking wait_job, and
|
|
230
|
+
* bounded regular-file RPCs on direct-disabled backends wait. FileSystem supplies immediate
|
|
231
|
+
* nonblocking I/O whenever direct shared-memory access is enabled; streams with potentially
|
|
232
|
+
* blocking read/write must expose tryRead/tryWrite. */
|
|
210
233
|
export class ProcessABI {
|
|
211
234
|
readonly imports: Record<string, (...args: number[]) => number>;
|
|
212
235
|
private readonly now: () => number;
|
|
@@ -568,6 +591,86 @@ export class ProcessABI {
|
|
|
568
591
|
throw new ProcessError(NOTCAPABLE, "descriptor rights denied");
|
|
569
592
|
return entry;
|
|
570
593
|
}
|
|
594
|
+
private settleDeferredWriteLease(
|
|
595
|
+
fs: NonblockingFileSystem,
|
|
596
|
+
completion: FilesystemCompletion,
|
|
597
|
+
lease: AsyncFileOffsetLease,
|
|
598
|
+
append: boolean,
|
|
599
|
+
): void {
|
|
600
|
+
const finish = () => {
|
|
601
|
+
try {
|
|
602
|
+
const committed = completion.tryConsumeCommitted();
|
|
603
|
+
if (committed !== undefined) {
|
|
604
|
+
if (append) lease.commit((committed as { written: number; offset: number }).offset);
|
|
605
|
+
else if (typeof committed === "number" && committed > 0)
|
|
606
|
+
lease.commit(lease.position + committed);
|
|
607
|
+
}
|
|
608
|
+
} catch {
|
|
609
|
+
// Aborted or failed write: leave shared offset unchanged before releasing the lease.
|
|
610
|
+
} finally {
|
|
611
|
+
completion.release();
|
|
612
|
+
lease.release();
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
if (fs.trackDeferredWrite) fs.trackDeferredWrite(completion, finish);
|
|
616
|
+
else completion.onBackendSettled(finish);
|
|
617
|
+
}
|
|
618
|
+
private directDisabledFileIO(
|
|
619
|
+
write: boolean,
|
|
620
|
+
resource: Extract<Descriptor["resource"], { kind: "file" }>,
|
|
621
|
+
bytes: Uint8Array,
|
|
622
|
+
fs: NonblockingFileSystem,
|
|
623
|
+
): number {
|
|
624
|
+
this.checkFatal();
|
|
625
|
+
if (this.pending()) return -ProcessErrno.AGAIN;
|
|
626
|
+
const claim = tryClaimFileOffset(resource, true);
|
|
627
|
+
if (claim?.kind === "failed") {
|
|
628
|
+
this.checkFatal();
|
|
629
|
+
return -claim.errno;
|
|
630
|
+
}
|
|
631
|
+
if (claim?.kind === "busy") return -ProcessErrno.AGAIN;
|
|
632
|
+
const lease = claim?.kind === "acquired" ? claim.lease : undefined;
|
|
633
|
+
let releaseLease = true;
|
|
634
|
+
let outcome: number;
|
|
635
|
+
try {
|
|
636
|
+
const append = write && (lease ? lease.append : resource.append);
|
|
637
|
+
const position = lease ? lease.position : resource.offset;
|
|
638
|
+
const op = append ? "append" : write ? "write" : "read";
|
|
639
|
+
const commitOffset = (result: { written: number; offset: number } | number): number => {
|
|
640
|
+
if (typeof result !== "number") {
|
|
641
|
+
if (lease) lease.commit(result.offset);
|
|
642
|
+
else resource.offset = result.offset;
|
|
643
|
+
return result.written;
|
|
644
|
+
}
|
|
645
|
+
if (result > 0) {
|
|
646
|
+
if (lease) lease.commit(position + result);
|
|
647
|
+
else resource.offset = position + result;
|
|
648
|
+
}
|
|
649
|
+
return result;
|
|
650
|
+
};
|
|
651
|
+
const result = fs.directDisabledIO!(op, resource.handle, bytes, position, {
|
|
652
|
+
checkFatal: () => this.checkFatal(),
|
|
653
|
+
pending: () => this.pending(),
|
|
654
|
+
wait: (ms, word, expected) => {
|
|
655
|
+
assertTerminalWaitAllowed();
|
|
656
|
+
if (this.options.wait) this.options.wait(ms, word, expected);
|
|
657
|
+
else controlWait(word, 0, expected, ms);
|
|
658
|
+
},
|
|
659
|
+
commitWrite: commitOffset,
|
|
660
|
+
deferWrite: lease
|
|
661
|
+
? (completion) => {
|
|
662
|
+
releaseLease = false;
|
|
663
|
+
this.settleDeferredWriteLease(fs, completion, lease, append);
|
|
664
|
+
}
|
|
665
|
+
: undefined,
|
|
666
|
+
});
|
|
667
|
+
outcome = commitOffset(result);
|
|
668
|
+
} finally {
|
|
669
|
+
if (lease && releaseLease) lease.release();
|
|
670
|
+
}
|
|
671
|
+
this.checkFatal();
|
|
672
|
+
return outcome;
|
|
673
|
+
}
|
|
571
674
|
private io(write: boolean, fd: number, ptr: number, length: number): number {
|
|
572
675
|
const entry = this.descriptor(fd, write ? WRITE : READ),
|
|
573
676
|
resource = entry.resource;
|
|
@@ -576,6 +679,8 @@ export class ProcessABI {
|
|
|
576
679
|
if (typedArrayLength(bytes) === 0) return 0;
|
|
577
680
|
if (resource.kind === "file") {
|
|
578
681
|
const fs: NonblockingFileSystem = this.options.fs;
|
|
682
|
+
if (fs.directDisabled && fs.directDisabledIO)
|
|
683
|
+
return this.directDisabledFileIO(write, resource, bytes, fs);
|
|
579
684
|
return withFileOffset(
|
|
580
685
|
resource,
|
|
581
686
|
() => {
|
|
@@ -950,6 +1055,44 @@ export class ProcessABI {
|
|
|
950
1055
|
}
|
|
951
1056
|
return fullyNotified;
|
|
952
1057
|
}
|
|
1058
|
+
private pollFilesystemWait(sub: Subscription): "pending" | "file" | "none" {
|
|
1059
|
+
if (sub.kind === 3) return this.options.service.filesystemReady ? "none" : "pending";
|
|
1060
|
+
if (sub.kind !== 0 && sub.kind !== 1) return "none";
|
|
1061
|
+
const resource = this.descriptor(sub.id, POLL).resource;
|
|
1062
|
+
if (resource.kind !== "file") return "none";
|
|
1063
|
+
const fs: NonblockingFileSystem = this.options.fs;
|
|
1064
|
+
return fs.ioPending?.(resource.handle, sub.kind === 1) ? "pending" : "file";
|
|
1065
|
+
}
|
|
1066
|
+
private scanPollSubscriptions(
|
|
1067
|
+
subscriptions: readonly Subscription[],
|
|
1068
|
+
timeout: number,
|
|
1069
|
+
hasFsSignal: boolean,
|
|
1070
|
+
): {
|
|
1071
|
+
ready: { sub: Subscription; flags: number }[];
|
|
1072
|
+
waitOnFs: boolean;
|
|
1073
|
+
sawFile: boolean;
|
|
1074
|
+
allFs: boolean;
|
|
1075
|
+
} {
|
|
1076
|
+
const ready: { sub: Subscription; flags: number }[] = [];
|
|
1077
|
+
let waitOnFs = false;
|
|
1078
|
+
let sawFile = false;
|
|
1079
|
+
let allFs = subscriptions.length > 0;
|
|
1080
|
+
for (const sub of subscriptions) {
|
|
1081
|
+
const state = this.readiness(sub, timeout);
|
|
1082
|
+
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup)) {
|
|
1083
|
+
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
if (!hasFsSignal) continue;
|
|
1087
|
+
const fsWait = this.pollFilesystemWait(sub);
|
|
1088
|
+
if (fsWait === "none") allFs = false;
|
|
1089
|
+
else if (fsWait === "pending") {
|
|
1090
|
+
waitOnFs = true;
|
|
1091
|
+
sawFile = true;
|
|
1092
|
+
} else sawFile = true;
|
|
1093
|
+
}
|
|
1094
|
+
return { ready, waitOnFs, sawFile, allFs };
|
|
1095
|
+
}
|
|
953
1096
|
private poll(ptr: number, count: number, timeout: number, out: number): number {
|
|
954
1097
|
count >>>= 0;
|
|
955
1098
|
if (!Number.isInteger(timeout) || timeout < -1) return -ProcessErrno.INVAL;
|
|
@@ -970,6 +1113,7 @@ export class ProcessABI {
|
|
|
970
1113
|
if (this.pending()) return 0;
|
|
971
1114
|
const unregister: (() => void)[] = [];
|
|
972
1115
|
const wake = this.options.wake;
|
|
1116
|
+
const fsWord = validWakeWord((this.options.fs as NonblockingFileSystem).signal);
|
|
973
1117
|
try {
|
|
974
1118
|
const fullyNotified = this.registerPollWakes(subscriptions, wake, unregister);
|
|
975
1119
|
const deadline = timeout === -1 ? Infinity : this.now() + timeout;
|
|
@@ -977,13 +1121,13 @@ export class ProcessABI {
|
|
|
977
1121
|
// Observe sequence before state; publication racing scan makes Atomics.wait return immediately.
|
|
978
1122
|
const word = wake ?? this.options.signals.interrupt;
|
|
979
1123
|
const sequence = controlLoad(word, 0);
|
|
1124
|
+
const fsSequence = fsWord ? controlLoad(fsWord, 0) : 0;
|
|
980
1125
|
if (this.pending()) return 0;
|
|
981
|
-
const
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
}
|
|
1126
|
+
const { ready, waitOnFs, sawFile, allFs } = this.scanPollSubscriptions(
|
|
1127
|
+
subscriptions,
|
|
1128
|
+
timeout,
|
|
1129
|
+
fsWord !== undefined,
|
|
1130
|
+
);
|
|
987
1131
|
if (ready.length) {
|
|
988
1132
|
// Waiting/host services can grow memory; never retain a DataView across them.
|
|
989
1133
|
const output = this.view(out, ready.length * 16);
|
|
@@ -998,13 +1142,16 @@ export class ProcessABI {
|
|
|
998
1142
|
if (this.pending()) return 0;
|
|
999
1143
|
const remaining = deadline - this.now();
|
|
1000
1144
|
if (remaining <= 0) return 0;
|
|
1145
|
+
if (allFs && sawFile && fsWord && controlLoad(fsWord, 0) !== fsSequence) continue;
|
|
1001
1146
|
// Independent signals, early child startup, filesystem and legacy streams need a bounded
|
|
1002
1147
|
// rescan. A fully notified wait set can park until its actual deadline.
|
|
1148
|
+
const useFsWake = allFs && waitOnFs && fsWord !== undefined;
|
|
1149
|
+
const waitWord = useFsWake ? fsWord : word;
|
|
1150
|
+
const expected = useFsWake ? fsSequence : sequence;
|
|
1003
1151
|
const milliseconds = fullyNotified ? remaining : Math.min(remaining, 10);
|
|
1004
|
-
const expected = sequence;
|
|
1005
1152
|
assertTerminalWaitAllowed();
|
|
1006
|
-
if (this.options.wait) this.options.wait(milliseconds,
|
|
1007
|
-
else controlWait(
|
|
1153
|
+
if (this.options.wait) this.options.wait(milliseconds, waitWord, expected);
|
|
1154
|
+
else controlWait(waitWord, 0, expected, milliseconds);
|
|
1008
1155
|
}
|
|
1009
1156
|
} finally {
|
|
1010
1157
|
for (let index = unregister.length - 1; index >= 0; index--) unregister[index]!();
|