Skip to content

fix: clean up adaptive timeout signals in connection monitor#3392

Merged
dozyio merged 1 commit intolibp2p:mainfrom
lodekeeper:fix/connection-monitor-timeout-signal-cleanup
Mar 3, 2026
Merged

fix: clean up adaptive timeout signals in connection monitor#3392
dozyio merged 1 commit intolibp2p:mainfrom
lodekeeper:fix/connection-monitor-timeout-signal-cleanup

Conversation

@lodekeeper
Copy link
Contributor

Context

Fixes a steady memory growth path in heartbeat pinging.

Related issue: #3391

What this changes

1) Always clean up connection-monitor timeout signals

ConnectionMonitor now wraps each heartbeat ping attempt with:

  • const signal = this.timeout.getTimeoutSignal(...)
  • finally { this.timeout.cleanUp(signal) }

This ensures cleanup runs on success, timeout, and error paths.

2) Ensure AdaptiveTimeout.cleanUp releases composed signal listeners

AdaptiveTimeout.cleanUp(signal) now calls:

signal.clear()

before computing/recording timing stats.

This detaches listener chains created by anySignal(...)/AbortSignal.timeout(...).

Tests

Added tests:

  • packages/utils/test/adaptive-timeout.spec.ts
    • verifies cleanUp calls signal.clear()
  • packages/libp2p/test/connection-monitor/index.spec.ts
    • verifies ConnectionMonitor invokes timeout cleanup during heartbeat

Notes

This patch was validated downstream with a long A/B soak where unpatched nodes grew ~2 MB/h faster than patched under identical conditions.

@dozyio dozyio merged commit f4b950d into libp2p:main Mar 3, 2026
47 of 48 checks passed
@tabcat tabcat mentioned this pull request Mar 3, 2026
@dozyio
Copy link
Collaborator

dozyio commented Mar 3, 2026

Thanks @lodekeeper !

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