@waffle-os/runtime 0.0.147 → 0.0.149
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 +26 -5
- package/browser/fs-worker.ts +5 -2
- package/browser/process-broker.ts +6 -3
- package/browser/run-command.ts +18 -7
- package/browser/shared-fs.ts +93 -10
- package/core/filesystem.ts +2 -0
- package/dist/browser/filesystem-backend.js +19 -2
- package/dist/browser/fs-worker.js +7 -2
- package/dist/browser/process-broker.js +3 -2
- package/dist/browser/run-command.js +8 -2
- package/dist/browser/shared-fs.js +77 -8
- package/dist/fs/client.js +82 -21
- package/dist/fs/directory-pages.js +22 -7
- package/dist/fs/guest-watch.js +9 -3
- package/dist/fs/idle-database.js +1 -1
- package/dist/fs/idle-owner.js +35 -4
- package/dist/fs/indexeddb.js +322 -63
- package/dist/fs/memory.js +18 -9
- package/dist/fs/multitab-backend.js +5065 -0
- package/dist/fs/multitab-protocol.js +301 -0
- package/dist/fs/opfs.js +21 -0
- package/dist/fs/persistent.js +70 -1
- package/dist/fs/protocol.js +1 -1
- package/dist/fs/service.js +266 -86
- package/dist/fs/shared.js +45 -0
- package/dist/fs/vfs.js +26 -8
- package/dist/fs/watch-ring.js +7 -3
- package/dist/proc/command-stdio.js +56 -1
- package/dist/proc/registry.js +8 -8
- package/dist/proc/start-command.js +10 -0
- package/dist/proc/supervisor.js +4 -4
- package/dist/runtime.js +4 -2
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +36 -0
- package/fs/client.ts +81 -19
- package/fs/directory-pages.ts +40 -6
- package/fs/guest-watch.ts +8 -3
- package/fs/idle-database.ts +1 -0
- package/fs/idle-owner.ts +48 -12
- package/fs/indexeddb.ts +396 -78
- package/fs/memory.ts +33 -9
- package/fs/multitab-backend.ts +6870 -0
- package/fs/multitab-protocol.ts +655 -0
- package/fs/opfs.ts +21 -0
- package/fs/persistent.ts +70 -1
- package/fs/protocol.ts +1 -1
- package/fs/service.ts +314 -109
- package/fs/shared.ts +45 -0
- package/fs/storage.ts +3 -0
- package/fs/vfs.ts +44 -15
- package/fs/watch-ring.ts +5 -2
- package/package.json +1 -1
- package/proc/command-stdio.ts +72 -2
- package/proc/registry.ts +13 -19
- package/proc/start-command.ts +29 -0
- package/proc/supervisor.ts +8 -6
- package/runtime.ts +12 -3
- package/tooling/program/program-transformer.json +1 -1
package/fs/service.ts
CHANGED
|
@@ -119,9 +119,13 @@ interface SessionStage {
|
|
|
119
119
|
}
|
|
120
120
|
/** Largest request, in encoded bytes, whose decoded form stays with it while it is queued. */
|
|
121
121
|
const RETAINED_DECODE_BYTES = 512;
|
|
122
|
+
interface WatchResponseSettlement {
|
|
123
|
+
readonly cancel: () => void;
|
|
124
|
+
readonly release: (() => void) | undefined;
|
|
125
|
+
}
|
|
122
126
|
interface Session {
|
|
123
127
|
watchTransactions: Map<number, WatchTransaction>;
|
|
124
|
-
watchResponses: Map<number,
|
|
128
|
+
watchResponses: Map<number, WatchResponseSettlement>;
|
|
125
129
|
walks?: WalkTable;
|
|
126
130
|
walkTransactions: Map<number, WalkTransaction>;
|
|
127
131
|
walkResponses: Map<number, () => void>;
|
|
@@ -186,7 +190,7 @@ interface ShutdownOwner {
|
|
|
186
190
|
interface PreparedRequest {
|
|
187
191
|
handles: number[];
|
|
188
192
|
failure?: { error: unknown };
|
|
189
|
-
read?: { value: unknown };
|
|
193
|
+
read?: { value: unknown; startEpoch?: number; startVersion?: string };
|
|
190
194
|
}
|
|
191
195
|
/** One serial owner of the tree. No request ever waits for a guest acknowledgment. */
|
|
192
196
|
export class FilesystemService {
|
|
@@ -223,6 +227,7 @@ export class FilesystemService {
|
|
|
223
227
|
if (sessionRevoked(source)) throw new FilesystemError(11);
|
|
224
228
|
if (!source.handles.has(directory)) throw new FilesystemError(76);
|
|
225
229
|
this.check(source, directory);
|
|
230
|
+
if (this.backend.supportsSockets === false) throw new FilesystemError(58);
|
|
226
231
|
if (
|
|
227
232
|
!(this.backend instanceof MemoryBackend) ||
|
|
228
233
|
this.backend.fs.stat(directory).kind !== "directory"
|
|
@@ -263,6 +268,8 @@ export class FilesystemService {
|
|
|
263
268
|
connection.publication?.buffers,
|
|
264
269
|
);
|
|
265
270
|
this.backend.shared.onContention = () => this.sweep();
|
|
271
|
+
} else {
|
|
272
|
+
this.backend.attachShared?.(connection.shared);
|
|
266
273
|
}
|
|
267
274
|
if (this.requestBudget) assertRequestBudget(this.requestBudget, connection.requestBudget);
|
|
268
275
|
else this.requestBudget = connection.requestBudget;
|
|
@@ -550,6 +557,9 @@ export class FilesystemService {
|
|
|
550
557
|
);
|
|
551
558
|
return done;
|
|
552
559
|
}
|
|
560
|
+
private trackRequestWork<T>(session: Session, done: Promise<T>): Promise<T> {
|
|
561
|
+
return this.backend.asyncErrorsAreClientResults ? done : this.trackWork(session, done);
|
|
562
|
+
}
|
|
553
563
|
private enqueueMaintenance(session: Session, run: () => void): void {
|
|
554
564
|
const done = this.trackWork(session, this.queue.then(run));
|
|
555
565
|
this.queue = done.then(
|
|
@@ -675,7 +685,15 @@ export class FilesystemService {
|
|
|
675
685
|
const watch = session.watchResponses.get(id);
|
|
676
686
|
session.watchResponses.delete(id);
|
|
677
687
|
const request = session.retainedRequests.get(id);
|
|
678
|
-
if (
|
|
688
|
+
if (watch) {
|
|
689
|
+
if (
|
|
690
|
+
sessionRevoked(session) ||
|
|
691
|
+
!this.sessions.has(session) ||
|
|
692
|
+
(request && requestCancelled(request))
|
|
693
|
+
)
|
|
694
|
+
watch.cancel();
|
|
695
|
+
else watch.release?.();
|
|
696
|
+
}
|
|
679
697
|
this.releaseResponseMemory(session, id);
|
|
680
698
|
const retained = session.retainedRequests.get(id);
|
|
681
699
|
const cancelledWalk = retained && requestCancelled(retained);
|
|
@@ -742,13 +760,15 @@ export class FilesystemService {
|
|
|
742
760
|
let idle = 0,
|
|
743
761
|
detached = false;
|
|
744
762
|
const shared = this.backend instanceof MemoryBackend ? this.backend.shared : undefined;
|
|
763
|
+
const isUnlinked = (handle: number) =>
|
|
764
|
+
shared ? shared.isUnlinked(handle) : (this.backend.isUnlinked?.(handle) ?? false);
|
|
745
765
|
const responseHeld = this.responseReferenceCounts(session);
|
|
746
766
|
for (const handle of session.transient.keys()) {
|
|
747
767
|
const references = Atomics.load(session.capabilities, session.handles.get(handle)! + 1);
|
|
748
768
|
if (session.preparingHandles.has(handle) || references !== (responseHeld.get(handle) ?? 0))
|
|
749
769
|
continue;
|
|
750
770
|
idle++;
|
|
751
|
-
if (references === 0 &&
|
|
771
|
+
if (references === 0 && isUnlinked(handle)) detached = true;
|
|
752
772
|
}
|
|
753
773
|
if (idle <= TRANSIENT_HANDLES && !detached) return;
|
|
754
774
|
// Over the bound, evict the least recently used down to half of it. Each eviction bumps the
|
|
@@ -766,7 +786,7 @@ export class FilesystemService {
|
|
|
766
786
|
try {
|
|
767
787
|
for (const handle of session.transient.keys()) {
|
|
768
788
|
if (session.preparingHandles.has(handle)) continue;
|
|
769
|
-
if (idle <= keep && !
|
|
789
|
+
if (idle <= keep && !isUnlinked(handle)) continue;
|
|
770
790
|
const key = session.handles.get(handle)!;
|
|
771
791
|
// A local retain racing this CAS either owns a live pin or sees a revoked entry.
|
|
772
792
|
if (Atomics.compareExchange(session.capabilities, key + 1, 0, -1) !== 0) continue;
|
|
@@ -847,11 +867,10 @@ export class FilesystemService {
|
|
|
847
867
|
].includes(operation)
|
|
848
868
|
)
|
|
849
869
|
throw new FilesystemError(76);
|
|
850
|
-
if (
|
|
851
|
-
|
|
852
|
-
!scope
|
|
853
|
-
|
|
854
|
-
throw new FilesystemError(76);
|
|
870
|
+
if (["bindSocket", "bindSocketPath", "unbindSocket", "connectSocket"].includes(operation)) {
|
|
871
|
+
if (this.backend.supportsSockets === false) throw new FilesystemError(58);
|
|
872
|
+
if (!scope) throw new FilesystemError(76);
|
|
873
|
+
}
|
|
855
874
|
if (
|
|
856
875
|
scope &&
|
|
857
876
|
(operation === "resolve" ||
|
|
@@ -950,20 +969,33 @@ export class FilesystemService {
|
|
|
950
969
|
args: any[],
|
|
951
970
|
): unknown {
|
|
952
971
|
const [a, b] = args;
|
|
972
|
+
const request = session.pending.get(requestId);
|
|
973
|
+
const cancelled = () =>
|
|
974
|
+
sessionRevoked(session) ||
|
|
975
|
+
!this.sessions.has(session) ||
|
|
976
|
+
!request ||
|
|
977
|
+
requestCancelled(request);
|
|
953
978
|
if (operation === "watchOpen") {
|
|
979
|
+
if (this.backend.watchOpen) {
|
|
980
|
+
const register = (id: number) => {
|
|
981
|
+
if (cancelled()) {
|
|
982
|
+
this.backend.watchClose?.(session.connection.owner, id);
|
|
983
|
+
throw new FilesystemError(11);
|
|
984
|
+
}
|
|
985
|
+
session.watchTransactions.set(requestId, {
|
|
986
|
+
cancel: () => this.backend.watchClose?.(session.connection.owner, id),
|
|
987
|
+
release: undefined,
|
|
988
|
+
});
|
|
989
|
+
return id;
|
|
990
|
+
};
|
|
991
|
+
const opening = this.backend.watchOpen(session.connection.owner, a, b, cancelled);
|
|
992
|
+
return opening instanceof Promise
|
|
993
|
+
? this.trackRequestWork(session, opening).then(register)
|
|
994
|
+
: register(opening);
|
|
995
|
+
}
|
|
954
996
|
if (!(this.backend instanceof MemoryBackend)) throw new FilesystemError(58);
|
|
955
997
|
this.watchService ??= new WatchService(this.backend);
|
|
956
|
-
const
|
|
957
|
-
const id = this.watchService.open(
|
|
958
|
-
session.connection.owner,
|
|
959
|
-
a,
|
|
960
|
-
b,
|
|
961
|
-
() =>
|
|
962
|
-
sessionRevoked(session) ||
|
|
963
|
-
!this.sessions.has(session) ||
|
|
964
|
-
!request ||
|
|
965
|
-
requestCancelled(request),
|
|
966
|
-
);
|
|
998
|
+
const id = this.watchService.open(session.connection.owner, a, b, cancelled);
|
|
967
999
|
session.watchTransactions.set(requestId, {
|
|
968
1000
|
cancel: () => this.watchService?.close(session.connection.owner, id),
|
|
969
1001
|
release: undefined,
|
|
@@ -971,6 +1003,25 @@ export class FilesystemService {
|
|
|
971
1003
|
return id;
|
|
972
1004
|
}
|
|
973
1005
|
if (operation === "watchRead") {
|
|
1006
|
+
if (this.backend.watchRead) {
|
|
1007
|
+
const register = (held: import("./watch-subscriptions.js").HeldWatchBatch) => {
|
|
1008
|
+
if (cancelled()) {
|
|
1009
|
+
held.rollback();
|
|
1010
|
+
throw new FilesystemError(11);
|
|
1011
|
+
}
|
|
1012
|
+
if (held.batch.events.length)
|
|
1013
|
+
session.watchTransactions.set(requestId, {
|
|
1014
|
+
cancel: held.rollback,
|
|
1015
|
+
release: held.release,
|
|
1016
|
+
});
|
|
1017
|
+
else held.release();
|
|
1018
|
+
return held.batch;
|
|
1019
|
+
};
|
|
1020
|
+
const reading = this.backend.watchRead(session.connection.owner, a, cancelled);
|
|
1021
|
+
return reading instanceof Promise
|
|
1022
|
+
? this.trackRequestWork(session, reading).then(register)
|
|
1023
|
+
: register(reading);
|
|
1024
|
+
}
|
|
974
1025
|
if (!this.watchService) throw new FilesystemError(8);
|
|
975
1026
|
const held = this.watchService.readHeld(session.connection.owner, a);
|
|
976
1027
|
if (held.batch.events.length)
|
|
@@ -979,6 +1030,10 @@ export class FilesystemService {
|
|
|
979
1030
|
return held.batch;
|
|
980
1031
|
}
|
|
981
1032
|
if (operation === "watchClose") {
|
|
1033
|
+
if (this.backend.watchClose) {
|
|
1034
|
+
this.backend.watchClose(session.connection.owner, a);
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
982
1037
|
this.watchService?.close(session.connection.owner, a);
|
|
983
1038
|
return;
|
|
984
1039
|
}
|
|
@@ -1038,6 +1093,23 @@ export class FilesystemService {
|
|
|
1038
1093
|
session.walkResponses.set(id, transaction.cancel);
|
|
1039
1094
|
} else transaction.rollback();
|
|
1040
1095
|
}
|
|
1096
|
+
private takeResponseChunk(session: Session, a: number, b: number, c: number): Uint8Array {
|
|
1097
|
+
const response = session.responses.get(a);
|
|
1098
|
+
const bytes =
|
|
1099
|
+
response?.kind === "bytes"
|
|
1100
|
+
? response.bytes
|
|
1101
|
+
: response?.payload.kind === "overflow"
|
|
1102
|
+
? response.payload.bytes
|
|
1103
|
+
: undefined;
|
|
1104
|
+
if (!bytes) throw new FilesystemError(11);
|
|
1105
|
+
const part = bytes.slice(b, b + c);
|
|
1106
|
+
if (b + c < bytes.length) return part;
|
|
1107
|
+
this.releaseResponseMemory(session, a);
|
|
1108
|
+
if (response?.kind === "resolve") response.payload = { kind: "inline" };
|
|
1109
|
+
else if (session.watchResponses.has(a)) session.responses.delete(a);
|
|
1110
|
+
else this.discardResponse(session, a);
|
|
1111
|
+
return part;
|
|
1112
|
+
}
|
|
1041
1113
|
private execute(
|
|
1042
1114
|
session: Session,
|
|
1043
1115
|
id: number,
|
|
@@ -1050,22 +1122,7 @@ export class FilesystemService {
|
|
|
1050
1122
|
if (operation.startsWith("walk")) return this.walkRequest(session, id, operation, args);
|
|
1051
1123
|
if (operation.startsWith("watch")) return this.watchRequest(session, id, operation, args);
|
|
1052
1124
|
if (operation === "stageWrite") return this.stageWrite(session, id, a, b, c, data);
|
|
1053
|
-
if (operation === "take")
|
|
1054
|
-
const response = session.responses.get(a);
|
|
1055
|
-
const bytes =
|
|
1056
|
-
response?.kind === "bytes"
|
|
1057
|
-
? response.bytes
|
|
1058
|
-
: response?.payload.kind === "overflow"
|
|
1059
|
-
? response.payload.bytes
|
|
1060
|
-
: undefined;
|
|
1061
|
-
if (!bytes) throw new FilesystemError(11);
|
|
1062
|
-
const part = bytes.slice(b, b + c);
|
|
1063
|
-
if (b + c < bytes.length) return part;
|
|
1064
|
-
this.releaseResponseMemory(session, a);
|
|
1065
|
-
if (response?.kind === "resolve") response.payload = { kind: "inline" };
|
|
1066
|
-
else this.discardResponse(session, a);
|
|
1067
|
-
return part;
|
|
1068
|
-
}
|
|
1125
|
+
if (operation === "take") return this.takeResponseChunk(session, a, b, c);
|
|
1069
1126
|
if (operation === "retain") {
|
|
1070
1127
|
const key = session.handles.get(a)!;
|
|
1071
1128
|
for (;;) {
|
|
@@ -1107,7 +1164,7 @@ export class FilesystemService {
|
|
|
1107
1164
|
const flushing = this.backend.flush?.(
|
|
1108
1165
|
operation === "sync" ? a : typeof a === "string" ? a : undefined,
|
|
1109
1166
|
);
|
|
1110
|
-
return this.
|
|
1167
|
+
return this.trackRequestWork(session, Promise.resolve(flushing)).then(() =>
|
|
1111
1168
|
this.finishResult(session, id, operation, operation === "sync" ? 0 : undefined),
|
|
1112
1169
|
);
|
|
1113
1170
|
}
|
|
@@ -1117,7 +1174,7 @@ export class FilesystemService {
|
|
|
1117
1174
|
}
|
|
1118
1175
|
const result = this.backend.execute(operation, args, data);
|
|
1119
1176
|
if (result instanceof Promise)
|
|
1120
|
-
return this.
|
|
1177
|
+
return this.trackRequestWork(session, result).then((value) =>
|
|
1121
1178
|
this.finishResult(session, id, operation, value, args),
|
|
1122
1179
|
);
|
|
1123
1180
|
return this.finishResult(session, id, operation, result, args);
|
|
@@ -1129,25 +1186,29 @@ export class FilesystemService {
|
|
|
1129
1186
|
result: any,
|
|
1130
1187
|
args: any[] = [],
|
|
1131
1188
|
): unknown {
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1189
|
+
try {
|
|
1190
|
+
this.finishSocketOperation(session, operation, result, args);
|
|
1191
|
+
const pending = session.pending.get(id);
|
|
1192
|
+
if (
|
|
1193
|
+
sessionRevoked(session) ||
|
|
1194
|
+
!this.sessions.has(session) ||
|
|
1195
|
+
!pending ||
|
|
1196
|
+
requestCancelled(pending)
|
|
1197
|
+
)
|
|
1198
|
+
throw new FilesystemError(11);
|
|
1199
|
+
if (operation === "preopen" || (operation === "create" && typeof result === "number"))
|
|
1200
|
+
this.pin(session, result, operation === "preopen");
|
|
1201
|
+
// Touch every returned identity before choosing an eviction victim: a new parent may point
|
|
1202
|
+
// to a hard-linked node that was previously the oldest transient capability.
|
|
1203
|
+
if (operation === "resolve" && typeof result !== "number")
|
|
1204
|
+
this.holdResolvedResponse(session, id, result);
|
|
1205
|
+
if (mutations.has(operation)) {
|
|
1206
|
+
for (const peer of this.sessions) if (!isRevoked(peer.connection)) this.trim(peer);
|
|
1207
|
+
} else this.trim(session);
|
|
1208
|
+
return result;
|
|
1209
|
+
} finally {
|
|
1210
|
+
this.backend.settleResult?.(operation, result);
|
|
1211
|
+
}
|
|
1151
1212
|
}
|
|
1152
1213
|
private checkSocketTree(session: Session, operation: string, args: any[]): void {
|
|
1153
1214
|
if (
|
|
@@ -1187,6 +1248,35 @@ export class FilesystemService {
|
|
|
1187
1248
|
this.backend.release(owned.binding.node);
|
|
1188
1249
|
this.backend.release(owned.binding.parent);
|
|
1189
1250
|
}
|
|
1251
|
+
private preparedReadInvalidated(
|
|
1252
|
+
prepared: PreparedRequest | undefined,
|
|
1253
|
+
epoch: Int32Array,
|
|
1254
|
+
): boolean {
|
|
1255
|
+
const read = prepared?.read;
|
|
1256
|
+
if (!read) return false;
|
|
1257
|
+
if (read.startEpoch !== undefined && Atomics.load(epoch, 0) !== read.startEpoch) return true;
|
|
1258
|
+
return read.startVersion !== undefined && this.backend.readVersion?.() !== read.startVersion;
|
|
1259
|
+
}
|
|
1260
|
+
private dirtyRetryChange(error: unknown): number | undefined {
|
|
1261
|
+
if (
|
|
1262
|
+
!(error instanceof DirtyCapacityError) ||
|
|
1263
|
+
error.permanent ||
|
|
1264
|
+
!this.backend.waitForDirtyCapacity
|
|
1265
|
+
)
|
|
1266
|
+
return undefined;
|
|
1267
|
+
return (
|
|
1268
|
+
this.backend.dirtyChange?.() ??
|
|
1269
|
+
(this.backend instanceof MemoryBackend
|
|
1270
|
+
? this.backend.shared.dirtyAdmission.change
|
|
1271
|
+
: undefined)
|
|
1272
|
+
);
|
|
1273
|
+
}
|
|
1274
|
+
private cancelSessionWatchResponses(session: Session): void {
|
|
1275
|
+
for (const [id, watch] of session.watchResponses) {
|
|
1276
|
+
session.watchResponses.delete(id);
|
|
1277
|
+
watch.cancel();
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1190
1280
|
private assertBackendAccess(): void {
|
|
1191
1281
|
if (this.backend instanceof MemoryBackend) this.backend.shared.publication?.assertAccess();
|
|
1192
1282
|
}
|
|
@@ -1218,6 +1308,10 @@ export class FilesystemService {
|
|
|
1218
1308
|
admission = publicationAdmission(input.publication);
|
|
1219
1309
|
this.checkRequest(session, operation, args);
|
|
1220
1310
|
if (prepared?.failure) throw prepared.failure.error;
|
|
1311
|
+
if (this.preparedReadInvalidated(prepared, epoch)) {
|
|
1312
|
+
this.releasePrepared(session, prepared);
|
|
1313
|
+
prepared = undefined;
|
|
1314
|
+
}
|
|
1221
1315
|
this.assertBackendAccess();
|
|
1222
1316
|
const continuation = this.prepareRequest(
|
|
1223
1317
|
session,
|
|
@@ -1242,8 +1336,12 @@ export class FilesystemService {
|
|
|
1242
1336
|
else opened = this.beginEpochMutation(epoch);
|
|
1243
1337
|
}
|
|
1244
1338
|
const payload = bytes.subarray(inputLength, inputLength + dataLength);
|
|
1245
|
-
const
|
|
1246
|
-
|
|
1339
|
+
const preparedRead =
|
|
1340
|
+
prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid())
|
|
1341
|
+
? prepared.read
|
|
1342
|
+
: undefined;
|
|
1343
|
+
const value = preparedRead
|
|
1344
|
+
? this.finishResult(session, id, operation, preparedRead.value)
|
|
1247
1345
|
: local && operation !== "flush" && operation !== "sync"
|
|
1248
1346
|
? this.executePublication(session, id, operation, args, payload, admission)
|
|
1249
1347
|
: await this.execute(session, id, operation, args, payload);
|
|
@@ -1252,13 +1350,9 @@ export class FilesystemService {
|
|
|
1252
1350
|
staging = operation === "stageWrite";
|
|
1253
1351
|
} catch (error) {
|
|
1254
1352
|
this.discardResponse(session, id);
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
this.backend.waitForDirtyCapacity &&
|
|
1259
|
-
this.backend instanceof MemoryBackend
|
|
1260
|
-
) {
|
|
1261
|
-
dirtyRetry = this.backend.shared.dirtyAdmission.change;
|
|
1353
|
+
const dirtyChange = this.dirtyRetryChange(error);
|
|
1354
|
+
if (dirtyChange !== undefined) {
|
|
1355
|
+
dirtyRetry = dirtyChange;
|
|
1262
1356
|
prepared ??= { handles: [] };
|
|
1263
1357
|
const input = unpack(bytes.subarray(0, cells[1]!));
|
|
1264
1358
|
this.checkRequest(session, input.operation, input.args, prepared.handles);
|
|
@@ -1324,8 +1418,10 @@ export class FilesystemService {
|
|
|
1324
1418
|
transaction.cancel();
|
|
1325
1419
|
return;
|
|
1326
1420
|
}
|
|
1327
|
-
session.watchResponses.set(id,
|
|
1328
|
-
|
|
1421
|
+
session.watchResponses.set(id, {
|
|
1422
|
+
cancel: transaction.cancel,
|
|
1423
|
+
release: transaction.release,
|
|
1424
|
+
});
|
|
1329
1425
|
}
|
|
1330
1426
|
private retainRequestReply(session: Session, id: number, buffer: FilesystemRequestBuffer): void {
|
|
1331
1427
|
if (
|
|
@@ -1441,10 +1537,12 @@ export class FilesystemService {
|
|
|
1441
1537
|
args: unknown[],
|
|
1442
1538
|
read: Promise<Uint8Array>,
|
|
1443
1539
|
): PreparedRequest {
|
|
1540
|
+
const startEpoch = Atomics.load(session.epoch, 0);
|
|
1541
|
+
const startVersion = this.backend.readVersion?.();
|
|
1444
1542
|
const continuation: PreparedRequest = { handles: [] };
|
|
1445
1543
|
this.checkRequest(session, operation, args, continuation.handles);
|
|
1446
1544
|
const capture = read.then((value) => {
|
|
1447
|
-
continuation.read = { value };
|
|
1545
|
+
continuation.read = { value, startEpoch, startVersion };
|
|
1448
1546
|
});
|
|
1449
1547
|
this.joinPreparation(session, id, buffer, continuation, capture);
|
|
1450
1548
|
return continuation;
|
|
@@ -1778,8 +1876,38 @@ export class FilesystemService {
|
|
|
1778
1876
|
);
|
|
1779
1877
|
}
|
|
1780
1878
|
}
|
|
1879
|
+
private clearSessionHandlesAndBuffers(session: Session): void {
|
|
1880
|
+
for (const key of session.handles.values()) Atomics.store(session.capabilities, key + 1, -1);
|
|
1881
|
+
for (const handle of Array.from(session.handles.keys())) {
|
|
1882
|
+
if (Atomics.load(session.capabilities, session.handles.get(handle)! + 2) > 0) continue;
|
|
1883
|
+
this.backend.release(handle);
|
|
1884
|
+
session.handles.delete(handle);
|
|
1885
|
+
}
|
|
1886
|
+
Atomics.store(session.capabilities, 0, 0);
|
|
1887
|
+
for (const lease of session.socketScope?.leases.values() ?? []) {
|
|
1888
|
+
this.backend.release(lease.binding.node);
|
|
1889
|
+
this.backend.release(lease.binding.parent);
|
|
1890
|
+
}
|
|
1891
|
+
session.socketScope?.leases.clear();
|
|
1892
|
+
session.preopens.clear();
|
|
1893
|
+
session.transient.clear();
|
|
1894
|
+
session.references.clear();
|
|
1895
|
+
for (const retained of session.retainedRequests.values())
|
|
1896
|
+
releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
1897
|
+
session.retainedRequests.clear();
|
|
1898
|
+
for (const release of session.responseMemory.values()) release();
|
|
1899
|
+
session.responseMemory.clear();
|
|
1900
|
+
session.responses.clear();
|
|
1901
|
+
session.preparingHandles.clear();
|
|
1902
|
+
if (this.backend instanceof MemoryBackend)
|
|
1903
|
+
for (const id of session.publicationPins ?? [])
|
|
1904
|
+
this.backend.shared.publication?.releasePin(id);
|
|
1905
|
+
session.publicationPins?.clear();
|
|
1906
|
+
}
|
|
1781
1907
|
private releaseSessionPins(session: Session): void {
|
|
1908
|
+
this.cancelSessionWatchResponses(session);
|
|
1782
1909
|
this.watchService?.closeOwner(session.connection.owner);
|
|
1910
|
+
this.backend.closeWatchOwner?.(session.connection.owner);
|
|
1783
1911
|
session.namespace?.close();
|
|
1784
1912
|
this.stopSessionReader(session);
|
|
1785
1913
|
if (!session.transportFailed) revokeFilesystem(session.connection);
|
|
@@ -1829,33 +1957,9 @@ export class FilesystemService {
|
|
|
1829
1957
|
session.walks?.dispose();
|
|
1830
1958
|
session.walkResponses.clear();
|
|
1831
1959
|
for (const id of session.watchTransactions.keys()) this.settleWatch(session, id, false);
|
|
1960
|
+
this.cancelSessionWatchResponses(session);
|
|
1832
1961
|
session.watchResponses.clear();
|
|
1833
|
-
|
|
1834
|
-
for (const handle of Array.from(session.handles.keys())) {
|
|
1835
|
-
if (Atomics.load(session.capabilities, session.handles.get(handle)! + 2) > 0) continue;
|
|
1836
|
-
this.backend.release(handle);
|
|
1837
|
-
session.handles.delete(handle);
|
|
1838
|
-
}
|
|
1839
|
-
Atomics.store(session.capabilities, 0, 0);
|
|
1840
|
-
for (const lease of session.socketScope?.leases.values() ?? []) {
|
|
1841
|
-
this.backend.release(lease.binding.node);
|
|
1842
|
-
this.backend.release(lease.binding.parent);
|
|
1843
|
-
}
|
|
1844
|
-
session.socketScope?.leases.clear();
|
|
1845
|
-
session.preopens.clear();
|
|
1846
|
-
session.transient.clear();
|
|
1847
|
-
session.references.clear();
|
|
1848
|
-
for (const retained of session.retainedRequests.values())
|
|
1849
|
-
releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
1850
|
-
session.retainedRequests.clear();
|
|
1851
|
-
for (const release of session.responseMemory.values()) release();
|
|
1852
|
-
session.responseMemory.clear();
|
|
1853
|
-
session.responses.clear();
|
|
1854
|
-
session.preparingHandles.clear();
|
|
1855
|
-
if (this.backend instanceof MemoryBackend)
|
|
1856
|
-
for (const id of session.publicationPins ?? [])
|
|
1857
|
-
this.backend.shared.publication?.releasePin(id);
|
|
1858
|
-
session.publicationPins?.clear();
|
|
1962
|
+
this.clearSessionHandlesAndBuffers(session);
|
|
1859
1963
|
session.connection.port.close();
|
|
1860
1964
|
session.closed = true;
|
|
1861
1965
|
this.retireTransport(session);
|
|
@@ -1977,6 +2081,9 @@ export class FilesystemService {
|
|
|
1977
2081
|
if (this.disposingWatches && !this.sessions.size && this.backend instanceof MemoryBackend)
|
|
1978
2082
|
this.backend.watchAccount.close();
|
|
1979
2083
|
}
|
|
2084
|
+
beginDispose(): void {
|
|
2085
|
+
this.backend.beginDispose?.();
|
|
2086
|
+
}
|
|
1980
2087
|
dispose(): void {
|
|
1981
2088
|
if (this.shutdownOwner) return;
|
|
1982
2089
|
this.disposingWatches = true;
|
|
@@ -2003,6 +2110,7 @@ export class FilesystemService {
|
|
|
2003
2110
|
const owner = { done, sessions };
|
|
2004
2111
|
this.shutdownOwner = owner;
|
|
2005
2112
|
this.ownerStopping = true;
|
|
2113
|
+
this.backend.beginShutdown?.();
|
|
2006
2114
|
for (const session of sessions) this.stopSessionReader(session);
|
|
2007
2115
|
const fences = sessions.map((session) => this.shutdownFence(session));
|
|
2008
2116
|
void this.drainShutdown(owner, fences).then(resolve, reject);
|
|
@@ -2136,7 +2244,14 @@ function failureDiagnostic(error: unknown) {
|
|
|
2136
2244
|
vfs: diagnosticField(error, "vfs"),
|
|
2137
2245
|
};
|
|
2138
2246
|
if (detail.name === "VfsError") message = diagnosticField(error, "message") ?? message;
|
|
2139
|
-
|
|
2247
|
+
const released = diagnosticReleased(error);
|
|
2248
|
+
return {
|
|
2249
|
+
type: "filesystem-error",
|
|
2250
|
+
message,
|
|
2251
|
+
stack,
|
|
2252
|
+
...detail,
|
|
2253
|
+
...(released !== undefined ? { released } : {}),
|
|
2254
|
+
};
|
|
2140
2255
|
}
|
|
2141
2256
|
function diagnosticField(error: unknown, key: string): string | undefined {
|
|
2142
2257
|
try {
|
|
@@ -2148,6 +2263,26 @@ function diagnosticField(error: unknown, key: string): string | undefined {
|
|
|
2148
2263
|
return;
|
|
2149
2264
|
}
|
|
2150
2265
|
}
|
|
2266
|
+
function diagnosticReleased(error: unknown): boolean | undefined {
|
|
2267
|
+
try {
|
|
2268
|
+
if (!error || typeof error !== "object") return undefined;
|
|
2269
|
+
const value: unknown = Reflect.get(error, "released");
|
|
2270
|
+
return typeof value === "boolean" ? value : undefined;
|
|
2271
|
+
} catch {
|
|
2272
|
+
return undefined;
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
function isCancellationError(error: unknown): boolean {
|
|
2276
|
+
if (error instanceof FilesystemError && error.errno === 11) return true;
|
|
2277
|
+
const name = diagnosticField(error, "name");
|
|
2278
|
+
return name === "AbortError";
|
|
2279
|
+
}
|
|
2280
|
+
|
|
2281
|
+
export interface FilesystemWorkerCallbacks {
|
|
2282
|
+
readonly onState?: (status: "waiting" | "ready") => void;
|
|
2283
|
+
readonly onOwnershipLost?: () => void;
|
|
2284
|
+
readonly onFatalError?: (error: unknown, released: boolean) => void;
|
|
2285
|
+
}
|
|
2151
2286
|
|
|
2152
2287
|
async function prepareAcceptance(binding: OperationBinding) {
|
|
2153
2288
|
try {
|
|
@@ -2176,32 +2311,89 @@ export function installFilesystemWorker(
|
|
|
2176
2311
|
onStartupStage?: RuntimeStartupObserver,
|
|
2177
2312
|
signal?: AbortSignal,
|
|
2178
2313
|
onWaiting?: () => void,
|
|
2314
|
+
callbacks?: FilesystemWorkerCallbacks,
|
|
2179
2315
|
) => FilesystemBackend | Promise<FilesystemBackend> = (options) =>
|
|
2180
2316
|
new MemoryBackend(undefined, undefined, undefined, undefined, options.connect),
|
|
2181
2317
|
): void {
|
|
2182
2318
|
// Selection belongs to the controller, never to guest filesystem requests.
|
|
2183
2319
|
let service: Promise<FilesystemService> | undefined;
|
|
2320
|
+
let resolvedService: FilesystemService | undefined;
|
|
2184
2321
|
let errorReported = false;
|
|
2185
2322
|
let onStartupStage: RuntimeStartupObserver | undefined;
|
|
2186
2323
|
const startupAbort = new AbortController();
|
|
2187
2324
|
let disposing = false;
|
|
2325
|
+
let beginDisposeRequested = false;
|
|
2326
|
+
const callbacks: FilesystemWorkerCallbacks = {
|
|
2327
|
+
onState(status) {
|
|
2328
|
+
if (!disposing) scope.postMessage({ type: "filesystem-state", status });
|
|
2329
|
+
},
|
|
2330
|
+
onOwnershipLost() {
|
|
2331
|
+
scope.postMessage({ type: "filesystem-ownership-lost" });
|
|
2332
|
+
},
|
|
2333
|
+
onFatalError(error, released) {
|
|
2334
|
+
if (disposing || errorReported) return;
|
|
2335
|
+
errorReported = true;
|
|
2336
|
+
try {
|
|
2337
|
+
scope.postMessage({ ...failureDiagnostic(error), released });
|
|
2338
|
+
} catch {
|
|
2339
|
+
/* Diagnostic delivery failure is non-fatal when retaining locks. */
|
|
2340
|
+
}
|
|
2341
|
+
},
|
|
2342
|
+
};
|
|
2188
2343
|
scope.onmessage = ({ data }) => {
|
|
2344
|
+
if (data?.beginDispose === true) {
|
|
2345
|
+
beginDisposeRequested = true;
|
|
2346
|
+
if (resolvedService) resolvedService.beginDispose();
|
|
2347
|
+
else
|
|
2348
|
+
void service?.then(
|
|
2349
|
+
(owner) => owner.beginDispose(),
|
|
2350
|
+
() => {},
|
|
2351
|
+
);
|
|
2352
|
+
return;
|
|
2353
|
+
}
|
|
2189
2354
|
if (data?.dispose === true) {
|
|
2190
2355
|
if (disposing) return;
|
|
2191
2356
|
disposing = true;
|
|
2192
2357
|
startupAbort.abort();
|
|
2193
2358
|
void (service ?? Promise.resolve(undefined))
|
|
2194
2359
|
.then(
|
|
2195
|
-
(owner) =>
|
|
2196
|
-
|
|
2360
|
+
(owner) =>
|
|
2361
|
+
owner
|
|
2362
|
+
? owner
|
|
2363
|
+
.shutdown()
|
|
2364
|
+
.then(() => ({ released: true as boolean, error: undefined as unknown }))
|
|
2365
|
+
: { released: true as boolean, error: undefined as unknown },
|
|
2366
|
+
(error) => {
|
|
2367
|
+
const released = diagnosticReleased(error);
|
|
2368
|
+
if (released === false) return { released: false, error };
|
|
2369
|
+
if (released === true) {
|
|
2370
|
+
return isCancellationError(error)
|
|
2371
|
+
? { released: true, error: undefined }
|
|
2372
|
+
: { released: true, error };
|
|
2373
|
+
}
|
|
2374
|
+
return { released: true, error: undefined };
|
|
2375
|
+
},
|
|
2197
2376
|
)
|
|
2198
2377
|
.then(
|
|
2199
|
-
(
|
|
2200
|
-
|
|
2378
|
+
({ released, error }) => {
|
|
2379
|
+
if (error !== undefined) {
|
|
2380
|
+
scope.postMessage({
|
|
2381
|
+
type: "filesystem-disposed",
|
|
2382
|
+
error: failureDiagnostic(error).message,
|
|
2383
|
+
released,
|
|
2384
|
+
});
|
|
2385
|
+
} else {
|
|
2386
|
+
scope.postMessage({ type: "filesystem-disposed", released });
|
|
2387
|
+
}
|
|
2388
|
+
},
|
|
2389
|
+
(error) => {
|
|
2390
|
+
const released = diagnosticReleased(error);
|
|
2201
2391
|
scope.postMessage({
|
|
2202
2392
|
type: "filesystem-disposed",
|
|
2203
2393
|
error: failureDiagnostic(error).message,
|
|
2204
|
-
|
|
2394
|
+
...(released !== undefined ? { released } : {}),
|
|
2395
|
+
});
|
|
2396
|
+
},
|
|
2205
2397
|
);
|
|
2206
2398
|
return;
|
|
2207
2399
|
}
|
|
@@ -2216,11 +2408,22 @@ export function installFilesystemWorker(
|
|
|
2216
2408
|
scope.postMessage({ type: "filesystem-startup", stage, timestamp });
|
|
2217
2409
|
reportRuntimeStartup(onStartupStage, "filesystem-connected");
|
|
2218
2410
|
}
|
|
2219
|
-
return backendFactory(
|
|
2220
|
-
|
|
2221
|
-
|
|
2411
|
+
return backendFactory(
|
|
2412
|
+
backendOptions(data),
|
|
2413
|
+
onStartupStage,
|
|
2414
|
+
startupAbort.signal,
|
|
2415
|
+
() => {
|
|
2416
|
+
if (!disposing) scope.postMessage({ type: "filesystem-state", status: "waiting" });
|
|
2417
|
+
},
|
|
2418
|
+
callbacks,
|
|
2419
|
+
);
|
|
2222
2420
|
})
|
|
2223
|
-
.then((backend) =>
|
|
2421
|
+
.then((backend) => {
|
|
2422
|
+
const owner = new FilesystemService(backend);
|
|
2423
|
+
resolvedService = owner;
|
|
2424
|
+
if (beginDisposeRequested) owner.beginDispose();
|
|
2425
|
+
return owner;
|
|
2426
|
+
});
|
|
2224
2427
|
void service
|
|
2225
2428
|
.then(async (owner) => {
|
|
2226
2429
|
if (disposing) {
|
|
@@ -2254,9 +2457,11 @@ export function installFilesystemWorker(
|
|
|
2254
2457
|
/* The worker error below also covers a failed diagnostic transfer. */
|
|
2255
2458
|
} finally {
|
|
2256
2459
|
// A rejected async message handler alone does not dispatch a worker error event.
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2460
|
+
if (diagnosticReleased(error) !== false) {
|
|
2461
|
+
setTimeout(() => {
|
|
2462
|
+
throw error;
|
|
2463
|
+
}, 0);
|
|
2464
|
+
}
|
|
2260
2465
|
}
|
|
2261
2466
|
});
|
|
2262
2467
|
};
|