Commit b2dff9c
fix: remove peers from reactors when stopAndRemovePeer called with nil reason (#2688)
## Description
When `stopAndRemovePeer` is called with `reason=nil` (during `OnStop()`
or `StopPeerGracefully()`), peers were removed from the switch but not
from individual reactors, leaving stale references.
**Changes:**
- Modified `stopAndRemovePeer` to default `reason` to `"stopping"` when
nil
- Removed conditional check that skipped `removePeerFromAllReactors`
when `reason=nil`
- Added test verifying peers are removed from reactor peer sets even
with nil reason
```go
func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
// ...
if reason == nil {
reason = "stopping"
}
schema.WritePeerUpdate(sw.traceClient, string(peer.ID()), schema.PeerDisconnect, fmt.Sprintf("%v", reason))
sw.removePeerFromAllReactors(peer, reason) // Now always called
// ...
}
```
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `ya.ru`
> - Triggering command: `/tmp/go-build3042114909/b001/p2p.test
/tmp/go-build3042114909/b001/p2p.test
-test.testlogfile=/tmp/go-build3042114909/b001/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true` (dns block)
> - Triggering command: `/tmp/go-build3136998786/b001/p2p.test
/tmp/go-build3136998786/b001/p2p.test
-test.testlogfile=/tmp/go-build3136998786/b001/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true` (dns block)
> - Triggering command: `/tmp/go-build491487171/b001/p2p.test
/tmp/go-build491487171/b001/p2p.test
-test.testlogfile=/tmp/go-build491487171/b001/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/celestiaorg/celestia-core/settings/copilot/coding_agent)
(admins only)
>
> </details>
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Zombie peers in the reactors</issue_title>
> <issue_description>I noticed that when cycling through many peers, we
would try to add a peer to a reactor however the peer was already in the
reactor.
>
> This means that inherently there is an issue with the peer being
removed from the switch but not the reactor. I haven't dug into it
thoroughly, however it might be that when removing peers with reason ==
nil (I don't know where nil is being given as a reason) we remove the
peers from the switch but skip the reactors.
>
> We could instead simply add a noop reason for now, and long term get
rid of reasons.</issue_description>
>
> <agent_instructions>basically change the code to this
>
> func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
> sw.transport.Cleanup(peer)
> if err := peer.Stop(); err != nil {
> sw.Logger.Error("error while stopping peer", "error", err) // TODO:
should return error to be handled accordingly
> }
> if reason == nil {
> reason = "stopping"
> }
> schema.WritePeerUpdate(sw.traceClient, string(peer.ID()),
schema.PeerDisconnect, fmt.Sprintf("%v", reason))
> sw.removePeerFromAllReactors(peer, reason)</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes #2687
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 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](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: evan-forbes <[email protected]>
Co-authored-by: evan-forbes <[email protected]>1 parent b5d759d commit b2dff9c
2 files changed
+57
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
422 | | - | |
423 | | - | |
424 | | - | |
| 422 | + | |
| 423 | + | |
425 | 424 | | |
| 425 | + | |
| 426 | + | |
426 | 427 | | |
427 | 428 | | |
428 | 429 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
841 | 841 | | |
842 | 842 | | |
843 | 843 | | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
844 | 897 | | |
845 | 898 | | |
846 | 899 | | |
| |||
0 commit comments