celestia-core icon indicating copy to clipboard operation
celestia-core copied to clipboard

fix: remove CAT mempool panic for duplicate peers (#1902 backport to main)

Open Copilot opened this issue 10 months ago • 0 comments

The CAT (Content Addressable Transaction) mempool was panicking when attempting to add a duplicate peer, which could occur during rapid peer removal and addition cycles under heavy network load.

Problem

The ReserveForPeer function in mempool/cat/peers.go would panic with the message "duplicate peer added to mempool" when a peer that was already present tried to be added again. This panic could crash the node in scenarios where peers are quickly disconnected and reconnected.

Solution

Modified the ReserveForPeer function to handle duplicate peer additions gracefully by making the operation idempotent:

  • Before: panic("duplicate peer added to mempool")
  • After: Return early if the peer already exists (no-op)

This approach is consistent with the priority mempool implementation and prevents crashes while maintaining the same functional behavior.

Changes

  1. mempool/cat/peers.go: Changed panic to early return in ReserveForPeer
  2. mempool/cat/peers_test.go: Updated tests to verify graceful handling and added concurrent safety test

Testing

  • Updated existing TestPeerLifecycle to verify no panic occurs on duplicate additions
  • Added TestPeerConcurrentReservation to test concurrent peer reservations with 20 goroutines
  • All existing CAT mempool tests continue to pass

The fix ensures that rapid peer connection changes under heavy load won't crash the node due to timing issues in peer management.

Fixes #2060.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Jun 24 '25 15:06 Copilot