Skip to content

fix: per-IP connection counter leak blocking reconnection#70

Merged
0xZunia merged 1 commit intomainfrom
fix/per-ip-counter-leak
Feb 23, 2026
Merged

fix: per-IP connection counter leak blocking reconnection#70
0xZunia merged 1 commit intomainfrom
fix/per-ip-counter-leak

Conversation

@0xZunia
Copy link
Contributor

@0xZunia 0xZunia commented Feb 23, 2026

Summary

Fixes a bug where validators permanently lose the ability to reconnect to peers after ~3 disconnect/reconnect cycles, due to the per-IP connection counter never being decremented.

Two leaks in TcpTransport:

  1. UpdatePeerId (main cause): On duplicate connection detection (simultaneous inbound+outbound), _peerIpMap.TryRemove(realId, ...) destroyed the kept connection's IP mapping. When that connection later disconnected, RemoveConnection couldn't find the IP to decrement the counter. Each cycle leaked +1, hitting the limit of 3 after ~3 cycles.

  2. AcceptLoopAsync (secondary): If TryAdd failed for a duplicate temp ID, the already-incremented per-IP counter was never decremented.

Fix

  • UpdatePeerId: Transfer IP mapping to realId only after successful TryAdd. On duplicate path, only decrement the counter for the disposed connection — don't touch _peerIpMap[realId].
  • AcceptLoopAsync: Decrement per-IP counter when TryAdd fails before disposing the connection.

Test plan

  • dotnet test — 110 network tests passing
  • Verified logic: counter increments and decrements are now balanced across all code paths (connect, accept, update, duplicate, remove)

…ailure

Two bugs caused the per-IP counter to never decrement, permanently blocking
reconnection after ~3 disconnect/reconnect cycles:

1. UpdatePeerId: on duplicate detection, _peerIpMap[realId] was removed,
   destroying the kept connection's IP mapping. When the kept connection
   later disconnected, RemoveConnection could not find the IP to decrement.
   Fix: don't touch _peerIpMap[realId] in the duplicate path.

2. AcceptLoopAsync: if TryAdd failed for a duplicate tempId, the per-IP
   counter (already incremented) was never decremented.
   Fix: decrement before disposing.
@0xZunia 0xZunia merged commit d011e34 into main Feb 23, 2026
4 checks passed
@0xZunia 0xZunia deleted the fix/per-ip-counter-leak branch February 23, 2026 13:55
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.

1 participant