Skip to content

Conversation

@sanity
Copy link
Collaborator

@sanity sanity commented Oct 26, 2025

Problem

Nodes were experiencing unbounded growth in pending connection targets, with pending_adds increasing indefinitely (e.g., 2831 → 2855 → 2879 → 2903 → 2927) even when unable to establish new connections.

From the logs:

INFO freenet::ring: Topology adjustment result, ... current_connections: 1, pending_adds: 2831
INFO freenet::ring: Topology adjustment result, ... current_connections: 1, pending_adds: 2855
INFO freenet::ring: Topology adjustment result, ... current_connections: 1, pending_adds: 2879

This represents a memory leak where the pending_conn_adds BTreeSet grows without bound.

Root Cause

The connection maintenance loop:

  1. Can only process one pending connection at a time (gated by live_tx)
  2. Topology adjustment adds ~24 new locations per iteration
  3. No limit was enforced on pending additions based on existing backlog
  4. Result: backlog grows faster than it can be processed

Solution

Added calculate_allowed_connection_additions() function that:

  • Considers both current and pending connections as "effective connections"
  • Enforces max_connections limit based on effective connections
  • When below min_connections, limits additions to deficit needed
  • Prevents backlog from satisfying the entire connection capacity

Example Scenarios

Healthy state (1 connection, 0 pending, need 25 min):

  • Allows 24 additions to reach minimum

Backlog exists (1 connection, 24 pending, need 25 min):

  • Blocks new additions since backlog will satisfy minimum
  • Allows system to process existing backlog

At capacity (1 connection, 2831 pending, max 200):

  • Blocks all new additions (effective = 2832 > max = 200)
  • Prevents unbounded growth

Testing

  • Added 4 unit tests covering key scenarios
  • All existing ring tests pass
  • Validates backlog is respected when calculating capacity

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Copy link
Collaborator Author

@sanity sanity left a comment

Choose a reason for hiding this comment

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

[Codex] Change looks solid to me—thanks for capping the backlog and adding the targeted tests to guard the helper.

@sanity sanity added this pull request to the merge queue Oct 26, 2025
@sanity sanity removed this pull request from the merge queue due to a manual request Oct 26, 2025
@sanity sanity added this pull request to the merge queue Oct 26, 2025
Merged via the queue into main with commit cc33eb5 Oct 26, 2025
11 checks passed
@sanity sanity deleted the fix/pending-connection-backlog branch October 26, 2025 22:30
@sanity sanity mentioned this pull request Oct 26, 2025
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.

2 participants