From 14b892281610e82aabb0dd9c6011da8acad8580b Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 6 Oct 2023 12:18:09 +0400 Subject: [PATCH] use mutex for peer-manager pool cleanup --- share/p2p/peers/manager.go | 5 +---- share/p2p/peers/pool.go | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) 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..c8c00b18ba 100644 --- a/share/p2p/peers/pool.go +++ b/share/p2p/peers/pool.go @@ -221,3 +221,10 @@ func (p *pool) len() int { defer p.m.RUnlock() return p.activeCount } + +func (p *pool) reset() { + p.m.Lock() + defer p.m.Unlock() + // swap the pool with an empty one + *p = *newPool(time.Second) +}