fix(gossip): stop membership tracker ticker on channel shutdown #5364
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bug Description:
A time.Ticker is created when initializing the membership tracker, but only the ticker’s channel is retained.
During shutdown, other resources are cleaned up, however the ticker itself is never stopped, violating Go’s ticker lifecycle requirements.
Need for Change:
-The gossip channel creates a time.Ticker for membership tracking but does not stop it when the channel is shut down.
This leads to a resource leak where ticker goroutines continue running after the channel lifecycle has ended.
-In long-running peers or environments where gossip channels are frequently started and stopped, this can accumulate and cause unnecessary memory and CPU usage.
Summary of Fix:
-This PR ensures proper cleanup of the membership tracker ticker by:
-Storing a reference to the created time.Ticker in the membership tracker
-Explicitly stopping the ticker during gossip channel shutdown
-Adding a nil check to safely handle cleanup
The fix is minimal and does not alter any existing behavior beyond proper resource management.
Impact:
-Prevents goroutine and memory leaks
-Improves stability of long-running peers
-Ensures correct lifecycle management of background timers
-Particularly important when multiple gossip channels are created over time
Verification:
-Existing gossip channel tests pass

-No linter or build errors
-Ticker is now correctly stopped during shutdown
-The issue was reproduced using a small standalone program that repeatedly starts and stops gossip channels and compares -goroutine counts before and after shutdown. A terminal screenshot demonstrating this behavior is attached.
A dedicated unit test was not added to avoid flaky goroutine-count assertions in CI environments.