diff --git a/share/p2p/peers/manager.go b/share/p2p/peers/manager.go index caef242eec..4935db2774 100644 --- a/share/p2p/peers/manager.go +++ b/share/p2p/peers/manager.go @@ -7,7 +7,6 @@ import ( "sync" "sync/atomic" "time" - "unsafe" logging "github.com/ipfs/go-log/v2" pubsub "github.com/libp2p/go-libp2p-pubsub" @@ -518,9 +517,7 @@ func (m *Manager) markPoolAsSynced(datahash string) { p := m.getOrCreatePool(datahash) if p.isSynced.CompareAndSwap(false, true) { p.isSynced.Store(true) - old := (*unsafe.Pointer)(unsafe.Pointer(&p.pool)) - // release pointer to old pool to free up memory - atomic.StorePointer(old, unsafe.Pointer(newPool(time.Second))) + p.reset() } } diff --git a/share/p2p/peers/pool.go b/share/p2p/peers/pool.go index c43bbc963b..0166042380 100644 --- a/share/p2p/peers/pool.go +++ b/share/p2p/peers/pool.go @@ -221,3 +221,11 @@ func (p *pool) len() int { defer p.m.RUnlock() return p.activeCount } + +// reset will reset the pool to its initial state. +func (p *pool) reset() { + p.m.Lock() + defer p.m.Unlock() + // swap the pool with an empty one + *p = *newPool(time.Second) +}