Commit 55909f0
fix(ocap-kernel): plug second-round leaks in peer-incarnation restart path
Follow-up review of the issue #944 fix surfaced four real defects, each
fixed here:
1. RemoteHandle.#sendRemoteCommand swallowed PeerRestartedError and
NetworkStoppedError. Only `intentional close` triggered the cleanup
path; the other two terminal verdicts fell through to logger.error
with the message persisted to remotePending and #nextSendSeq
advanced, leaving it pending until ACK timeout × MAX_RETRIES. The
catch now routes all terminal errors through the shared
isTerminalSendError predicate.
2. RemoteManager.#handleIncarnationChange enqueued kernelQueue
notifications and ran in-memory mutations inside the savepoint
window. A kv rollback would not undo either, leaving the kernel
with run-queue entries against promises the persisted store still
considers unresolved. Split RemoteHandle.handlePeerRestart into
`persistPeerRestart` (kv-only, savepoint-safe) and
`finalizePeerRestart` (in-memory + redemption rejections); the
manager now snapshots `getPromisesByDecider` before any kv mutation,
runs `persistPeerRestart` inside the savepoint, and defers
`finalizePeerRestart` and `resolvePromises` to after release.
3. doInboundHandshake silently ignored kernelDetectedRestart. The
outbound path closes the freshly-dialed channel and throws
PeerRestartedError on detected restart; the inbound path only
logged. A concurrent in-flight outbound send could then write a
pre-restart payload over a fresh-incarnation channel. Inbound now
returns false when the kernel detects a restart, and the caller
closes the channel without registering it — symmetric with the
outbound guard.
4. deleteRemoteInfo left peerIncarnation.{peerId} stranded. A
re-established remote with the same peerId would mis-classify its
first handshake as a restart against the leftover incarnation. Now
reads the info before deletion and clears the peerIncarnation row;
corrupt JSON is logged and swallowed so the rest of the cleanup
still runs.
Other tightening:
- #rejectAllPending is a no-op when nothing is pending, so the catch
path is safe even when the kernel-side restart cleanup ran upstream.
- #handleIncarnationChange logs a warning when the restart verdict
fires but no live RemoteHandle exists (boot race / peer teardown
scoping issue), instead of silently advancing the persisted
incarnation.
- Sentinel error matching uses the centralized `isTerminalSendError`
imported from @metamask/kernel-errors (this commit's prerequisite),
removing the brittle string-match in the predicate.
- Outbound PeerRestartedError now logs at info severity, not error —
it is an expected, recoverable event.
Tests:
- New: PeerRestartedError/IntentionalCloseError/NetworkStoppedError
from initial #sendRemoteCommand reject pending and fire onGiveUp.
- New: handlePeerRestart actually clears c-list state (seeds an
exportFromEndpoint pair, asserts both halves gone after restart).
- New: PeerRestartedError does not trigger reconnection from the
outbound catch path.
- New: inbound handshake closes the channel without registration when
the kernel detects a restart.
- New: deleteRemoteInfo clears the peer-incarnation row.
- Updated: RemoteManager savepoint-rollback test now spies on
persistPeerRestart and asserts finalizePeerRestart is NOT called
when the persisted phase throws.
- Updated: #handleIncarnationChange verdict tests assert false on
first observation and stable incarnation (not just true on restart).
E2E: extension/test/e2e/remote-comms.test.ts had its toBeVisible
budget raised to 50s (suite to 90s) to fit inside the 40s URL
redemption budget that the new awaited handshake RPC pushes the
round-trip into under CI load.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent bafe273 commit 55909f0
9 files changed
Lines changed: 375 additions & 217 deletions
File tree
- packages
- extension/test/e2e
- ocap-kernel/src
- remotes
- kernel
- platform
- store/methods
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
144 | 148 | | |
145 | 149 | | |
146 | 150 | | |
| |||
Lines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1674 | 1674 | | |
1675 | 1675 | | |
1676 | 1676 | | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
| 1728 | + | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
1677 | 1752 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | 38 | | |
65 | 39 | | |
66 | 40 | | |
| |||
448 | 422 | | |
449 | 423 | | |
450 | 424 | | |
451 | | - | |
| 425 | + | |
452 | 426 | | |
453 | 427 | | |
454 | 428 | | |
| |||
495 | 469 | | |
496 | 470 | | |
497 | 471 | | |
498 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
499 | 475 | | |
500 | 476 | | |
501 | 477 | | |
502 | 478 | | |
503 | 479 | | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
504 | 483 | | |
505 | 484 | | |
506 | 485 | | |
| |||
635 | 614 | | |
636 | 615 | | |
637 | 616 | | |
638 | | - | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
639 | 630 | | |
640 | 631 | | |
641 | 632 | | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
| 633 | + | |
| 634 | + | |
647 | 635 | | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
| 636 | + | |
| 637 | + | |
653 | 638 | | |
654 | 639 | | |
655 | 640 | | |
656 | | - | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
657 | 645 | | |
658 | 646 | | |
659 | 647 | | |
| |||
1164 | 1152 | | |
1165 | 1153 | | |
1166 | 1154 | | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
1172 | | - | |
1173 | | - | |
1174 | | - | |
1175 | | - | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
1176 | 1159 | | |
1177 | | - | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
1178 | 1166 | | |
1179 | | - | |
| 1167 | + | |
1180 | 1168 | | |
1181 | 1169 | | |
1182 | 1170 | | |
1183 | | - | |
1184 | | - | |
1185 | | - | |
1186 | | - | |
1187 | | - | |
1188 | | - | |
1189 | | - | |
1190 | | - | |
1191 | | - | |
1192 | 1171 | | |
1193 | | - | |
1194 | | - | |
1195 | | - | |
1196 | | - | |
1197 | | - | |
1198 | | - | |
1199 | | - | |
1200 | | - | |
1201 | | - | |
1202 | 1172 | | |
| 1173 | + | |
1203 | 1174 | | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
1209 | 1187 | | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
1215 | 1197 | | |
1216 | 1198 | | |
1217 | 1199 | | |
1218 | 1200 | | |
| 1201 | + | |
| 1202 | + | |
1219 | 1203 | | |
1220 | | - | |
1221 | | - | |
1222 | 1204 | | |
1223 | 1205 | | |
1224 | 1206 | | |
| |||
0 commit comments