fix(pubsub): disconnect hygiene and once-per-subscription mcache replay - #1407
Open
gmelodie wants to merge 1 commit into
Open
fix(pubsub): disconnect hygiene and once-per-subscription mcache replay#1407gmelodie wants to merge 1 commit into
gmelodie wants to merge 1 commit into
Conversation
gmelodie
force-pushed
the
fix/pubsub/mcache-follow-ups
branch
from
July 23, 2026 16:53
0704bc2 to
579ff07
Compare
gmelodie
marked this pull request as ready for review
July 23, 2026 16:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong?
Issue #1403
Follow-ups from the review of #1398:
_handle_dead_peerreturned too early. A peer's subscriptions arrive on its inbound stream, so it can sit inpeer_topicswhile the outbound stream was never registered inpeers. Theif peer_id not in self.peers: returnguard ran before thepeer_topicscleanup, so a peer that dropped while half-registered left stale subscription entries behind forever._send_recent_messages_to_new_peerreplayed the whole mcache window for every topic the peer appeared subscribed to, with no equivalent ofhandle_subscription'swas_newly_addedguard. Combined with the stale entries above, a messy disconnect + reconnect could hand a peer the full window again; the only thing saving us was receiver-side seen-cache dedup.hasattrprobes.pubsub.pyreached into the router with fourhasattr(self.router, ...)checks plus# type: ignore[attr-defined]on every call, so the optional hooks were invisible to type checkers and to anyone implementing a router.How was it fixed?
Disconnect hygiene —
_handle_dead_peernow runs_clear_pending_announce_retries_for_peerand a new_forget_peer_subscriptionsbefore thepeersearly return. Stream teardown, queue close, androuter.remove_peerstill run only for fully-registered peers, since router registration happens in lockstep withself.peers(noawaitbetweenrouter.add_peerandself.peers[peer_id] = stream)._forget_peer_subscriptionsalso replaces the open-coded loop in_teardown_if_connected, so the blacklist path gets the same cleanup.Sender-side replay gating — all mcache replay now goes through one method,
_replay_recent_messages(peer_id, topic), called by both_handle_new_peerandhandle_subscription. It records(peer, topic)in_replayed_recent_topicsso a peer receives the window at most once per subscription. Two details matter:peer_id in self.peers). Marking it while the outbound stream is still pending would make the later_handle_new_peerreplay a no-op and reintroduce the exact bug fix(pubsub): replay recent messages once a peer's outbound stream is registered #1398 fixed._handle_dead_peer, and on blacklist, so a genuine resubscribe or reconnect replays again.Router interface —
IPubsubRouterdeclaresflush_pending_messagesandsend_recent_messagesas non-abstract async hooks with no-op bodies. GossipSub's existing implementations become real overrides, FloodSub inherits the no-ops, and all fourhasattrprobes and theirtype: ignorecomments are gone.Not in this PR: the
GossipSub.publishqueueing item from the issue. It is filed there as a separate design — peers known only via in-flight connection setup are in neitherpeersnorpeer_topicsat publish time, and changing when publish queues speculatively affects every pubsub user.Tests
TestRecentMessageReplayGate(4 tests): replay runs once per subscription; is deferred but not spent before the peer is writable; runs again after unsubscribe/resubscribe; runs again after disconnect/reconnect.test_dead_peer_clears_subscriptions_of_half_registered_peer— a peer inpeer_topicsbut never inpeersis cleaned up.test_router_replay_hooks_default_to_noop— FloodSub is callable through the new interface hooks.test_publish_before_identify_with_subscription_before_streamnow waits onwait_for_peerinstead oftrio.sleep(publisher_stream_delay + 4), as the issue suggested. Runtime drops from ~7s to ~1.4s.tests/core/pubsubis green (408 passed), as areruff check,ruff format --checkon the touched files,mypy -p libp2p, andpyrefly check.To-Do
IPubsubRouterhooks and_replay_recent_messages; no user-facing docs describe these internals1403.bugfix.rstand1403.internal.rstCute Animal Picture