@waffle-os/runtime 0.0.153 → 0.0.155

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.
Files changed (61) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +18 -0
  9. package/core/wasi.ts +75 -4
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +62 -4
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +75 -17
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/guest-watch.js +267 -94
  21. package/dist/fs/indexeddb-chunks.js +88 -11
  22. package/dist/fs/indexeddb.js +366 -110
  23. package/dist/fs/memory.js +106 -20
  24. package/dist/fs/multitab-backend.js +291 -74
  25. package/dist/fs/multitab-protocol.js +1 -0
  26. package/dist/fs/persistent.js +70 -16
  27. package/dist/fs/protocol.js +15 -7
  28. package/dist/fs/publication-attachment.js +9 -7
  29. package/dist/fs/service.js +62 -9
  30. package/dist/fs/shared.js +100 -6
  31. package/dist/fs/storage.js +107 -8
  32. package/dist/fs/watch-service.js +97 -2
  33. package/dist/fs/watch-subscriptions.js +121 -8
  34. package/dist/proc/command-stdio.js +1 -1
  35. package/dist/proc/intrinsics/core.js +5 -0
  36. package/dist/proc/intrinsics.js +1 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +17 -0
  39. package/fs/client.ts +99 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +299 -103
  42. package/fs/indexeddb-chunks.ts +103 -9
  43. package/fs/indexeddb.ts +477 -121
  44. package/fs/memory.ts +120 -19
  45. package/fs/multitab-backend.ts +440 -95
  46. package/fs/multitab-protocol.ts +1 -0
  47. package/fs/persistent.ts +88 -16
  48. package/fs/protocol.ts +26 -7
  49. package/fs/publication-attachment.ts +9 -7
  50. package/fs/service.ts +62 -7
  51. package/fs/shared.ts +110 -8
  52. package/fs/storage.ts +128 -10
  53. package/fs/watch-service.ts +110 -3
  54. package/fs/watch-subscriptions.ts +115 -7
  55. package/package.json +1 -1
  56. package/proc/command-stdio.ts +1 -1
  57. package/proc/intrinsics/core.ts +7 -0
  58. package/proc/intrinsics.ts +1 -0
  59. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  60. package/testing/indexeddb-storage-browser.ts +564 -1
  61. package/tooling/program/program-transformer.json +1 -1
@@ -9,7 +9,12 @@ import {
9
9
  pack,
10
10
  unpack
11
11
  } from "./protocol.js";
12
- import { CLIENT_MODE, CLIENT_MODE_NO_CLIENT_CACHE, CLIENT_MODE_NO_DIRECT_IO } from "./shared.js";
12
+ import {
13
+ CLIENT_MODE,
14
+ CLIENT_MODE_NO_CLIENT_CACHE,
15
+ CLIENT_MODE_NO_DIRECT_IO,
16
+ CLIENT_MODE_NO_WATCH_DRAIN
17
+ } from "./shared.js";
13
18
  import { HEADER_WORDS, WATCH_OFFSET } from "./storage-geometry.js";
14
19
  import { WatchRing } from "./watch-ring.js";
15
20
  import { WatchService } from "./watch-service.js";
@@ -141,6 +146,11 @@ function waitForAbortOrCancel(signal, cancelled) {
141
146
  };
142
147
  return { promise, cleanup };
143
148
  }
149
+
150
+ class LeaseLapsedError extends Error {
151
+ }
152
+ const LEASE_LAPSED = new LeaseLapsedError("host lease lapsed");
153
+ const LEASE_RENEWAL_BUDGET_LEASES = 4;
144
154
  async function raceWithAbort(promise, signal, cancelled) {
145
155
  if (signal.aborted || cancelled?.())
146
156
  return;
@@ -318,7 +328,14 @@ export class MultiTabBackend {
318
328
  sharedMaxBytes;
319
329
  retainedSegments = [];
320
330
  watchCells;
321
- role = "init";
331
+ roleState = "init";
332
+ get role() {
333
+ return this.roleState;
334
+ }
335
+ set role(next) {
336
+ this.roleState = next;
337
+ this.syncClientMode();
338
+ }
322
339
  phase = "opening";
323
340
  steppingDown = false;
324
341
  disposing = false;
@@ -345,6 +362,7 @@ export class MultiTabBackend {
345
362
  leaseTimer;
346
363
  leaseRenewInFlight;
347
364
  persistent;
365
+ unlinkEvents = 0;
348
366
  watchService;
349
367
  hostQueue = Promise.resolve();
350
368
  barrier = new BarrierGate;
@@ -403,6 +421,7 @@ export class MultiTabBackend {
403
421
  byFileId = new Map;
404
422
  nextLocalWatchId = 1;
405
423
  localWatches = new Map;
424
+ hostWatchToLocal = new Map;
406
425
  constructor(options) {
407
426
  this.options = options;
408
427
  this.scope = multitabScope(options.implementation, options.name);
@@ -503,14 +522,22 @@ export class MultiTabBackend {
503
522
  this.configureSharedBuffer(buffer);
504
523
  if (this.persistent) {
505
524
  this.persistent.attachShared(buffer, this.sharedMaxBytes, this.retainedSegments);
506
- this.persistent.shared.setClientMode(CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE);
507
525
  }
526
+ this.syncClientMode();
508
527
  }
509
528
  configureSharedBuffer(buffer) {
510
- const header = new Int32Array(buffer, 0, HEADER_WORDS);
511
- Atomics.store(header, CLIENT_MODE, CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE);
512
529
  const ring = new WatchRing(buffer, WATCH_OFFSET);
513
530
  this.watchCells = ring.cells;
531
+ this.syncClientMode();
532
+ }
533
+ syncClientMode() {
534
+ const mode = CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE | (this.roleState === "host" ? 0 : CLIENT_MODE_NO_WATCH_DRAIN);
535
+ if (this.persistent) {
536
+ this.persistent.shared.setClientMode(mode);
537
+ } else if (this.sharedBuffer) {
538
+ const header = new Int32Array(this.sharedBuffer, 0, HEADER_WORDS);
539
+ Atomics.store(header, CLIENT_MODE, mode);
540
+ }
514
541
  }
515
542
  wakeWatchWord() {
516
543
  if (!this.watchCells)
@@ -533,6 +560,17 @@ export class MultiTabBackend {
533
560
  }
534
561
  return Promise.resolve();
535
562
  }
563
+ setPersistent(next) {
564
+ if (next === this.persistent)
565
+ return;
566
+ this.unlinkEvents += (this.persistent?.unlinkGeneration() ?? 0) + 1;
567
+ this.persistent = next;
568
+ }
569
+ unlinkGeneration() {
570
+ if (this.role !== "host" || !this.persistent)
571
+ return NaN;
572
+ return this.unlinkEvents + this.persistent.unlinkGeneration();
573
+ }
536
574
  isUnlinked(handle) {
537
575
  const entry = this.byLocalHandle.get(handle);
538
576
  if (!entry)
@@ -585,7 +623,7 @@ export class MultiTabBackend {
585
623
  const entry = this.byLocalHandle.get(handle);
586
624
  if (!entry || entry.stale || entry.remoteHandle === undefined)
587
625
  return;
588
- return this.persistent.prepareDescriptorRead(entry.remoteHandle, offset, length, () => cancelled() || !this.isPreparedReadValid() || this.hostTerm !== admittedTerm);
626
+ return this.persistent.prepareDescriptorRead(entry.remoteHandle, offset, length, () => cancelled() || this.hostTerm !== admittedTerm || !this.isPreparedReadValid() && !this.leaseOnlyLapsed(admittedTerm));
589
627
  }
590
628
  prepare(operation, args, _data) {
591
629
  if (this.role !== "host" || !this.persistent || !this.isLeaseValid())
@@ -717,7 +755,8 @@ export class MultiTabBackend {
717
755
  }
718
756
  if (this.role === "host") {
719
757
  const admittedTerm = this.hostTerm;
720
- this.assertHostReadyForLocal(admittedTerm);
758
+ const budget = {};
759
+ await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
721
760
  let resolvedTarget = target;
722
761
  if (typeof target === "number") {
723
762
  const entry = this.byLocalHandle.get(target);
@@ -728,7 +767,7 @@ export class MultiTabBackend {
728
767
  this.enterHostAdmitted();
729
768
  try {
730
769
  await this.hostQueue;
731
- this.assertHostReadyForLocal(admittedTerm);
770
+ await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
732
771
  await this.persistent.flush(resolvedTarget);
733
772
  } finally {
734
773
  this.leaveHostAdmitted();
@@ -759,13 +798,15 @@ export class MultiTabBackend {
759
798
  throw new FilesystemError(8);
760
799
  if (this.role === "host") {
761
800
  const admittedTerm = this.hostTerm;
762
- this.assertHostReadyForLocal(admittedTerm);
801
+ if (!this.hostReadyForLocal(admittedTerm)) {
802
+ return this.watchOpenAfterRenewal(owner, handle, recursive, cancelled, admittedTerm);
803
+ }
763
804
  if (entry.remoteHandle === undefined)
764
805
  throw new FilesystemError(8);
765
806
  this.watchService ??= new WatchService(this.persistent);
766
807
  const hostWatchId = this.watchService.open(owner, entry.remoteHandle, recursive, cancelled);
767
808
  const localWatchId = this.nextLocalWatchId++;
768
- this.localWatches.set(localWatchId, {
809
+ const localEntry = {
769
810
  localWatchId,
770
811
  localOwner: owner,
771
812
  fileId: entry.fileId,
@@ -774,7 +815,9 @@ export class MultiTabBackend {
774
815
  hostWatchId,
775
816
  overflowed: false,
776
817
  closed: false
777
- });
818
+ };
819
+ this.localWatches.set(localWatchId, localEntry);
820
+ this.hostWatchToLocal.set(hostWatchId, localEntry);
778
821
  return localWatchId;
779
822
  }
780
823
  if (this.needsFailoverWait()) {
@@ -832,20 +875,124 @@ export class MultiTabBackend {
832
875
  }
833
876
  throw new FilesystemError(11);
834
877
  }
878
+ async watchOpenAfterRenewal(owner, handle, recursive, cancelled, admittedTerm) {
879
+ await this.renewLapsedLease(admittedTerm, cancelled);
880
+ return this.watchOpen(owner, handle, recursive, cancelled);
881
+ }
835
882
  async watchReadHostAfterRenew(owner, watch, cancelled) {
883
+ const overflow = {
884
+ batch: { kind: "overflow", events: [] },
885
+ release() {},
886
+ rollback() {}
887
+ };
888
+ const budget = {};
889
+ await this.renewWatchLeaseOnce(watch.hostTerm, cancelled);
890
+ for (;; ) {
891
+ if (this.disposing || cancelled())
892
+ throw new FilesystemError(11);
893
+ const termEnded = watch.hostTerm !== this.hostTerm || this.role !== "host" || this.ownershipLost || this.leaseClosed;
894
+ if (watch.overflowed || termEnded || !this.watchService) {
895
+ watch.overflowed = true;
896
+ return overflow;
897
+ }
898
+ if (this.isLeaseValid())
899
+ return this.watchService.readHeld(owner, watch.hostWatchId);
900
+ if (!await this.renewWatchLease(watch.hostTerm, cancelled, budget))
901
+ return overflow;
902
+ }
903
+ }
904
+ async renewWatchLeaseOnce(admittedTerm, cancelled) {
836
905
  if (this.disposing || cancelled())
837
906
  throw new FilesystemError(11);
838
- let ok = false;
839
- await raceWithAbort(this.renewLeaseNow().then((renewed) => {
840
- ok = renewed;
841
- }), this.disposeAbort.signal, cancelled);
842
- if (this.disposing || cancelled())
843
- throw new FilesystemError(11);
844
- if (!ok || !this.isLeaseValid() || watch.overflowed || watch.hostTerm !== this.hostTerm || !this.watchService) {
845
- watch.overflowed = true;
846
- return { batch: { kind: "overflow", events: [] }, release() {}, rollback() {} };
907
+ if (this.leaseOnlyLapsed(admittedTerm))
908
+ return;
909
+ await raceWithAbort(this.renewLeaseNow(), this.disposeAbort.signal, cancelled);
910
+ }
911
+ async renewWatchLease(admittedTerm, cancelled, budget) {
912
+ if (!this.leaseOnlyLapsed(admittedTerm))
913
+ return false;
914
+ try {
915
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
916
+ return true;
917
+ } catch {
918
+ if (this.disposing || cancelled())
919
+ throw new FilesystemError(11);
920
+ return false;
921
+ }
922
+ }
923
+ watchDrain(owner, cancelled) {
924
+ if (this.role === "host") {
925
+ const admittedTerm = this.hostTerm;
926
+ if (!this.isLeaseValid()) {
927
+ return this.watchDrainHostAfterRenew(owner, admittedTerm, cancelled);
928
+ }
929
+ return this.drainHostWatches(owner);
930
+ }
931
+ throw new FilesystemError(58);
932
+ }
933
+ overflowOwnerWatches(owner, permanent) {
934
+ const entries = [];
935
+ for (const watch of this.localWatches.values()) {
936
+ if (watch.closed || watch.localOwner !== owner)
937
+ continue;
938
+ if (permanent || watch.hostTerm !== this.hostTerm)
939
+ watch.overflowed = true;
940
+ entries.push({ id: watch.localWatchId, batch: { kind: "overflow", events: [] } });
847
941
  }
848
- return this.watchService.readHeld(owner, watch.hostWatchId);
942
+ return { entries, more: false, hasEvents: false, release() {}, rollback() {} };
943
+ }
944
+ async watchDrainHostAfterRenew(owner, admittedTerm, cancelled) {
945
+ const budget = {};
946
+ await this.renewWatchLeaseOnce(admittedTerm, cancelled);
947
+ for (;; ) {
948
+ if (this.disposing || cancelled())
949
+ throw new FilesystemError(11);
950
+ const termEnded = admittedTerm !== this.hostTerm || this.role !== "host" || this.ownershipLost || this.leaseClosed || !!this.terminalFailure || !this.watchService;
951
+ if (termEnded)
952
+ return this.overflowOwnerWatches(owner, true);
953
+ if (this.isLeaseValid())
954
+ return this.drainHostWatches(owner);
955
+ if (!await this.renewWatchLease(admittedTerm, cancelled, budget))
956
+ return this.overflowOwnerWatches(owner, false);
957
+ }
958
+ }
959
+ drainHostWatches(owner) {
960
+ if (this.terminalFailure || !this.watchService) {
961
+ return this.overflowOwnerWatches(owner, true);
962
+ }
963
+ const entries = [];
964
+ const seen = new Set;
965
+ const held = this.watchService.drainHeld(owner);
966
+ for (const entry of held.entries) {
967
+ const local = this.hostWatchToLocal.get(entry.id);
968
+ if (!local || local.closed || local.localOwner !== owner)
969
+ continue;
970
+ if (local.overflowed || local.hostTerm !== this.hostTerm) {
971
+ local.overflowed = true;
972
+ seen.add(local.localWatchId);
973
+ entries.push({ id: local.localWatchId, batch: { kind: "overflow", events: [] } });
974
+ continue;
975
+ }
976
+ seen.add(local.localWatchId);
977
+ entries.push("batch" in entry ? { id: local.localWatchId, batch: entry.batch } : { id: local.localWatchId, errno: entry.errno });
978
+ }
979
+ if (this.hostWatchToLocal.size !== this.localWatches.size) {
980
+ for (const watch of this.localWatches.values()) {
981
+ if (watch.closed || watch.localOwner !== owner || seen.has(watch.localWatchId))
982
+ continue;
983
+ if (watch.overflowed || watch.hostTerm !== this.hostTerm || watch.hostWatchId === undefined) {
984
+ watch.overflowed = true;
985
+ entries.push({ id: watch.localWatchId, batch: { kind: "overflow", events: [] } });
986
+ }
987
+ }
988
+ }
989
+ return {
990
+ entries,
991
+ more: held.more,
992
+ hasEvents: held.hasEvents,
993
+ release: held.release,
994
+ rollback: held.rollback
995
+ };
849
996
  }
850
997
  watchClose(owner, localWatchId) {
851
998
  const watch = this.localWatches.get(localWatchId);
@@ -853,6 +1000,9 @@ export class MultiTabBackend {
853
1000
  return;
854
1001
  watch.closed = true;
855
1002
  this.localWatches.delete(localWatchId);
1003
+ if (watch.hostWatchId !== undefined && this.hostWatchToLocal.get(watch.hostWatchId) === watch) {
1004
+ this.hostWatchToLocal.delete(watch.hostWatchId);
1005
+ }
856
1006
  if (this.role === "host" && watch.hostTerm === this.hostTerm && watch.hostWatchId !== undefined) {
857
1007
  this.watchService?.close(owner, watch.hostWatchId);
858
1008
  } else if (this.role === "peer" && watch.hostTerm === this.peerHostTerm && watch.hostWatchId !== undefined && this.peerSessionId) {
@@ -931,7 +1081,7 @@ export class MultiTabBackend {
931
1081
  }
932
1082
  if (this.persistent) {
933
1083
  const residual = this.persistent;
934
- this.persistent = undefined;
1084
+ this.setPersistent(undefined);
935
1085
  this.watchService = undefined;
936
1086
  await this.retireFailedMountBackend(residual);
937
1087
  }
@@ -1154,7 +1304,7 @@ export class MultiTabBackend {
1154
1304
  }
1155
1305
  });
1156
1306
  if (this.sharedBuffer) {
1157
- backend.shared.setClientMode(CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE);
1307
+ backend.shared.setClientMode(CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE | CLIENT_MODE_NO_WATCH_DRAIN);
1158
1308
  }
1159
1309
  if (!backend.hasLoadedIndex) {
1160
1310
  await backend.flush();
@@ -1196,7 +1346,7 @@ export class MultiTabBackend {
1196
1346
  this.triggerExternalOwnershipLost();
1197
1347
  throw new FilesystemError(11, "workspace taken over by exclusive owner");
1198
1348
  }
1199
- this.persistent = backend;
1349
+ this.setPersistent(backend);
1200
1350
  this.watchService = new WatchService(backend);
1201
1351
  this.hostTerm = backend.initialReservationEra;
1202
1352
  this.hostStorageEpoch = storageEpoch;
@@ -1255,7 +1405,7 @@ export class MultiTabBackend {
1255
1405
  if (this.terminalFailure)
1256
1406
  throw wrapReleaseError(this.terminalFailure.reason, false);
1257
1407
  if (backend) {
1258
- this.persistent = undefined;
1408
+ this.setPersistent(undefined);
1259
1409
  this.watchService = undefined;
1260
1410
  await this.retireFailedMountBackend(backend);
1261
1411
  } else {
@@ -1585,7 +1735,7 @@ export class MultiTabBackend {
1585
1735
  return;
1586
1736
  }
1587
1737
  this.captureRetainedSegments(persistent);
1588
- this.persistent = undefined;
1738
+ this.setPersistent(undefined);
1589
1739
  this.watchService = undefined;
1590
1740
  if (this.sharedBuffer)
1591
1741
  persistent.shared.resetForRemount(this.retainedSegments);
@@ -1641,7 +1791,7 @@ export class MultiTabBackend {
1641
1791
  this.closeAllHostPeerSessions();
1642
1792
  if (this.persistent) {
1643
1793
  const persistent = this.persistent;
1644
- this.persistent = undefined;
1794
+ this.setPersistent(undefined);
1645
1795
  this.watchService = undefined;
1646
1796
  await persistent.retireFenced().catch(() => {});
1647
1797
  this.captureRetainedSegments(persistent);
@@ -1691,7 +1841,7 @@ export class MultiTabBackend {
1691
1841
  this.closeAllHostPeerSessions();
1692
1842
  if (this.persistent) {
1693
1843
  const persistent = this.persistent;
1694
- this.persistent = undefined;
1844
+ this.setPersistent(undefined);
1695
1845
  this.watchService = undefined;
1696
1846
  await persistent.retireFenced().catch(() => {});
1697
1847
  this.captureRetainedSegments(persistent);
@@ -1714,7 +1864,7 @@ export class MultiTabBackend {
1714
1864
  this.closeAllHostPeerSessions();
1715
1865
  if (this.persistent) {
1716
1866
  const persistent = this.persistent;
1717
- this.persistent = undefined;
1867
+ this.setPersistent(undefined);
1718
1868
  this.watchService = undefined;
1719
1869
  await persistent.retireFenced().catch(() => {});
1720
1870
  this.captureRetainedSegments(persistent);
@@ -3993,7 +4143,7 @@ export class MultiTabBackend {
3993
4143
  this.hostAdmittedZeroWaiters.push(resolve);
3994
4144
  });
3995
4145
  }
3996
- assertHostReadyForLocal(admittedTerm) {
4146
+ hostReadyForLocal(admittedTerm) {
3997
4147
  if (this.terminalFailure || this.persistent?.isTerminallyFailed()) {
3998
4148
  throw new FilesystemError(29, describeReason(this.terminalFailure?.reason));
3999
4149
  }
@@ -4003,46 +4153,114 @@ export class MultiTabBackend {
4003
4153
  if (this.steppingDown && (!this.disposing || this.disposeHostTerm !== admittedTerm)) {
4004
4154
  throw new FilesystemError(11, "host is stepping down");
4005
4155
  }
4006
- if (!this.isLeaseValid()) {
4007
- throw new FilesystemError(11, "host lease expired");
4156
+ if (this.isLeaseValid())
4157
+ return true;
4158
+ if (this.lapseIsRenewable(admittedTerm))
4159
+ return false;
4160
+ throw new FilesystemError(11, "host lease expired");
4161
+ }
4162
+ leaseOnlyLapsed(admittedTerm) {
4163
+ return !this.isLeaseValid() && this.lapseIsRenewable(admittedTerm);
4164
+ }
4165
+ lapseIsRenewable(admittedTerm) {
4166
+ return !this.leaseClosed && !this.ownershipLost && !this.terminalFailure && !this.persistent?.isTerminallyFailed() && !!this.persistent && this.role === "host" && this.hostTerm === admittedTerm && !this.steppingDown && !this.disposing;
4167
+ }
4168
+ assertLocalAttempt(admittedTerm, cancelled) {
4169
+ if (cancelled?.())
4170
+ throw new FilesystemError(11, "operation cancelled");
4171
+ if (!this.hostReadyForLocal(admittedTerm))
4172
+ throw LEASE_LAPSED;
4173
+ }
4174
+ localAttemptStale(admittedTerm, cancelled) {
4175
+ if (cancelled?.() || this.hostTerm !== admittedTerm)
4176
+ return true;
4177
+ return !this.isLeaseValid() && !this.leaseOnlyLapsed(admittedTerm);
4178
+ }
4179
+ async retryAfterLeaseRenewal(admittedTerm, cancelled, budget, attempt) {
4180
+ for (;; ) {
4181
+ try {
4182
+ return await attempt();
4183
+ } catch (error) {
4184
+ if (error !== LEASE_LAPSED)
4185
+ throw error;
4186
+ }
4187
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4008
4188
  }
4009
4189
  }
4010
- async executeLocalOnHost(operation, args, data, responseCookieTag, cancelled) {
4190
+ async assertHostAfterRenewal(admittedTerm, cancelled, budget) {
4191
+ while (!this.hostReadyForLocal(admittedTerm)) {
4192
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4193
+ }
4194
+ }
4195
+ async renewLapsedLease(admittedTerm, cancelled, budget = {}) {
4196
+ const fresh = budget.deadline === undefined;
4197
+ const deadline = budget.deadline ??= performance.now() + this.timings.leaseDurationMs * LEASE_RENEWAL_BUDGET_LEASES;
4198
+ for (let attempt = 0;this.leaseOnlyLapsed(admittedTerm); attempt++) {
4199
+ if ((attempt > 0 || !fresh) && performance.now() >= deadline) {
4200
+ throw new FilesystemError(11, "host lease could not be renewed");
4201
+ }
4202
+ if (attempt > 0)
4203
+ await this.pauseBeforeRenewal(cancelled);
4204
+ await raceWithAbort(this.renewLeaseNow(this.currentAttemptId), this.disposeAbort.signal, cancelled);
4205
+ if (cancelled?.())
4206
+ throw new FilesystemError(11, "operation cancelled");
4207
+ }
4208
+ }
4209
+ async pauseBeforeRenewal(cancelled) {
4210
+ await raceWithAbort(delay(this.timings.waitPollMs), this.disposeAbort.signal, cancelled);
4011
4211
  if (cancelled?.())
4012
4212
  throw new FilesystemError(11, "operation cancelled");
4013
- const admittedTerm = this.hostTerm;
4014
- if (!this.isLeaseValid()) {
4015
- if (this.disposing)
4016
- throw new FilesystemError(11, "host lease expired during dispose");
4017
- let ok = false;
4018
- await raceWithAbort(this.renewLeaseNow(this.currentAttemptId).then((renewed) => {
4019
- ok = renewed;
4020
- }), this.disposeAbort.signal, cancelled);
4213
+ }
4214
+ async renewBeforeLocalHostCall(admittedTerm, budget, operation, args, data, responseCookieTag, cancelled) {
4215
+ if (this.disposing)
4216
+ throw new FilesystemError(11, "host lease expired during dispose");
4217
+ if (this.leaseOnlyLapsed(admittedTerm)) {
4218
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4219
+ } else {
4220
+ await raceWithAbort(this.renewLeaseNow(this.currentAttemptId), this.disposeAbort.signal, cancelled);
4221
+ }
4222
+ if (cancelled?.())
4223
+ throw new FilesystemError(11, "operation cancelled");
4224
+ if (this.disposing)
4225
+ throw new FilesystemError(11, "filesystem is disposing");
4226
+ if (!this.isLeaseValid() && !this.leaseOnlyLapsed(admittedTerm)) {
4227
+ await this.awaitPendingFenceLoss(cancelled);
4021
4228
  if (cancelled?.())
4022
4229
  throw new FilesystemError(11, "operation cancelled");
4023
4230
  if (this.disposing)
4024
4231
  throw new FilesystemError(11, "filesystem is disposing");
4025
- if (!ok || !this.isLeaseValid()) {
4026
- await this.awaitPendingFenceLoss(cancelled);
4027
- if (cancelled?.())
4028
- throw new FilesystemError(11, "operation cancelled");
4029
- if (this.disposing)
4030
- throw new FilesystemError(11, "filesystem is disposing");
4031
- if (this.terminalFailure) {
4032
- throw new FilesystemError(29, describeReason(this.terminalFailure.reason));
4033
- }
4034
- if (this.ownershipLost)
4035
- throw new FilesystemError(11, "filesystem ownership lost");
4036
- if (this.role === "peer" || this.role === "failover" || this.role === "waiting") {
4037
- const nextCookieTag = this.revalidateCookieForPeerForward(operation, args, responseCookieTag);
4038
- return this.executeOnPeer(operation, args, data, nextCookieTag, cancelled);
4039
- }
4040
- throw new FilesystemError(11, "host lease expired");
4232
+ if (this.terminalFailure) {
4233
+ throw new FilesystemError(29, describeReason(this.terminalFailure.reason));
4041
4234
  }
4235
+ if (this.ownershipLost)
4236
+ throw new FilesystemError(11, "filesystem ownership lost");
4237
+ if (this.role === "peer" || this.role === "failover" || this.role === "waiting") {
4238
+ const nextCookieTag = this.revalidateCookieForPeerForward(operation, args, responseCookieTag);
4239
+ return {
4240
+ result: await this.executeOnPeer(operation, args, data, nextCookieTag, cancelled)
4241
+ };
4242
+ }
4243
+ throw new FilesystemError(11, "host lease expired");
4244
+ }
4245
+ return;
4246
+ }
4247
+ async executeLocalOnHost(operation, args, data, responseCookieTag, cancelled) {
4248
+ if (cancelled?.())
4249
+ throw new FilesystemError(11, "operation cancelled");
4250
+ const admittedTerm = this.hostTerm;
4251
+ const budget = {};
4252
+ if (!this.isLeaseValid()) {
4253
+ const forwarded = await this.renewBeforeLocalHostCall(admittedTerm, budget, operation, args, data, responseCookieTag, cancelled);
4254
+ if (forwarded)
4255
+ return forwarded.result;
4042
4256
  }
4043
4257
  if (cancelled?.())
4044
4258
  throw new FilesystemError(11, "operation cancelled");
4045
- this.assertHostReadyForLocal(admittedTerm);
4259
+ if (!this.hostReadyForLocal(admittedTerm)) {
4260
+ await this.assertHostAfterRenewal(admittedTerm, cancelled, budget);
4261
+ if (cancelled?.())
4262
+ throw new FilesystemError(11, "operation cancelled");
4263
+ }
4046
4264
  const persistent = this.persistent;
4047
4265
  const translated = this.translateArgsToHost(operation, args);
4048
4266
  if (!translated)
@@ -4054,14 +4272,14 @@ export class MultiTabBackend {
4054
4272
  await this.hostQueue;
4055
4273
  if (cancelled?.())
4056
4274
  throw new FilesystemError(11, "operation cancelled");
4057
- this.assertHostReadyForLocal(admittedTerm);
4275
+ await this.assertHostAfterRenewal(admittedTerm, cancelled, budget);
4058
4276
  await persistent.flush(target);
4059
4277
  return operation === "sync" ? 0 : undefined;
4060
4278
  }
4061
4279
  if (operation === "read") {
4062
- return await this.executeLocalHostRead(persistent, admittedTerm, translated, cancelled);
4280
+ return await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () => this.executeLocalHostRead(persistent, admittedTerm, translated, cancelled));
4063
4281
  }
4064
- const result = await this.executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled);
4282
+ const result = await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () => this.executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled));
4065
4283
  return this.mapLocalHostHandles(persistent, admittedTerm, operation, result);
4066
4284
  } finally {
4067
4285
  this.leaveHostAdmitted();
@@ -4069,15 +4287,11 @@ export class MultiTabBackend {
4069
4287
  }
4070
4288
  async executeLocalHostRead(persistent, admittedTerm, translated, cancelled) {
4071
4289
  const [remoteHandle, length, offset] = translated;
4072
- const assertValid = () => {
4073
- if (cancelled?.())
4074
- throw new FilesystemError(11, "operation cancelled");
4075
- this.assertHostReadyForLocal(admittedTerm);
4076
- };
4290
+ const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
4077
4291
  for (;; ) {
4078
4292
  assertValid();
4079
4293
  const startVer = this.readVersion();
4080
- const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid());
4294
+ const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () => this.localAttemptStale(admittedTerm, cancelled));
4081
4295
  if (prep) {
4082
4296
  const accepted = await this.revalidateColdReadBytes(prep, startVer, assertValid);
4083
4297
  if (accepted !== undefined)
@@ -4093,11 +4307,7 @@ export class MultiTabBackend {
4093
4307
  }
4094
4308
  }
4095
4309
  async executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled) {
4096
- const assertValid = () => {
4097
- if (cancelled?.())
4098
- throw new FilesystemError(11, "operation cancelled");
4099
- this.assertHostReadyForLocal(admittedTerm);
4100
- };
4310
+ const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
4101
4311
  for (;; ) {
4102
4312
  assertValid();
4103
4313
  const preparing = persistent.prepare(operation, translated);
@@ -4116,7 +4326,7 @@ export class MultiTabBackend {
4116
4326
  result = step.result;
4117
4327
  });
4118
4328
  if (dirtyRetryChange !== undefined) {
4119
- await persistent.waitForDirtyCapacity(dirtyRetryChange, () => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid());
4329
+ await persistent.waitForDirtyCapacity(dirtyRetryChange, () => this.localAttemptStale(admittedTerm, cancelled));
4120
4330
  continue;
4121
4331
  }
4122
4332
  return result;
@@ -4917,6 +5127,7 @@ export class MultiTabBackend {
4917
5127
  entry.remoteSessionId = sessionId;
4918
5128
  entry.pinToken = wire.pinToken;
4919
5129
  entry.unlinked = !!wire.unlinked;
5130
+ this.unlinkEvents++;
4920
5131
  entry.stale = false;
4921
5132
  }
4922
5133
  } finally {
@@ -4925,6 +5136,7 @@ export class MultiTabBackend {
4925
5136
  }
4926
5137
  rebindLocalHandlesOnHost(term) {
4927
5138
  const persistent = this.persistent;
5139
+ this.unlinkEvents++;
4928
5140
  for (const entry of this.byLocalHandle.values()) {
4929
5141
  const node = persistent.nodeByFileId(entry.fileId);
4930
5142
  if (!node) {
@@ -4996,6 +5208,8 @@ export class MultiTabBackend {
4996
5208
  this.notifyFailoverReadyWaiters();
4997
5209
  }
4998
5210
  ensureLocalHandle(fileId, remoteHandle, remoteTerm, remoteSessionId, pinToken, unlinked) {
5211
+ if (unlinked)
5212
+ this.unlinkEvents++;
4999
5213
  const existing = this.byFileId.get(fileId);
5000
5214
  if (existing) {
5001
5215
  if (remoteSessionId !== undefined && pinToken !== undefined) {
@@ -5058,6 +5272,7 @@ export class MultiTabBackend {
5058
5272
  }
5059
5273
  }
5060
5274
  detachHandlesForRemount() {
5275
+ this.unlinkEvents++;
5061
5276
  for (const entry of this.byLocalHandle.values()) {
5062
5277
  entry.remoteHandle = undefined;
5063
5278
  entry.remoteTerm = undefined;
@@ -5126,7 +5341,8 @@ export class MultiTabBackend {
5126
5341
  case "setTimes":
5127
5342
  case "readdir":
5128
5343
  case "readdirPage":
5129
- case "resolve": {
5344
+ case "resolve":
5345
+ case "realpath": {
5130
5346
  const remote = mapHandle(args[0]);
5131
5347
  if (remote === undefined)
5132
5348
  return;
@@ -5157,6 +5373,7 @@ export class MultiTabBackend {
5157
5373
  this.dirEpoch++;
5158
5374
  }
5159
5375
  markLocalWatchesOverflow() {
5376
+ this.hostWatchToLocal.clear();
5160
5377
  for (const watch of this.localWatches.values()) {
5161
5378
  watch.overflowed = true;
5162
5379
  }
@@ -143,6 +143,7 @@ const PURE_STATELESS_READ_OPS = new Set([
143
143
  "readdir",
144
144
  "readlink",
145
145
  "resolve",
146
+ "realpath",
146
147
  "readdirPage"
147
148
  ]);
148
149
  export function isReplayableRead(operation, args) {