From 2d08fa524019bb2298428074097a7242116fba24 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 4 Mar 2025 12:41:07 +0300 Subject: [PATCH] [#339] pool/tree: Close replaced connection in client map There is a race condition: multiple clients are created and dialled, but only one is stored in the map. Others are remaining active but not used. With this change, new connection replaces old connection and closes it. Signed-off-by: Alex Vanin --- pool/tree/pool.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pool/tree/pool.go b/pool/tree/pool.go index 60c205f..7b1afef 100644 --- a/pool/tree/pool.go +++ b/pool/tree/pool.go @@ -1012,7 +1012,7 @@ LOOP: continue } - p.addClientToMap(cnrNode.Hash(), treeCl) + treeCl = p.addClientToMap(cnrNode.Hash(), treeCl) } attempts-- @@ -1047,10 +1047,16 @@ func (p *Pool) getClientFromMap(hash uint64) (client, bool) { return cl, ok } -func (p *Pool) addClientToMap(hash uint64, cl client) { +func (p *Pool) addClientToMap(hash uint64, cl client) client { p.mutex.Lock() + defer p.mutex.Unlock() + + if old, ok := p.clientMap[hash]; ok { + _ = cl.close() + return old + } p.clientMap[hash] = cl - p.mutex.Unlock() + return cl } func (p *Pool) deleteClientFromMap(hash uint64) {