fix: remove CAT mempool panic for duplicate peers (#1902 backport to main)
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
-
mempool/cat/peers.go: Changed panic to early return inReserveForPeer -
mempool/cat/peers_test.go: Updated tests to verify graceful handling and added concurrent safety test
Testing
- Updated existing
TestPeerLifecycleto verify no panic occurs on duplicate additions - Added
TestPeerConcurrentReservationto 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.