From 37350dbb1ef22ec7343d62a5b5851881e159ac94 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 29 Jan 2025 19:57:29 +0300 Subject: [PATCH] [#326] pool: Fix panic that causes mutex deadlock Two concurrent 'deleteClientFromMap' calls for the same client may produce panic and deadlock. First goroutine acquires lock, removes element from the map, releases lock. Second goroutine acquires lock, and throws panic while trying to call 'close()' on empty struct. Lock is never released and it causes deadlock for other goroutines. Signed-off-by: Alex Vanin --- pool/tree/pool.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pool/tree/pool.go b/pool/tree/pool.go index ddfdc0e..b77d63a 100644 --- a/pool/tree/pool.go +++ b/pool/tree/pool.go @@ -1009,8 +1009,10 @@ func (p *Pool) addClientToMap(hash uint64, cl client) { func (p *Pool) deleteClientFromMap(hash uint64) { p.mutex.Lock() - _ = p.clientMap[hash].close() - delete(p.clientMap, hash) + if cli, ok := p.clientMap[hash]; ok { + _ = cli.close() + delete(p.clientMap, hash) + } p.mutex.Unlock() }