Skip to content

fix(gossipsub): prune empty topic entries from topics map#3405

Merged
dozyio merged 5 commits into
libp2p:mainfrom
lodekeeper:fix/gossipsub-empty-topic-prune
May 16, 2026
Merged

fix(gossipsub): prune empty topic entries from topics map#3405
dozyio merged 5 commits into
libp2p:mainfrom
lodekeeper:fix/gossipsub-empty-topic-prune

Conversation

@lodekeeper
Copy link
Copy Markdown
Contributor

Summary

Monorepo port of ChainSafe/js-libp2p-gossipsub#543.

This carries over the empty-topic pruning fix for the internal topics: Map<TopicStr, Set<PeerIdStr>> bookkeeping:

  • prune empty topics entries on remote unsubscribe
  • prune empty topics entries on peer disconnect
  • avoid creating empty topics entries from unsubscribe-only updates
  • add regression coverage for the above cases

Original standalone PR / discussion: ChainSafe/js-libp2p-gossipsub#543
Original issue context: ChainSafe/js-libp2p-gossipsub#542

Why reopen here?

wemeetagain asked for this to be reopened against the js-libp2p monorepo because js-libp2p-gossipsub will soon be archived.

Notes

This is intentionally scoped to topics only. I did not fold in any mesh cleanup changes here because mesh tracks local joined-topic state and deleting empty mesh topics on disconnect would interfere with heartbeat maintenance/regrafting.

Testing

  • npm exec --workspace packages/gossipsub -- aegir lint
  • focused gossip.spec.ts run currently trips pre-existing monorepo package type/build issues in unrelated test/utils/* files; this patch itself is limited to packages/gossipsub/src/gossipsub.ts and packages/gossipsub/test/gossip.spec.ts

AI disclosure

Initial heap analysis and patch drafting were assisted by AI tooling, then validated with local source inspection and review.

@dozyio
Copy link
Copy Markdown
Collaborator

dozyio commented Apr 6, 2026

moved to blocked - waiting on remaining skipped gossipsub tests

Copy link
Copy Markdown
Member

@tabcat tabcat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lodekeeper! nice find!

Copy link
Copy Markdown
Member

@tabcat tabcat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lodekeeper just clear the topics in stop() and i think we are good.

await Promise.all(closePromises)

this.peers.clear()
this.subscriptions.clear()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets clear this.topics too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in fb480b5c4this.topics.clear() placed right after this.subscriptions.clear(), grouped with the other per-session map resets.

Per tabcat review (libp2p#3405): also reset the topics map alongside the
other per-session state in stop() so a restart begins with an empty
topic→peer index.

🤖 Generated with AI assistance
@lodekeeper
Copy link
Copy Markdown
Contributor Author

@tabcat addressed in fb480b5c4this.topics.clear() added in stop() next to the existing peers / subscriptions resets.

@dozyio dozyio merged commit 2514c01 into libp2p:main May 16, 2026
59 of 62 checks passed
@tabcat tabcat mentioned this pull request May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants