@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/dist/fs/service.js
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
retainResponseBytes,
|
|
8
8
|
captureRequestMemory,
|
|
9
9
|
closeRequestMemory,
|
|
10
|
+
requestMemoryRetired,
|
|
10
11
|
retireRequestMemory,
|
|
11
12
|
requestTransportError,
|
|
12
13
|
requestLengths,
|
|
@@ -79,6 +80,9 @@ const mutations = new Set([
|
|
|
79
80
|
]);
|
|
80
81
|
let connectObserved;
|
|
81
82
|
const RETAINED_DECODE_BYTES = 512;
|
|
83
|
+
function isCommittedScalarWrite(operation, state) {
|
|
84
|
+
return (operation === "write" || operation === "append") && state === State.Complete;
|
|
85
|
+
}
|
|
82
86
|
function sessionRevoked(session) {
|
|
83
87
|
return isRevoked(session.connection) || !!requestTransportError(session.requests);
|
|
84
88
|
}
|
|
@@ -195,6 +199,7 @@ export class FilesystemService {
|
|
|
195
199
|
stages: new Map,
|
|
196
200
|
retainedRequests: new Map,
|
|
197
201
|
pending: new Map,
|
|
202
|
+
inFlightWrites: new Set,
|
|
198
203
|
privateInputs: new Map,
|
|
199
204
|
preparingHandles: new Map,
|
|
200
205
|
preparations: new Set,
|
|
@@ -984,7 +989,8 @@ export class FilesystemService {
|
|
|
984
989
|
try {
|
|
985
990
|
this.finishSocketOperation(session, operation, result, args);
|
|
986
991
|
const pending = session.pending.get(id);
|
|
987
|
-
|
|
992
|
+
const scalarWrite = operation === "write" || operation === "append";
|
|
993
|
+
if (!pending || !scalarWrite && (sessionRevoked(session) || !this.sessions.has(session) || requestCancelled(pending)))
|
|
988
994
|
throw new FilesystemError(11);
|
|
989
995
|
if (operation === "preopen" || operation === "create" && typeof result === "number")
|
|
990
996
|
this.pin(session, result, operation === "preopen");
|
|
@@ -1062,6 +1068,7 @@ export class FilesystemService {
|
|
|
1062
1068
|
let opened = false;
|
|
1063
1069
|
let preparing = false;
|
|
1064
1070
|
let staging = false;
|
|
1071
|
+
let committedWrite = false;
|
|
1065
1072
|
let admission;
|
|
1066
1073
|
let dirtyRetry;
|
|
1067
1074
|
const epoch = session.epoch;
|
|
@@ -1098,10 +1105,13 @@ export class FilesystemService {
|
|
|
1098
1105
|
}
|
|
1099
1106
|
const payload = bytes.subarray(inputLength, inputLength + dataLength);
|
|
1100
1107
|
const preparedRead = prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid()) ? prepared.read : undefined;
|
|
1108
|
+
if (operation === "write" || operation === "append")
|
|
1109
|
+
session.inFlightWrites.add(id);
|
|
1101
1110
|
const value = preparedRead ? this.finishResult(session, id, operation, preparedRead.value) : local && operation !== "flush" && operation !== "sync" ? this.executePublication(session, id, operation, args, payload, admission) : await this.execute(session, id, operation, args, payload);
|
|
1102
1111
|
this.announceSegments(this.backend.takeSharedSegments?.() ?? [], this.sessions);
|
|
1103
1112
|
state = this.writeResponse(session, id, cells, response, value, bytes);
|
|
1104
1113
|
staging = operation === "stageWrite";
|
|
1114
|
+
committedWrite = isCommittedScalarWrite(operation, state);
|
|
1105
1115
|
} catch (error) {
|
|
1106
1116
|
this.discardResponse(session, id);
|
|
1107
1117
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
@@ -1126,22 +1136,20 @@ export class FilesystemService {
|
|
|
1126
1136
|
this.cancelPublication(session, admission);
|
|
1127
1137
|
this.releasePrepared(session, prepared);
|
|
1128
1138
|
cells[5] = Atomics.load(epoch, 0);
|
|
1129
|
-
|
|
1130
|
-
this.discardResponse(session, id);
|
|
1131
|
-
state = State.Cancelled;
|
|
1132
|
-
}
|
|
1133
|
-
if (staging && !session.stages.has(id))
|
|
1134
|
-
state = State.Cancelled;
|
|
1139
|
+
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1135
1140
|
this.settleWalk(session, id, state === State.Complete);
|
|
1136
1141
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1137
1142
|
Atomics.store(cells, 0, state);
|
|
1138
1143
|
Atomics.notify(cells, 0);
|
|
1139
1144
|
wake(session.connection);
|
|
1145
|
+
session.inFlightWrites.delete(id);
|
|
1140
1146
|
session.pending.delete(id);
|
|
1141
1147
|
session.privateInputs.delete(id);
|
|
1142
1148
|
this.retainRequestReply(session, id, buffer);
|
|
1143
1149
|
session.connection.port.postMessage({ completed: id });
|
|
1144
1150
|
this.sweep();
|
|
1151
|
+
} else {
|
|
1152
|
+
session.inFlightWrites.delete(id);
|
|
1145
1153
|
}
|
|
1146
1154
|
}
|
|
1147
1155
|
if (dirtyRetry !== undefined && prepared) {
|
|
@@ -1149,6 +1157,16 @@ export class FilesystemService {
|
|
|
1149
1157
|
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1150
1158
|
}
|
|
1151
1159
|
}
|
|
1160
|
+
settleCancelledState(session, id, buffer, state, staging, committedWrite) {
|
|
1161
|
+
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
1162
|
+
this.discardResponse(session, id);
|
|
1163
|
+
if (!committedWrite)
|
|
1164
|
+
state = State.Cancelled;
|
|
1165
|
+
}
|
|
1166
|
+
if (staging && !session.stages.has(id))
|
|
1167
|
+
return State.Cancelled;
|
|
1168
|
+
return state;
|
|
1169
|
+
}
|
|
1152
1170
|
holdResponseMemory(session, id, release) {
|
|
1153
1171
|
const prior = session.responseMemory.get(id);
|
|
1154
1172
|
session.responseMemory.set(id, () => {
|
|
@@ -1524,6 +1542,16 @@ export class FilesystemService {
|
|
|
1524
1542
|
this.backend.shared.publication?.releasePin(id);
|
|
1525
1543
|
session.publicationPins?.clear();
|
|
1526
1544
|
}
|
|
1545
|
+
cancelPendingRequests(session) {
|
|
1546
|
+
for (const [id, buffer] of session.pending) {
|
|
1547
|
+
const cells = requestCells(buffer);
|
|
1548
|
+
Atomics.store(cells, 6, 1);
|
|
1549
|
+
Atomics.store(cells, 0, State.Cancelled);
|
|
1550
|
+
Atomics.notify(cells, 0);
|
|
1551
|
+
session.connection.port.postMessage({ completed: id });
|
|
1552
|
+
}
|
|
1553
|
+
session.pending.clear();
|
|
1554
|
+
}
|
|
1527
1555
|
releaseSessionPins(session) {
|
|
1528
1556
|
this.cancelSessionWatchResponses(session);
|
|
1529
1557
|
this.watchService?.closeOwner(session.connection.owner);
|
|
@@ -1549,18 +1577,13 @@ export class FilesystemService {
|
|
|
1549
1577
|
}));
|
|
1550
1578
|
return;
|
|
1551
1579
|
}
|
|
1580
|
+
if (session.inFlightWrites.size)
|
|
1581
|
+
return;
|
|
1552
1582
|
if (ownRevocation === 2)
|
|
1553
1583
|
this.backend.revokeOwner?.(session.connection.owner, session.epoch);
|
|
1554
1584
|
for (const id of Array.from(session.stages.keys()))
|
|
1555
1585
|
this.settleStage(session, id);
|
|
1556
|
-
|
|
1557
|
-
const cells = requestCells(buffer);
|
|
1558
|
-
Atomics.store(cells, 6, 1);
|
|
1559
|
-
Atomics.store(cells, 0, State.Cancelled);
|
|
1560
|
-
Atomics.notify(cells, 0);
|
|
1561
|
-
session.connection.port.postMessage({ completed: id });
|
|
1562
|
-
}
|
|
1563
|
-
session.pending.clear();
|
|
1586
|
+
this.cancelPendingRequests(session);
|
|
1564
1587
|
wake(session.connection);
|
|
1565
1588
|
for (const id of session.walkTransactions.keys())
|
|
1566
1589
|
this.settleWalk(session, id, false);
|
|
@@ -1706,7 +1729,45 @@ export class FilesystemService {
|
|
|
1706
1729
|
}
|
|
1707
1730
|
async shutdownFence(session) {
|
|
1708
1731
|
revokeFilesystemService(session.connection);
|
|
1709
|
-
|
|
1732
|
+
const quiesced = waitForFilesystemQuiescence(session.connection);
|
|
1733
|
+
let done = false;
|
|
1734
|
+
quiesced.finally(() => {
|
|
1735
|
+
done = true;
|
|
1736
|
+
wake(session.connection);
|
|
1737
|
+
});
|
|
1738
|
+
const signal = new Int32Array(session.connection.signal);
|
|
1739
|
+
while (!done) {
|
|
1740
|
+
const observed = Atomics.load(signal, 0);
|
|
1741
|
+
if (requestTransportError(session.requests)) {
|
|
1742
|
+
session.transportFailed = true;
|
|
1743
|
+
await this.drainFailedSessionRequests(session);
|
|
1744
|
+
break;
|
|
1745
|
+
}
|
|
1746
|
+
const waiting = atomicsWaitAsync(signal, 0, observed);
|
|
1747
|
+
if (waiting.async)
|
|
1748
|
+
await waiting.value;
|
|
1749
|
+
}
|
|
1750
|
+
await quiesced;
|
|
1751
|
+
}
|
|
1752
|
+
async drainFailedSessionRequests(session) {
|
|
1753
|
+
await waitForRequestQuiescence(session.connection, session.requests);
|
|
1754
|
+
for (;; ) {
|
|
1755
|
+
const queue = this.queue;
|
|
1756
|
+
await queue;
|
|
1757
|
+
const pending = Array.from(this.runningRequests).filter((done) => this.work.get(done) === session);
|
|
1758
|
+
if (pending.length)
|
|
1759
|
+
await Promise.allSettled(pending);
|
|
1760
|
+
if (session.preparations.size)
|
|
1761
|
+
await Promise.allSettled(Array.from(session.preparations));
|
|
1762
|
+
const stillRunning = Array.from(this.runningRequests).some((done) => this.work.get(done) === session);
|
|
1763
|
+
if (queue === this.queue && !stillRunning && !session.preparations.size && !session.inFlightWrites.size)
|
|
1764
|
+
break;
|
|
1765
|
+
}
|
|
1766
|
+
this.cancelPendingRequests(session);
|
|
1767
|
+
const failure = requestTransportError(session.requests);
|
|
1768
|
+
if (failure?.reason !== "stale" && !requestMemoryRetired(session.requests))
|
|
1769
|
+
retireRequestMemory(session.requests);
|
|
1770
|
+
wake(session.connection);
|
|
1710
1771
|
}
|
|
1711
1772
|
async drainShutdown(owner, fences) {
|
|
1712
1773
|
const failures = [];
|
package/dist/fs/shared.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
readIdentityWords,
|
|
12
12
|
writeIdentityWords
|
|
13
13
|
} from "./file-identity.js";
|
|
14
|
-
import { typedArraySet } from "../proc/intrinsics.js";
|
|
14
|
+
import { Int32ArrayConstructor, atomicsLoad, typedArraySet } from "../proc/intrinsics.js";
|
|
15
15
|
import {
|
|
16
16
|
PublicationDomain
|
|
17
17
|
} from "./publication.js";
|
|
@@ -213,10 +213,10 @@ export class SharedFilesystemState {
|
|
|
213
213
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
214
214
|
}
|
|
215
215
|
isDirectDisabled() {
|
|
216
|
-
return (
|
|
216
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
217
217
|
}
|
|
218
218
|
isClientCacheDisabled() {
|
|
219
|
-
return (
|
|
219
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_CLIENT_CACHE) !== 0;
|
|
220
220
|
}
|
|
221
221
|
resetForRemount(retainedSegments = []) {
|
|
222
222
|
Atomics.store(this.cells, GLOBAL_LOCK, 0);
|
|
@@ -1978,12 +1978,12 @@ export class SharedFilesystemState {
|
|
|
1978
1978
|
}
|
|
1979
1979
|
}
|
|
1980
1980
|
export function sharedRevoked(connection) {
|
|
1981
|
-
const words = [new
|
|
1981
|
+
const words = [new Int32ArrayConstructor(connection.ownerStates, 0, 1)].concat(connection.revocations.map((buffer) => new Int32ArrayConstructor(buffer)));
|
|
1982
1982
|
if (connection.publication)
|
|
1983
|
-
words.push(new
|
|
1983
|
+
words.push(new Int32ArrayConstructor(connection.publication.buffers.control, 0, 1));
|
|
1984
1984
|
return () => {
|
|
1985
1985
|
for (let i = 0;i < words.length; i++)
|
|
1986
|
-
if (
|
|
1986
|
+
if (atomicsLoad(words[i], 0) !== 0)
|
|
1987
1987
|
return true;
|
|
1988
1988
|
return false;
|
|
1989
1989
|
};
|
package/dist/proc/abi.js
CHANGED
|
@@ -29,7 +29,10 @@ import { controlLoad, controlWait } from "./control.js";
|
|
|
29
29
|
import {
|
|
30
30
|
MAX_DESCRIPTOR
|
|
31
31
|
} from "../core/descriptors.js";
|
|
32
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
tryClaimFileOffset,
|
|
34
|
+
withFileOffset
|
|
35
|
+
} from "../core/open-file.js";
|
|
33
36
|
import { PipeController } from "./pipes.js";
|
|
34
37
|
import { readOutcome } from "./outcome.js";
|
|
35
38
|
import { WasiExit } from "../core/wasi.js";
|
|
@@ -94,6 +97,9 @@ function releasePollWakes(unregister) {
|
|
|
94
97
|
function sameWord(first, second) {
|
|
95
98
|
return first !== undefined && typedArrayBuffer(first) === typedArrayBuffer(second) && typedArrayByteOffset(first) === typedArrayByteOffset(second) && typedArrayLength(first) === 1;
|
|
96
99
|
}
|
|
100
|
+
function validWakeWord(word) {
|
|
101
|
+
return word && isSharedArrayBuffer(typedArrayBuffer(word)) && typedArrayLength(word) === 1 ? word : undefined;
|
|
102
|
+
}
|
|
97
103
|
|
|
98
104
|
export class ProcessABI {
|
|
99
105
|
options;
|
|
@@ -387,6 +393,84 @@ export class ProcessABI {
|
|
|
387
393
|
throw new ProcessError(NOTCAPABLE, "descriptor rights denied");
|
|
388
394
|
return entry;
|
|
389
395
|
}
|
|
396
|
+
settleDeferredWriteLease(fs, completion, lease, append) {
|
|
397
|
+
const finish = () => {
|
|
398
|
+
try {
|
|
399
|
+
const committed = completion.tryConsumeCommitted();
|
|
400
|
+
if (committed !== undefined) {
|
|
401
|
+
if (append)
|
|
402
|
+
lease.commit(committed.offset);
|
|
403
|
+
else if (typeof committed === "number" && committed > 0)
|
|
404
|
+
lease.commit(lease.position + committed);
|
|
405
|
+
}
|
|
406
|
+
} catch {} finally {
|
|
407
|
+
completion.release();
|
|
408
|
+
lease.release();
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
if (fs.trackDeferredWrite)
|
|
412
|
+
fs.trackDeferredWrite(completion, finish);
|
|
413
|
+
else
|
|
414
|
+
completion.onBackendSettled(finish);
|
|
415
|
+
}
|
|
416
|
+
directDisabledFileIO(write, resource, bytes, fs) {
|
|
417
|
+
this.checkFatal();
|
|
418
|
+
if (this.pending())
|
|
419
|
+
return -ProcessErrno.AGAIN;
|
|
420
|
+
const claim = tryClaimFileOffset(resource, true);
|
|
421
|
+
if (claim?.kind === "failed") {
|
|
422
|
+
this.checkFatal();
|
|
423
|
+
return -claim.errno;
|
|
424
|
+
}
|
|
425
|
+
if (claim?.kind === "busy")
|
|
426
|
+
return -ProcessErrno.AGAIN;
|
|
427
|
+
const lease = claim?.kind === "acquired" ? claim.lease : undefined;
|
|
428
|
+
let releaseLease = true;
|
|
429
|
+
let outcome;
|
|
430
|
+
try {
|
|
431
|
+
const append = write && (lease ? lease.append : resource.append);
|
|
432
|
+
const position = lease ? lease.position : resource.offset;
|
|
433
|
+
const op = append ? "append" : write ? "write" : "read";
|
|
434
|
+
const commitOffset = (result) => {
|
|
435
|
+
if (typeof result !== "number") {
|
|
436
|
+
if (lease)
|
|
437
|
+
lease.commit(result.offset);
|
|
438
|
+
else
|
|
439
|
+
resource.offset = result.offset;
|
|
440
|
+
return result.written;
|
|
441
|
+
}
|
|
442
|
+
if (result > 0) {
|
|
443
|
+
if (lease)
|
|
444
|
+
lease.commit(position + result);
|
|
445
|
+
else
|
|
446
|
+
resource.offset = position + result;
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
const result = fs.directDisabledIO(op, resource.handle, bytes, position, {
|
|
451
|
+
checkFatal: () => this.checkFatal(),
|
|
452
|
+
pending: () => this.pending(),
|
|
453
|
+
wait: (ms, word, expected) => {
|
|
454
|
+
assertTerminalWaitAllowed();
|
|
455
|
+
if (this.options.wait)
|
|
456
|
+
this.options.wait(ms, word, expected);
|
|
457
|
+
else
|
|
458
|
+
controlWait(word, 0, expected, ms);
|
|
459
|
+
},
|
|
460
|
+
commitWrite: commitOffset,
|
|
461
|
+
deferWrite: lease ? (completion) => {
|
|
462
|
+
releaseLease = false;
|
|
463
|
+
this.settleDeferredWriteLease(fs, completion, lease, append);
|
|
464
|
+
} : undefined
|
|
465
|
+
});
|
|
466
|
+
outcome = commitOffset(result);
|
|
467
|
+
} finally {
|
|
468
|
+
if (lease && releaseLease)
|
|
469
|
+
lease.release();
|
|
470
|
+
}
|
|
471
|
+
this.checkFatal();
|
|
472
|
+
return outcome;
|
|
473
|
+
}
|
|
390
474
|
io(write, fd, ptr, length) {
|
|
391
475
|
const entry = this.descriptor(fd, write ? WRITE : READ), resource = entry.resource;
|
|
392
476
|
if (resource.kind === "directory")
|
|
@@ -396,6 +480,8 @@ export class ProcessABI {
|
|
|
396
480
|
return 0;
|
|
397
481
|
if (resource.kind === "file") {
|
|
398
482
|
const fs = this.options.fs;
|
|
483
|
+
if (fs.directDisabled && fs.directDisabledIO)
|
|
484
|
+
return this.directDisabledFileIO(write, resource, bytes, fs);
|
|
399
485
|
return withFileOffset(resource, () => {
|
|
400
486
|
if (write && resource.append) {
|
|
401
487
|
const result = fs.tryAppend ? fs.tryAppend(resource.handle, [bytes]) : fs.append(resource.handle, [bytes]);
|
|
@@ -725,6 +811,41 @@ export class ProcessABI {
|
|
|
725
811
|
}
|
|
726
812
|
return fullyNotified;
|
|
727
813
|
}
|
|
814
|
+
pollFilesystemWait(sub) {
|
|
815
|
+
if (sub.kind === 3)
|
|
816
|
+
return this.options.service.filesystemReady ? "none" : "pending";
|
|
817
|
+
if (sub.kind !== 0 && sub.kind !== 1)
|
|
818
|
+
return "none";
|
|
819
|
+
const resource = this.descriptor(sub.id, POLL).resource;
|
|
820
|
+
if (resource.kind !== "file")
|
|
821
|
+
return "none";
|
|
822
|
+
const fs = this.options.fs;
|
|
823
|
+
return fs.ioPending?.(resource.handle, sub.kind === 1) ? "pending" : "file";
|
|
824
|
+
}
|
|
825
|
+
scanPollSubscriptions(subscriptions, timeout, hasFsSignal) {
|
|
826
|
+
const ready = [];
|
|
827
|
+
let waitOnFs = false;
|
|
828
|
+
let sawFile = false;
|
|
829
|
+
let allFs = subscriptions.length > 0;
|
|
830
|
+
for (const sub of subscriptions) {
|
|
831
|
+
const state = this.readiness(sub, timeout);
|
|
832
|
+
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup)) {
|
|
833
|
+
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
if (!hasFsSignal)
|
|
837
|
+
continue;
|
|
838
|
+
const fsWait = this.pollFilesystemWait(sub);
|
|
839
|
+
if (fsWait === "none")
|
|
840
|
+
allFs = false;
|
|
841
|
+
else if (fsWait === "pending") {
|
|
842
|
+
waitOnFs = true;
|
|
843
|
+
sawFile = true;
|
|
844
|
+
} else
|
|
845
|
+
sawFile = true;
|
|
846
|
+
}
|
|
847
|
+
return { ready, waitOnFs, sawFile, allFs };
|
|
848
|
+
}
|
|
728
849
|
poll(ptr, count, timeout, out) {
|
|
729
850
|
count >>>= 0;
|
|
730
851
|
if (!Number.isInteger(timeout) || timeout < -1)
|
|
@@ -749,20 +870,17 @@ export class ProcessABI {
|
|
|
749
870
|
return 0;
|
|
750
871
|
const unregister = [];
|
|
751
872
|
const wake = this.options.wake;
|
|
873
|
+
const fsWord = validWakeWord(this.options.fs.signal);
|
|
752
874
|
try {
|
|
753
875
|
const fullyNotified = this.registerPollWakes(subscriptions, wake, unregister);
|
|
754
876
|
const deadline = timeout === -1 ? 1 / 0 : this.now() + timeout;
|
|
755
877
|
for (;; ) {
|
|
756
878
|
const word = wake ?? this.options.signals.interrupt;
|
|
757
879
|
const sequence = controlLoad(word, 0);
|
|
880
|
+
const fsSequence = fsWord ? controlLoad(fsWord, 0) : 0;
|
|
758
881
|
if (this.pending())
|
|
759
882
|
return 0;
|
|
760
|
-
const ready =
|
|
761
|
-
for (const sub of subscriptions) {
|
|
762
|
-
const state = this.readiness(sub, timeout);
|
|
763
|
-
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup))
|
|
764
|
-
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
765
|
-
}
|
|
883
|
+
const { ready, waitOnFs, sawFile, allFs } = this.scanPollSubscriptions(subscriptions, timeout, fsWord !== undefined);
|
|
766
884
|
if (ready.length) {
|
|
767
885
|
const output = this.view(out, ready.length * 16);
|
|
768
886
|
ready.forEach(({ sub, flags }, index) => {
|
|
@@ -778,13 +896,17 @@ export class ProcessABI {
|
|
|
778
896
|
const remaining = deadline - this.now();
|
|
779
897
|
if (remaining <= 0)
|
|
780
898
|
return 0;
|
|
899
|
+
if (allFs && sawFile && fsWord && controlLoad(fsWord, 0) !== fsSequence)
|
|
900
|
+
continue;
|
|
901
|
+
const useFsWake = allFs && waitOnFs && fsWord !== undefined;
|
|
902
|
+
const waitWord = useFsWake ? fsWord : word;
|
|
903
|
+
const expected = useFsWake ? fsSequence : sequence;
|
|
781
904
|
const milliseconds = fullyNotified ? remaining : Math.min(remaining, 10);
|
|
782
|
-
const expected = sequence;
|
|
783
905
|
assertTerminalWaitAllowed();
|
|
784
906
|
if (this.options.wait)
|
|
785
|
-
this.options.wait(milliseconds,
|
|
907
|
+
this.options.wait(milliseconds, waitWord, expected);
|
|
786
908
|
else
|
|
787
|
-
controlWait(
|
|
909
|
+
controlWait(waitWord, 0, expected, milliseconds);
|
|
788
910
|
}
|
|
789
911
|
} finally {
|
|
790
912
|
for (let index = unregister.length - 1;index >= 0; index--)
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { atomicsWaitAsync } from "./intrinsics.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
claimFileOffset,
|
|
4
|
+
withFileOffset,
|
|
5
|
+
shareFileResource
|
|
6
|
+
} from "../core/open-file.js";
|
|
3
7
|
import { FilesystemClient } from "../fs/client.js";
|
|
4
8
|
import { errno } from "../fs/protocol.js";
|
|
5
9
|
import { CommandStdioError } from "./command.js";
|
|
@@ -107,30 +111,81 @@ export class CommandStdio {
|
|
|
107
111
|
await this.wait(words, observed);
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
const
|
|
113
|
-
lease.commit(
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
commitAsyncWrite(lease, result) {
|
|
115
|
+
if (lease.append) {
|
|
116
|
+
const appended = result;
|
|
117
|
+
lease.commit(appended.offset);
|
|
118
|
+
return appended.written;
|
|
119
|
+
}
|
|
120
|
+
const written = result;
|
|
121
|
+
if (written > 0)
|
|
122
|
+
lease.commit(lease.position + written);
|
|
123
|
+
return written;
|
|
124
|
+
}
|
|
125
|
+
deferAsyncWrite(filesystem, completion, lease) {
|
|
126
|
+
filesystem.trackDeferredWrite(completion, () => {
|
|
127
|
+
try {
|
|
128
|
+
const settled = completion.tryConsumeCommitted();
|
|
129
|
+
if (settled !== undefined)
|
|
130
|
+
this.commitAsyncWrite(lease, settled);
|
|
131
|
+
} catch {} finally {
|
|
132
|
+
completion.release();
|
|
133
|
+
lease.release();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async writeAsyncFileLease(filesystem, resource, lease, bytes, onDefer) {
|
|
138
|
+
const completion = lease.append ? filesystem.submit("append", [resource.handle], bytes) : filesystem.submit("write", [resource.handle, lease.position], bytes);
|
|
139
|
+
let handedOff = false;
|
|
140
|
+
try {
|
|
141
|
+
const result = await completion.value();
|
|
142
|
+
return this.commitAsyncWrite(lease, result);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
const committed = completion.tryConsumeCommitted();
|
|
145
|
+
if (committed !== undefined) {
|
|
146
|
+
this.commitAsyncWrite(lease, committed);
|
|
147
|
+
} else {
|
|
148
|
+
handedOff = true;
|
|
149
|
+
onDefer();
|
|
150
|
+
this.deferAsyncWrite(filesystem, completion, lease);
|
|
151
|
+
}
|
|
152
|
+
throw error;
|
|
153
|
+
} finally {
|
|
154
|
+
if (!handedOff)
|
|
155
|
+
completion.release();
|
|
116
156
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
157
|
+
}
|
|
158
|
+
async runLeasedTransfer(filesystem, resource, lease, bytes, writing) {
|
|
159
|
+
let releaseLease = true;
|
|
160
|
+
const defer = () => {
|
|
161
|
+
releaseLease = false;
|
|
162
|
+
};
|
|
163
|
+
try {
|
|
164
|
+
if (writing) {
|
|
165
|
+
const written = await this.writeAsyncFileLease(filesystem, resource, lease, bytes, defer);
|
|
166
|
+
lease.release();
|
|
167
|
+
releaseLease = false;
|
|
168
|
+
this.check();
|
|
169
|
+
return written;
|
|
170
|
+
}
|
|
171
|
+
const read = await filesystem.asyncCall("read", [
|
|
172
|
+
resource.handle,
|
|
173
|
+
bytes.length,
|
|
174
|
+
lease.position
|
|
175
|
+
]);
|
|
176
|
+
bytes.set(read, 0);
|
|
177
|
+
if (read.length > 0)
|
|
178
|
+
lease.commit(lease.position + read.length);
|
|
121
179
|
this.check();
|
|
122
|
-
return
|
|
180
|
+
return read.length;
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (error instanceof CommandStdioError)
|
|
183
|
+
throw error;
|
|
184
|
+
throw stdioError(-errno(error));
|
|
185
|
+
} finally {
|
|
186
|
+
if (releaseLease)
|
|
187
|
+
lease.release();
|
|
123
188
|
}
|
|
124
|
-
const read = await filesystem.asyncCall("read", [
|
|
125
|
-
resource.handle,
|
|
126
|
-
bytes.length,
|
|
127
|
-
lease.position
|
|
128
|
-
]);
|
|
129
|
-
bytes.set(read, 0);
|
|
130
|
-
if (read.length > 0)
|
|
131
|
-
lease.commit(lease.position + read.length);
|
|
132
|
-
this.check();
|
|
133
|
-
return read.length;
|
|
134
189
|
}
|
|
135
190
|
async transferAsyncFile(resource, bytes, writing) {
|
|
136
191
|
const filesystem = this.filesystem;
|
|
@@ -139,23 +194,14 @@ export class CommandStdio {
|
|
|
139
194
|
shareFileResource(resource, filesystem.connection);
|
|
140
195
|
for (;; ) {
|
|
141
196
|
this.check();
|
|
142
|
-
const claim = claimFileOffset(resource);
|
|
197
|
+
const claim = claimFileOffset(resource, writing);
|
|
143
198
|
if (claim.kind === "failed")
|
|
144
199
|
throw stdioError(-claim.errno);
|
|
145
200
|
if (claim.kind === "busy") {
|
|
146
201
|
await this.wait([claim.word], [claim.observed]);
|
|
147
202
|
continue;
|
|
148
203
|
}
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
return await this.runAsyncFileLease(filesystem, resource, lease, bytes, writing);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
if (error instanceof CommandStdioError)
|
|
154
|
-
throw error;
|
|
155
|
-
throw stdioError(-errno(error));
|
|
156
|
-
} finally {
|
|
157
|
-
lease.release();
|
|
158
|
-
}
|
|
204
|
+
return await this.runLeasedTransfer(filesystem, resource, claim.lease, bytes, writing);
|
|
159
205
|
}
|
|
160
206
|
}
|
|
161
207
|
notifications(resource) {
|
|
@@ -211,6 +257,8 @@ export class CommandStdio {
|
|
|
211
257
|
async settle() {
|
|
212
258
|
while (this.pending.size)
|
|
213
259
|
await Promise.allSettled(Array.from(this.pending));
|
|
260
|
+
if (this.filesystem?.hasDeferredWrites())
|
|
261
|
+
await this.filesystem.settleDeferredWrites();
|
|
214
262
|
await this.filesystem?.stopResidentPublication();
|
|
215
263
|
this.filesystem?.dispose();
|
|
216
264
|
}
|
package/dist/proc/registry.js
CHANGED
|
@@ -70,8 +70,8 @@ export class ExecutableRegistry {
|
|
|
70
70
|
async check(name, cwd = "/", assertActive = () => {}) {
|
|
71
71
|
return (await this.admit(name, cwd, assertActive, undefined, "access")).entry;
|
|
72
72
|
}
|
|
73
|
-
async resolve(name, cwd = "/", assertActive = () => {}, argv,
|
|
74
|
-
const admitted = await this.admit(name, cwd, assertActive, argv,
|
|
73
|
+
async resolve(name, cwd = "/", assertActive = () => {}, argv, commandPrograms) {
|
|
74
|
+
const admitted = await this.admit(name, cwd, assertActive, argv, commandPrograms ?? "execute");
|
|
75
75
|
const entry = admitted.entry;
|
|
76
76
|
assertActive();
|
|
77
77
|
if ("command" in entry)
|
|
@@ -175,8 +175,8 @@ export class ExecutableRegistry {
|
|
|
175
175
|
return this.admitScript(target.startsWith("/") ? target : `${dirname(file.path)}/${target}`, cwd, assertActive, argv, mode, depth, seen, symlinks + 1, scriptName);
|
|
176
176
|
}
|
|
177
177
|
const resolved = file.path === predicted ? registered : this.entries.get(file.path);
|
|
178
|
-
if (mode === "byte-backed
|
|
179
|
-
return this.
|
|
178
|
+
if (mode === "byte-backed" || mode === "guest")
|
|
179
|
+
return this.admitGuest(file.kind === "file" ? resolved : undefined, file.path, argv, mode);
|
|
180
180
|
const admitted = resolved && !(("command" in resolved) && resolved.command.marker && !await this.matchesMarker(absolute, resolved.command.marker));
|
|
181
181
|
assertActive();
|
|
182
182
|
if (admitted && resolved && file.kind === "file")
|
|
@@ -210,8 +210,8 @@ export class ExecutableRegistry {
|
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
async admitEntry(entry, path, cwd, assertActive, argv, mode, depth, seen, symlinks, scriptName) {
|
|
213
|
-
if (mode === "byte-backed
|
|
214
|
-
return this.
|
|
213
|
+
if (mode === "byte-backed" || mode === "guest")
|
|
214
|
+
return this.admitGuest(entry, path, argv, mode);
|
|
215
215
|
if (!entry)
|
|
216
216
|
throw new ExecutableNotFound(path);
|
|
217
217
|
if ("script" in entry)
|
|
@@ -220,8 +220,8 @@ export class ExecutableRegistry {
|
|
|
220
220
|
this.checkCommand(entry.command);
|
|
221
221
|
return { entry, ...argv ? { argv } : {} };
|
|
222
222
|
}
|
|
223
|
-
|
|
224
|
-
if (!entry || "script" in entry || "command" in entry || !entry.interruptible || entry.execution?.kind !== "guest")
|
|
223
|
+
admitGuest(entry, path, argv, programs) {
|
|
224
|
+
if (!entry || "script" in entry || "command" in entry || programs === "byte-backed" && !entry.interruptible || entry.execution?.kind !== "guest")
|
|
225
225
|
throw new ExecutableNotRunnable(path, false);
|
|
226
226
|
return { entry, ...argv ? { argv } : {} };
|
|
227
227
|
}
|
|
@@ -16,12 +16,20 @@ function output(value) {
|
|
|
16
16
|
throw new TypeError("Command output callback must be a function");
|
|
17
17
|
return { limitBytes, onChunk };
|
|
18
18
|
}
|
|
19
|
+
function policies({ programs = "byte-backed", onLeaderExit = "join" }) {
|
|
20
|
+
if (programs !== "byte-backed" && programs !== "guest")
|
|
21
|
+
throw new TypeError('Command programs must be "byte-backed" or "guest"');
|
|
22
|
+
if (onLeaderExit !== "join" && onLeaderExit !== "stop")
|
|
23
|
+
throw new TypeError('Command onLeaderExit must be "join" or "stop"');
|
|
24
|
+
return { programs, onLeaderExit };
|
|
25
|
+
}
|
|
19
26
|
function snapshot(executable, options) {
|
|
20
27
|
if (options?.filesystem !== "runtime-root")
|
|
21
28
|
throw new TypeError('Commands require filesystem: "runtime-root"');
|
|
22
29
|
if (!path(executable))
|
|
23
30
|
throw new TypeError("Command executable must be an absolute path");
|
|
24
31
|
const { args, cwd = "/", env, stdin, stdout, stderr, deadlineMs } = options;
|
|
32
|
+
const { programs, onLeaderExit } = policies(options);
|
|
25
33
|
if (!path(cwd))
|
|
26
34
|
throw new TypeError("Command cwd must be an absolute path");
|
|
27
35
|
if (args !== undefined && !Array.isArray(args))
|
|
@@ -42,6 +50,8 @@ function snapshot(executable, options) {
|
|
|
42
50
|
throw new RangeError("Command deadline must be between 1 and 2147483647 ms");
|
|
43
51
|
return {
|
|
44
52
|
filesystem: "runtime-root",
|
|
53
|
+
programs,
|
|
54
|
+
onLeaderExit,
|
|
45
55
|
args: argv,
|
|
46
56
|
cwd,
|
|
47
57
|
env: Object.fromEntries(entries),
|
package/dist/proc/supervisor.js
CHANGED
|
@@ -627,7 +627,7 @@ export class ProcessSupervisor {
|
|
|
627
627
|
const executable = await this.options.registry.resolve(image.request.executable, image.request.cwd, () => {
|
|
628
628
|
if (record.result || record.revoked || readOutcome(record.outcome))
|
|
629
629
|
throw new Error("process exited before executable admission completed");
|
|
630
|
-
}, image.request.argv, this.options.
|
|
630
|
+
}, image.request.argv, this.options.commandPrograms?.(record.rootPid));
|
|
631
631
|
if (record.result || record.revoked)
|
|
632
632
|
return;
|
|
633
633
|
const settled = readOutcome(record.outcome);
|
|
@@ -771,7 +771,7 @@ export class ProcessSupervisor {
|
|
|
771
771
|
};
|
|
772
772
|
try {
|
|
773
773
|
active();
|
|
774
|
-
const resolved = await this.options.registry.resolve(replacement.request.executable, replacement.request.cwd, active, replacement.request.argv, this.options.
|
|
774
|
+
const resolved = await this.options.registry.resolve(replacement.request.executable, replacement.request.cwd, active, replacement.request.argv, this.options.commandPrograms?.(record.rootPid));
|
|
775
775
|
active();
|
|
776
776
|
if (record.processKind === "thread")
|
|
777
777
|
throw new ProcessError(ProcessErrno.NOTSUP, "exec requires a sole process leader");
|
|
@@ -836,7 +836,7 @@ export class ProcessSupervisor {
|
|
|
836
836
|
return image.stopping;
|
|
837
837
|
const stop = async () => {
|
|
838
838
|
this.revokeImage(image);
|
|
839
|
-
if (this.options.
|
|
839
|
+
if (this.options.commandPrograms?.(record.rootPid))
|
|
840
840
|
image.descriptors.dispose();
|
|
841
841
|
else
|
|
842
842
|
this.disposeDescriptors(image.descriptors);
|
|
@@ -1259,7 +1259,7 @@ export class ProcessSupervisor {
|
|
|
1259
1259
|
this.safely(hook);
|
|
1260
1260
|
this.revokeImage(record.image);
|
|
1261
1261
|
this.disposeDescriptors(record.image.descriptors, (error) => {
|
|
1262
|
-
if (!this.options.
|
|
1262
|
+
if (!this.options.commandPrograms?.(record.rootPid)) {
|
|
1263
1263
|
this.report(error);
|
|
1264
1264
|
return;
|
|
1265
1265
|
}
|