@kumiai/rpc 0.10.1 → 0.10.2
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/lib/peer.js +127 -32
- package/package.json +1 -1
package/lib/peer.js
CHANGED
|
@@ -203,6 +203,7 @@ export function createGroupPeer(params) {
|
|
|
203
203
|
*/ const captureAnchor = async ()=>{
|
|
204
204
|
// Secret and epoch from one export: a pair from two reads can straddle a handle move.
|
|
205
205
|
const { secret, epoch } = await crypto.exportSecret(APP_TOPIC_LABEL);
|
|
206
|
+
if (disposed) return;
|
|
206
207
|
anchor = {
|
|
207
208
|
secret,
|
|
208
209
|
epoch
|
|
@@ -210,6 +211,7 @@ export function createGroupPeer(params) {
|
|
|
210
211
|
sealError = undefined;
|
|
211
212
|
finishSealBarrier();
|
|
212
213
|
await anchorStore?.save(anchor);
|
|
214
|
+
if (disposed) return;
|
|
213
215
|
// The anchor moving IS the segment boundary, so every capture ends the segment the buffer
|
|
214
216
|
// belongs to. AFTER the assignment above: the lane rebuilds its cursors off the live anchor.
|
|
215
217
|
appLane.reset();
|
|
@@ -586,7 +588,9 @@ export function createGroupPeer(params) {
|
|
|
586
588
|
};
|
|
587
589
|
};
|
|
588
590
|
const rebuildEpoch = async ()=>{
|
|
591
|
+
if (disposed) return;
|
|
589
592
|
await teardownEpoch();
|
|
593
|
+
if (disposed) return;
|
|
590
594
|
await buildEpoch();
|
|
591
595
|
};
|
|
592
596
|
/**
|
|
@@ -597,10 +601,8 @@ export function createGroupPeer(params) {
|
|
|
597
601
|
* dispose and would otherwise land their publish after it.
|
|
598
602
|
*
|
|
599
603
|
* The check belongs immediately after each entry point's `await ready`, because that await is
|
|
600
|
-
* where the race lives.
|
|
601
|
-
*
|
|
602
|
-
* enough to run to completion against runtimes a teardown is about to walk. Unguarded, each
|
|
603
|
-
* entry point fails differently and none of them says why: `to()` handed back a live-looking
|
|
604
|
+
* where the race lives. Disposal can tear down while a call queued during init resumes.
|
|
605
|
+
* Unguarded, each entry point fails differently and none says why: `to()` handed back a live-looking
|
|
604
606
|
* client over an already-aborted transport, `resync()` rebuilt a whole epoch onto a disposed
|
|
605
607
|
* mux, and `commit()` published to the hub from a peer that is gone. In the mirror ordering,
|
|
606
608
|
* where teardown got there first and emptied `runtimes`, a protocol call blamed
|
|
@@ -903,10 +905,13 @@ export function createGroupPeer(params) {
|
|
|
903
905
|
// it. Per frame-epoch, not per rotation: a segment spanning five epochs is dispensed five
|
|
904
906
|
// times off the one pull.
|
|
905
907
|
await appLane.deliver();
|
|
908
|
+
assertLive();
|
|
906
909
|
// Normalized at this ingress so an MLS-recovered form flip between the two reads (never a
|
|
907
910
|
// real membership change) does not read as one — see {@link detectRosterChange}.
|
|
908
911
|
const rosterBefore = (await port.rosterEntries()).map((e)=>normalizeDID(e.did));
|
|
912
|
+
assertLive();
|
|
909
913
|
const epochBefore = await port.readEpoch();
|
|
914
|
+
assertLive();
|
|
910
915
|
sealBarrier = new Promise((resolve)=>{
|
|
911
916
|
releaseSealBarrier = resolve;
|
|
912
917
|
});
|
|
@@ -965,6 +970,7 @@ export function createGroupPeer(params) {
|
|
|
965
970
|
commitLogHead = head == null ? null : asLogPosition(head);
|
|
966
971
|
};
|
|
967
972
|
while(true){
|
|
973
|
+
if (disposed) return false;
|
|
968
974
|
const pageAfter = reconciledHead;
|
|
969
975
|
const result = await mux.fetchTopic({
|
|
970
976
|
topicID,
|
|
@@ -975,6 +981,7 @@ export function createGroupPeer(params) {
|
|
|
975
981
|
} : {},
|
|
976
982
|
limit: COMMIT_FETCH_LIMIT
|
|
977
983
|
});
|
|
984
|
+
if (disposed) return false;
|
|
978
985
|
// A short page may carry a one-shot fork reveal below the cursor. Only a full page loops.
|
|
979
986
|
if (result.messages.length === COMMIT_FETCH_LIMIT) {
|
|
980
987
|
assertForwardPage(pageAfter, result.messages);
|
|
@@ -1005,6 +1012,7 @@ export function createGroupPeer(params) {
|
|
|
1005
1012
|
// No digest: the frame's version put its commit bytes out of reach entirely. Settled at
|
|
1006
1013
|
// `ahead` before the digest is read.
|
|
1007
1014
|
const localEpoch = await port.readEpoch();
|
|
1015
|
+
if (disposed) return false;
|
|
1008
1016
|
const unreadable = classifyCommit({
|
|
1009
1017
|
header: UNKNOWN_FRAME_VERSION,
|
|
1010
1018
|
sequenceID: position,
|
|
@@ -1048,6 +1056,7 @@ export function createGroupPeer(params) {
|
|
|
1048
1056
|
// heal from — dropping it would step over the group's whole future. To the classifier.
|
|
1049
1057
|
if (isUnsupportedCommitFrameVersion(error)) {
|
|
1050
1058
|
const localEpoch = await port.readEpoch();
|
|
1059
|
+
if (disposed) return false;
|
|
1051
1060
|
const unreadable = classifyCommit({
|
|
1052
1061
|
header: UNKNOWN_FRAME_VERSION,
|
|
1053
1062
|
sequenceID: position,
|
|
@@ -1088,8 +1097,10 @@ export function createGroupPeer(params) {
|
|
|
1088
1097
|
// trusted: it could stamp every recipient's own DID onto one poison frame and make the
|
|
1089
1098
|
// whole group heal at once.
|
|
1090
1099
|
let localEpoch = await port.readEpoch();
|
|
1100
|
+
if (disposed) return false;
|
|
1091
1101
|
while(true){
|
|
1092
1102
|
const readHeader = await port.readCommitHeader(commitFrame.commit);
|
|
1103
|
+
if (disposed) return false;
|
|
1093
1104
|
// Normalized before `classifyCommit` compares it against `state.localDID` (also
|
|
1094
1105
|
// normalized, at construction) — an own-unmerged commit authenticated under a different
|
|
1095
1106
|
// form of this member's own DID must still be recognized as its own.
|
|
@@ -1170,12 +1181,20 @@ export function createGroupPeer(params) {
|
|
|
1170
1181
|
}
|
|
1171
1182
|
// Framed at this peer's epoch, by somebody else: a frame it can apply. Everything below is
|
|
1172
1183
|
// the port's answer to it.
|
|
1184
|
+
if (disposed) return false;
|
|
1173
1185
|
const framedEpoch = localEpoch;
|
|
1174
1186
|
let applied;
|
|
1175
1187
|
try {
|
|
1176
1188
|
// Through the seam, like every other site that ratchets the handle: it reads this
|
|
1177
1189
|
// epoch's app frames ahead of the apply and takes the anchor if the roster moved.
|
|
1178
|
-
applied = await advanceHandle(port, ()=>{
|
|
1190
|
+
applied = await advanceHandle(port, async ()=>{
|
|
1191
|
+
if (disposed) {
|
|
1192
|
+
return {
|
|
1193
|
+
advanced: false,
|
|
1194
|
+
epochBefore: framedEpoch,
|
|
1195
|
+
epochAfter: framedEpoch
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1179
1198
|
return port.processCommit(commitFrame.commit, {
|
|
1180
1199
|
senderDID: message.senderDID,
|
|
1181
1200
|
// The resolver, not the bodies: the blob opens only if the port asks for entries
|
|
@@ -1190,6 +1209,7 @@ export function createGroupPeer(params) {
|
|
|
1190
1209
|
// Only an APPLIED commit says anything about the group.
|
|
1191
1210
|
(result)=>result.advanced && header?.external === true, (result)=>result);
|
|
1192
1211
|
} catch (error) {
|
|
1212
|
+
if (disposed) return false;
|
|
1193
1213
|
if (!isMissingLedgerEntries(error)) {
|
|
1194
1214
|
// The port broke its contract. The cursor stays and the frame is re-read — the pull is
|
|
1195
1215
|
// a retry, and this is not an outcome it can name.
|
|
@@ -1207,6 +1227,7 @@ export function createGroupPeer(params) {
|
|
|
1207
1227
|
reconciledHead = position;
|
|
1208
1228
|
break;
|
|
1209
1229
|
}
|
|
1230
|
+
if (disposed) return false;
|
|
1210
1231
|
if (!applied.advanced && applied.epochBefore !== framedEpoch) {
|
|
1211
1232
|
advancedEpoch = true;
|
|
1212
1233
|
localEpoch = applied.epochBefore;
|
|
@@ -1242,25 +1263,35 @@ export function createGroupPeer(params) {
|
|
|
1242
1263
|
* nothing further is coming to prompt them, so a peer whose backlog is entirely at its current
|
|
1243
1264
|
* epoch (or whose log held no commits at all) would otherwise never read a thing.
|
|
1244
1265
|
*/ const pullCommits = async ()=>{
|
|
1266
|
+
if (disposed) return false;
|
|
1245
1267
|
if (!journalReplayed) {
|
|
1246
1268
|
throw new Error('pullCommits: the journal must be replayed first in every lane operation, or a peer that crashed on its own commit heals from it instead of adopting it');
|
|
1247
1269
|
}
|
|
1248
1270
|
if (mls == null || commitTopicID == null) return false;
|
|
1249
1271
|
const epochBefore = await mls.readEpoch();
|
|
1272
|
+
if (disposed) return false;
|
|
1250
1273
|
const anchorBefore = anchor;
|
|
1251
1274
|
try {
|
|
1252
1275
|
const advanced = await walkCommits(mls, commitTopicID);
|
|
1276
|
+
if (disposed) return false;
|
|
1253
1277
|
await appLane.deliver();
|
|
1278
|
+
if (disposed) return false;
|
|
1254
1279
|
return advanced;
|
|
1255
1280
|
} catch (error) {
|
|
1281
|
+
if (disposed) return false;
|
|
1256
1282
|
// A failed final drain can follow applied commits. Refresh the runtime before a retry.
|
|
1257
|
-
if (inboxLane != null
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1283
|
+
if (inboxLane != null) {
|
|
1284
|
+
const epochAfter = await mls.readEpoch();
|
|
1285
|
+
if (disposed) return false;
|
|
1286
|
+
if (epochAfter !== epochBefore || anchor !== anchorBefore) {
|
|
1287
|
+
try {
|
|
1288
|
+
await rebuildEpoch();
|
|
1289
|
+
} catch {
|
|
1290
|
+
// The walk's original failure remains the retryable cause.
|
|
1291
|
+
}
|
|
1262
1292
|
}
|
|
1263
1293
|
}
|
|
1294
|
+
if (disposed) return false;
|
|
1264
1295
|
if (crypto.pending != null && !appPullActive) {
|
|
1265
1296
|
appPullNeeded = true;
|
|
1266
1297
|
armAppPull(appPullBackoff);
|
|
@@ -1271,7 +1302,7 @@ export function createGroupPeer(params) {
|
|
|
1271
1302
|
};
|
|
1272
1303
|
/** Pull the commit log, and rebuild the app lane if the pull moved the epoch. */ const reconcileCommits = async ()=>{
|
|
1273
1304
|
const advanced = await pullCommits();
|
|
1274
|
-
if (advanced) await rebuildEpoch();
|
|
1305
|
+
if (!disposed && advanced) await rebuildEpoch();
|
|
1275
1306
|
};
|
|
1276
1307
|
/**
|
|
1277
1308
|
* A commit-topic delivery is a WAKEUP, nothing more. Frames come from the pull, never the push:
|
|
@@ -1280,20 +1311,23 @@ export function createGroupPeer(params) {
|
|
|
1280
1311
|
* never become the cursor.
|
|
1281
1312
|
*/ const onCommitDelivery = (_message, ack)=>{
|
|
1282
1313
|
ack();
|
|
1283
|
-
void
|
|
1284
|
-
|
|
1285
|
-
//
|
|
1286
|
-
// `settled`, never the mutex. Refused SILENTLY, unlike every host-facing entry point — there
|
|
1287
|
-
// is no caller to tell, and the catch below would swallow a throw anyway.
|
|
1314
|
+
void ready.then(()=>{
|
|
1315
|
+
// `ready` uses this mutex for its seed and pending pull. Never hold it while waiting for
|
|
1316
|
+
// initialization, and refuse a delivery that resumed after disposal.
|
|
1288
1317
|
if (disposed) return;
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1318
|
+
return runSerial(async ()=>{
|
|
1319
|
+
// A delivery queued before disposal is refused silently; it has no caller to notify.
|
|
1320
|
+
if (disposed) return;
|
|
1321
|
+
// A wakeup is a lane operation: step 0, the ledger invariant, then the pull. No return
|
|
1322
|
+
// value, so anything found is stashed for the next call that has one.
|
|
1323
|
+
const replayed = await replayJournal();
|
|
1324
|
+
if (mls != null && await ensureLedger(Date.now() + recoveryTimeoutMs)) {
|
|
1325
|
+
await finalizeBootstrap(mls);
|
|
1326
|
+
}
|
|
1327
|
+
if (disposed) return;
|
|
1328
|
+
const pulled = await pullCommits();
|
|
1329
|
+
if (!disposed && (replayed || pulled)) await rebuildEpoch();
|
|
1330
|
+
});
|
|
1297
1331
|
}).catch(()=>{
|
|
1298
1332
|
// pull failed (e.g. processCommit threw); the cursor did not advance, so the next wakeup
|
|
1299
1333
|
// re-reads those frames
|
|
@@ -1334,6 +1368,7 @@ export function createGroupPeer(params) {
|
|
|
1334
1368
|
const initControlLanes = async ()=>{
|
|
1335
1369
|
if (mls == null) return;
|
|
1336
1370
|
const recoverySecret = await mls.exportRecoverySecret();
|
|
1371
|
+
if (disposed) return;
|
|
1337
1372
|
commitTopicID = commitTopic(recoverySecret);
|
|
1338
1373
|
rendezvousTopicID = rendezvousTopic(recoverySecret);
|
|
1339
1374
|
// Both topics subscribed once for the peer's whole life — NOT rebuilt on resync, so a
|
|
@@ -1347,11 +1382,18 @@ export function createGroupPeer(params) {
|
|
|
1347
1382
|
// exactly the ones no push will bring it. A lane operation, so the journal replays AHEAD of
|
|
1348
1383
|
// it. Neither step rebuilds the epoch — buildEpoch runs next.
|
|
1349
1384
|
await runSerial(async ()=>{
|
|
1385
|
+
if (disposed) return;
|
|
1350
1386
|
await replayJournal();
|
|
1387
|
+
if (disposed) return;
|
|
1351
1388
|
// A peer restored with an incomplete ledger was killed between rejoining and bootstrapping.
|
|
1352
1389
|
// The invariant finds it here and at every later lane operation, with no memory of how it
|
|
1353
1390
|
// got there.
|
|
1354
|
-
|
|
1391
|
+
const ledgerReady = await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
1392
|
+
if (disposed) return;
|
|
1393
|
+
if (ledgerReady) {
|
|
1394
|
+
await finalizeBootstrap(mls);
|
|
1395
|
+
if (disposed) return;
|
|
1396
|
+
}
|
|
1355
1397
|
await pullCommits();
|
|
1356
1398
|
}).catch(()=>{
|
|
1357
1399
|
// a failed seed leaves the cursor put; the next wakeup replays and pulls again
|
|
@@ -1388,7 +1430,11 @@ export function createGroupPeer(params) {
|
|
|
1388
1430
|
journalReplayed = true;
|
|
1389
1431
|
if (mls == null || journal == null || commitTopicID == null) return false;
|
|
1390
1432
|
const entry = await journal.get();
|
|
1391
|
-
if (entry == null) return false;
|
|
1433
|
+
if (disposed || entry == null) return false;
|
|
1434
|
+
const adoptIfLive = async ()=>{
|
|
1435
|
+
if (disposed) return;
|
|
1436
|
+
await adoptJournalled(entry.journal);
|
|
1437
|
+
};
|
|
1392
1438
|
if (entry.acceptedAs != null) {
|
|
1393
1439
|
// It landed and this peer recorded that before adopting. Nothing to ask: no republish, no
|
|
1394
1440
|
// re-seal, no network. The recorded sequenceID is both the last position processed and the
|
|
@@ -1403,7 +1449,8 @@ export function createGroupPeer(params) {
|
|
|
1403
1449
|
});
|
|
1404
1450
|
// Through the seam: the adopt ratchets the handle, so this epoch's app frames are read
|
|
1405
1451
|
// first and the anchor is taken if the journalled commit moved the roster.
|
|
1406
|
-
await advanceHandle(mls,
|
|
1452
|
+
await advanceHandle(mls, adoptIfLive);
|
|
1453
|
+
if (disposed) return false;
|
|
1407
1454
|
await journal.clear(entry.publishID);
|
|
1408
1455
|
return true;
|
|
1409
1456
|
}
|
|
@@ -1411,10 +1458,12 @@ export function createGroupPeer(params) {
|
|
|
1411
1458
|
// which equals the framed epoch only while `onAccepted` is the sole place the host adopts.
|
|
1412
1459
|
// Sealing anyway publishes a blob no member can open and wedges the lane for the whole group.
|
|
1413
1460
|
const handleEpoch = await mls.readEpoch();
|
|
1461
|
+
if (disposed) return false;
|
|
1414
1462
|
if (handleEpoch !== entry.epoch) {
|
|
1415
1463
|
throw new JournalEpochError(`commit replay: the journalled commit was framed at epoch ${entry.epoch}, and this group is now at ${handleEpoch}. A commit is adopted in onAccepted, and nowhere else.`);
|
|
1416
1464
|
}
|
|
1417
1465
|
const { payload } = await frameCommit(entry.commit, entry.bodies, entry.epoch);
|
|
1466
|
+
if (disposed) return false;
|
|
1418
1467
|
let sequenceID;
|
|
1419
1468
|
try {
|
|
1420
1469
|
sequenceID = (await mux.publish({
|
|
@@ -1424,7 +1473,9 @@ export function createGroupPeer(params) {
|
|
|
1424
1473
|
expectedHead: entry.expectedHead,
|
|
1425
1474
|
publishID: entry.publishID
|
|
1426
1475
|
})).sequenceID;
|
|
1476
|
+
if (disposed) return false;
|
|
1427
1477
|
} catch (error) {
|
|
1478
|
+
if (disposed) return false;
|
|
1428
1479
|
if (!isHeadMismatch(error)) {
|
|
1429
1480
|
// Outcome UNKNOWN — the hub may have accepted and failed to say so. Leave the slot
|
|
1430
1481
|
// exactly as it is: the next lane operation asks again.
|
|
@@ -1434,6 +1485,7 @@ export function createGroupPeer(params) {
|
|
|
1434
1485
|
// again — the process that held it is gone — so hand back what survived and clear the slot:
|
|
1435
1486
|
// the notice must not be lost, never the slot.
|
|
1436
1487
|
await journal.clear(entry.publishID);
|
|
1488
|
+
if (disposed) return false;
|
|
1437
1489
|
lostCommit = entry.kind === 'ledger' ? {
|
|
1438
1490
|
kind: 'ledger',
|
|
1439
1491
|
tokens: entry.bodies,
|
|
@@ -1451,6 +1503,7 @@ export function createGroupPeer(params) {
|
|
|
1451
1503
|
// framed epoch, and a crash between the two would leave a journalled commit indistinguishable
|
|
1452
1504
|
// from a host that adopted out of band. Written first, replay is idempotent.
|
|
1453
1505
|
await journal.markAccepted(entry.publishID, sequenceID);
|
|
1506
|
+
if (disposed) return false;
|
|
1454
1507
|
// This peer's own accepted frame is BOTH the last thing it processed and the log's tip —
|
|
1455
1508
|
// otherwise a `commit()` right after a `replay()` would anchor on a stale tip.
|
|
1456
1509
|
const accepted = asLogPosition(sequenceID);
|
|
@@ -1460,7 +1513,8 @@ export function createGroupPeer(params) {
|
|
|
1460
1513
|
sequenceID: accepted,
|
|
1461
1514
|
digest: digestAppliedCommit(entry.commit)
|
|
1462
1515
|
});
|
|
1463
|
-
await advanceHandle(mls,
|
|
1516
|
+
await advanceHandle(mls, adoptIfLive);
|
|
1517
|
+
if (disposed) return false;
|
|
1464
1518
|
await journal.clear(entry.publishID);
|
|
1465
1519
|
return true;
|
|
1466
1520
|
};
|
|
@@ -1482,15 +1536,17 @@ export function createGroupPeer(params) {
|
|
|
1482
1536
|
};
|
|
1483
1537
|
};
|
|
1484
1538
|
const repairRejoin = async ()=>{
|
|
1485
|
-
if (awaitingBootstrap == null) return;
|
|
1539
|
+
if (disposed || awaitingBootstrap == null) return;
|
|
1486
1540
|
// Restore the subscription before gathering the ledger: responders may already have
|
|
1487
1541
|
// rotated to the new app anchor. A failed repair remains pending for the next lane call.
|
|
1488
1542
|
if (rejoinAnchorNeedsCapture) {
|
|
1489
1543
|
await captureAnchor();
|
|
1544
|
+
if (disposed) return;
|
|
1490
1545
|
rejoinAnchorNeedsCapture = false;
|
|
1491
1546
|
}
|
|
1492
1547
|
if (rejoinRuntimeNeedsBuild) {
|
|
1493
1548
|
await rebuildEpoch();
|
|
1549
|
+
if (disposed) return;
|
|
1494
1550
|
rejoinRuntimeNeedsBuild = false;
|
|
1495
1551
|
}
|
|
1496
1552
|
};
|
|
@@ -1540,9 +1596,12 @@ export function createGroupPeer(params) {
|
|
|
1540
1596
|
*/ const ensureLedger = async (deadline)=>{
|
|
1541
1597
|
if (mls == null || rendezvousTopicID == null) return true;
|
|
1542
1598
|
await repairRejoin();
|
|
1599
|
+
if (disposed) return false;
|
|
1543
1600
|
const port = mls;
|
|
1544
1601
|
const topicID = rendezvousTopicID;
|
|
1545
|
-
|
|
1602
|
+
const complete = await port.isLedgerComplete();
|
|
1603
|
+
if (disposed) return false;
|
|
1604
|
+
if (complete) return true;
|
|
1546
1605
|
// Gather the WHOLE ordered ledger — not "the missing ids", which nothing enumerates: the
|
|
1547
1606
|
// authenticated head is a chain digest, not a list. Every responder with a complete ledger
|
|
1548
1607
|
// answers, each checked against the head this handle carries: a lying responder can withhold,
|
|
@@ -1620,8 +1679,10 @@ export function createGroupPeer(params) {
|
|
|
1620
1679
|
const slot = journal;
|
|
1621
1680
|
const topicID = commitTopicID;
|
|
1622
1681
|
const op = runSerial(async ()=>{
|
|
1682
|
+
assertLive();
|
|
1623
1683
|
// 0. Replay the journal, ahead of the pull.
|
|
1624
1684
|
if (await replayJournal()) await rebuildEpoch();
|
|
1685
|
+
assertLive();
|
|
1625
1686
|
// 0.5. Refuse on an incomplete ledger. A rejoin whose bootstrap never finished leaves a
|
|
1626
1687
|
// reset roster: the fold sees only the genesis creator as admin, every promotion since
|
|
1627
1688
|
// is invisible. `ensureLedger` repairs it in place when a responder answers; when none
|
|
@@ -1633,14 +1694,17 @@ export function createGroupPeer(params) {
|
|
|
1633
1694
|
// would rotate the tree for nothing — the gather that just failed IS the repair,
|
|
1634
1695
|
// re-running at the head of the next lane operation.
|
|
1635
1696
|
if (!await ensureLedger(Date.now() + recoveryTimeoutMs)) {
|
|
1697
|
+
assertLive();
|
|
1636
1698
|
throw new RecoveryRequiredError('commit: the ledger is incomplete, so this handle rejoined the group and its bootstrap has not completed — its roster has reset, and a commit built now would be judged against a group whose admins it cannot see. It must finish bootstrapping its ledger before it can commit again.');
|
|
1637
1699
|
}
|
|
1638
1700
|
await finalizeBootstrap(mls);
|
|
1701
|
+
assertLive();
|
|
1639
1702
|
const deadline = Date.now() + commitDeadlineMs;
|
|
1640
1703
|
for(let attempt = 0; attempt < COMMIT_ATTEMPT_CEILING; attempt++){
|
|
1641
1704
|
// 1. Pull the log to the end: every frame processed, and the tip to race at learned from
|
|
1642
1705
|
// the store's own reply.
|
|
1643
1706
|
await reconcileCommits();
|
|
1707
|
+
assertLive();
|
|
1644
1708
|
// The pull found positive evidence this peer is off the group's line, which stands
|
|
1645
1709
|
// whether or not a following heal lands. Unwind rather than race: on the `ahead` path the
|
|
1646
1710
|
// pull already took the live tip, so a commit here would win at an epoch it never caught
|
|
@@ -1652,6 +1716,7 @@ export function createGroupPeer(params) {
|
|
|
1652
1716
|
// 2. Build against the host's CURRENT handle, adopting nothing. `build` closes over that
|
|
1653
1717
|
// handle, so a rebased retry frames at the rebased epoch.
|
|
1654
1718
|
const pending = await build();
|
|
1719
|
+
assertLive();
|
|
1655
1720
|
// 3. Journal BEFORE publishing, durably: from here to the hub's answer is the crash
|
|
1656
1721
|
// window, and the slot is the only thing that survives it.
|
|
1657
1722
|
//
|
|
@@ -1660,6 +1725,7 @@ export function createGroupPeer(params) {
|
|
|
1660
1725
|
const publishID = newPublishID();
|
|
1661
1726
|
const expectedHead = commitLogHead;
|
|
1662
1727
|
const { payload, epoch: framedEpoch } = await frameCommit(pending.commit, pending.bodies, crypto.frameEpoch(pending.commit));
|
|
1728
|
+
assertLive();
|
|
1663
1729
|
await slot.put({
|
|
1664
1730
|
publishID,
|
|
1665
1731
|
expectedHead,
|
|
@@ -1671,6 +1737,7 @@ export function createGroupPeer(params) {
|
|
|
1671
1737
|
kind: pending.kind,
|
|
1672
1738
|
journal: pending.journal
|
|
1673
1739
|
});
|
|
1740
|
+
assertLive();
|
|
1674
1741
|
// 4. Publish, conditional on the head the pull reached.
|
|
1675
1742
|
let sequenceID;
|
|
1676
1743
|
try {
|
|
@@ -1681,7 +1748,11 @@ export function createGroupPeer(params) {
|
|
|
1681
1748
|
expectedHead,
|
|
1682
1749
|
publishID
|
|
1683
1750
|
})).sequenceID;
|
|
1751
|
+
assertLive();
|
|
1684
1752
|
} catch (error) {
|
|
1753
|
+
if (disposed) throw new PeerDisposedError('Peer is disposed', {
|
|
1754
|
+
cause: error
|
|
1755
|
+
});
|
|
1685
1756
|
if (!isHeadMismatch(error)) {
|
|
1686
1757
|
// Unknown outcome: the frame may be in the log. The slot STAYS — the next lane
|
|
1687
1758
|
// operation replays it and asks the store which it was.
|
|
@@ -1690,6 +1761,7 @@ export function createGroupPeer(params) {
|
|
|
1690
1761
|
// 6. Lost the compare-and-set: someone committed first — expected, not an error. Drop
|
|
1691
1762
|
// the pending commit untouched, clear the slot, and go back to step 1.
|
|
1692
1763
|
await slot.clear(publishID);
|
|
1764
|
+
assertLive();
|
|
1693
1765
|
if (Date.now() >= deadline) {
|
|
1694
1766
|
// The last lost compare-and-set, carried: it is the proximate reason this attempt
|
|
1695
1767
|
// restarted, and the deadline only names how many such losses ran out the clock.
|
|
@@ -1704,6 +1776,7 @@ export function createGroupPeer(params) {
|
|
|
1704
1776
|
// carrying none at a later epoch is a host that adopted outside `onAccepted`. Recorded
|
|
1705
1777
|
// after the adopt, the two would be indistinguishable.
|
|
1706
1778
|
await slot.markAccepted(publishID, sequenceID);
|
|
1779
|
+
assertLive();
|
|
1707
1780
|
// The commit is the group's now — this frame is both the last position processed and the
|
|
1708
1781
|
// log's new tip.
|
|
1709
1782
|
const accepted = asLogPosition(sequenceID);
|
|
@@ -1722,8 +1795,11 @@ export function createGroupPeer(params) {
|
|
|
1722
1795
|
// Add sits on a topic the new member's handle cannot derive — silently, and no restart
|
|
1723
1796
|
// heals it.
|
|
1724
1797
|
await advanceHandle(mls, ()=>pending.onAccepted());
|
|
1798
|
+
assertLive();
|
|
1725
1799
|
await slot.clear(publishID);
|
|
1800
|
+
assertLive();
|
|
1726
1801
|
await rebuildEpoch();
|
|
1802
|
+
assertLive();
|
|
1727
1803
|
return takeLost();
|
|
1728
1804
|
}
|
|
1729
1805
|
throw new CommitDeadlineError(`commit: gave up after ${COMMIT_ATTEMPT_CEILING} attempts inside its deadline`);
|
|
@@ -2055,7 +2131,7 @@ export function createGroupPeer(params) {
|
|
|
2055
2131
|
* flight absorbs any trigger raised while it runs: the frame that raised it is still in the log,
|
|
2056
2132
|
* and the next pull raises it again if the heal did not settle it.
|
|
2057
2133
|
*/ const healIfRequested = async ()=>{
|
|
2058
|
-
if (!healRequested || activeRecovery != null) return;
|
|
2134
|
+
if (disposed || !healRequested || activeRecovery != null) return;
|
|
2059
2135
|
healRequested = false;
|
|
2060
2136
|
try {
|
|
2061
2137
|
await runRecovery('automatic');
|
|
@@ -2106,25 +2182,33 @@ export function createGroupPeer(params) {
|
|
|
2106
2182
|
// at its own add epoch — the same epoch every existing member rotates to on applying that
|
|
2107
2183
|
// add, so the two agree with no exchange between them.
|
|
2108
2184
|
const stored = await anchorStore?.load();
|
|
2185
|
+
if (disposed) return;
|
|
2109
2186
|
if (stored != null) {
|
|
2110
2187
|
anchor = stored;
|
|
2111
2188
|
} else {
|
|
2112
2189
|
await captureAnchor();
|
|
2113
2190
|
}
|
|
2191
|
+
if (disposed) return;
|
|
2114
2192
|
await restorePending();
|
|
2115
2193
|
if (disposed) return;
|
|
2116
2194
|
await initControlLanes();
|
|
2195
|
+
if (disposed) return;
|
|
2117
2196
|
await buildEpoch();
|
|
2197
|
+
if (disposed) return;
|
|
2118
2198
|
// The seed pull precedes the app listeners. Read once more after registration to close
|
|
2119
2199
|
// the publication gap; a failed read is retried on the same independent schedule.
|
|
2120
2200
|
if (crypto.pending != null) {
|
|
2121
2201
|
try {
|
|
2122
2202
|
await runSerial(async ()=>{
|
|
2203
|
+
if (disposed) return;
|
|
2123
2204
|
await replayJournal();
|
|
2205
|
+
if (disposed) return;
|
|
2124
2206
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
2207
|
+
if (disposed) return;
|
|
2125
2208
|
await reconcileCommits();
|
|
2126
2209
|
});
|
|
2127
2210
|
} catch {
|
|
2211
|
+
if (disposed) return;
|
|
2128
2212
|
appPullNeeded = true;
|
|
2129
2213
|
armAppPull(appPullBackoff);
|
|
2130
2214
|
}
|
|
@@ -2194,11 +2278,14 @@ export function createGroupPeer(params) {
|
|
|
2194
2278
|
await ready;
|
|
2195
2279
|
assertLive();
|
|
2196
2280
|
await runSerial(async ()=>{
|
|
2281
|
+
assertLive();
|
|
2197
2282
|
await appLane.dropFrame(topicID, position);
|
|
2283
|
+
assertLive();
|
|
2198
2284
|
await replayJournal();
|
|
2199
2285
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
2200
2286
|
await reconcileCommits();
|
|
2201
2287
|
});
|
|
2288
|
+
assertLive();
|
|
2202
2289
|
},
|
|
2203
2290
|
commit,
|
|
2204
2291
|
replay,
|
|
@@ -2216,6 +2303,7 @@ export function createGroupPeer(params) {
|
|
|
2216
2303
|
// one set of runtimes. Safe to wrap only because `rebuildEpoch` takes no lock itself and
|
|
2217
2304
|
// this is a top-level entry — `runSerial` is not reentrant.
|
|
2218
2305
|
await runSerial(()=>rebuildEpoch());
|
|
2306
|
+
assertLive();
|
|
2219
2307
|
},
|
|
2220
2308
|
anchorEpoch: ()=>anchor.epoch,
|
|
2221
2309
|
reauthorize: ()=>{
|
|
@@ -2246,7 +2334,14 @@ export function createGroupPeer(params) {
|
|
|
2246
2334
|
])finish();
|
|
2247
2335
|
disposePromise = (async ()=>{
|
|
2248
2336
|
// Tear down even a peer whose init failed — it still holds a hub drain.
|
|
2249
|
-
|
|
2337
|
+
// Initialization may be waiting on a host port that never answers. Disposal closes the
|
|
2338
|
+
// mux now; late initialization checks `disposed` before registering more listeners.
|
|
2339
|
+
await Promise.race([
|
|
2340
|
+
settled,
|
|
2341
|
+
pendingRestoreAborted
|
|
2342
|
+
]);
|
|
2343
|
+
// The host owns a bootstrap already touching its handle, including one started by init.
|
|
2344
|
+
// Drain it without waiting for ready, which can itself depend on the seed lane.
|
|
2250
2345
|
await Promise.allSettled([
|
|
2251
2346
|
...inFlightBootstraps
|
|
2252
2347
|
]);
|