Skip to content

Commit

Permalink
fix infinite discovery address leak (#37)
Browse files Browse the repository at this point in the history
* fix infinite discovery address leak

* fix race in tests
  • Loading branch information
gartnera authored Nov 15, 2024
1 parent 12f139f commit 8535262
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 1 addition & 2 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -85,7 +84,7 @@ func (pd *PeerDiscovery) addPeer(pinfo peer.AddrInfo) {
oldPinfo, ok := pd.knownPeers[pinfo.ID]
if ok {
for _, addr := range pinfo.Addrs {
if !slices.Contains(oldPinfo.Addrs, addr) {
if !multiaddr.Contains(oldPinfo.Addrs, addr) {
oldPinfo.Addrs = append(oldPinfo.Addrs, addr)
}
}
Expand Down
7 changes: 7 additions & 0 deletions p2p/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ func TestDiscovery(t *testing.T) {
assert.Equal(t, 4, len(comm2.host.Peerstore().Peers()))
assert.Equal(t, 4, len(comm3.host.Peerstore().Peers()))
assert.Equal(t, 4, len(comm4.host.Peerstore().Peers()))

comm.discovery.mu.Lock()
assert.Equal(t, 3, len(comm.discovery.knownPeers))
for peer, knownPeers := range comm.discovery.knownPeers {
assert.LessOrEqual(t, len(knownPeers.Addrs), 4, "%s has more than 4 addresses (%d)?", peer.String(), len(knownPeers.Addrs))
}
comm.discovery.mu.Unlock()
}

0 comments on commit 8535262

Please sign in to comment.