From 9b93d43ac6d91dda29fc17224ed2e25b169eca5b Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Mon, 23 Dec 2019 16:27:24 +0300 Subject: [PATCH] -(home): fix deadlock in clients --- home/clients.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/home/clients.go b/home/clients.go index ff53c6d4..64d67e86 100644 --- a/home/clients.go +++ b/home/clients.go @@ -185,14 +185,19 @@ func (clients *clientsContainer) Exists(ip string, source clientSource) bool { // Find searches for a client by IP func (clients *clientsContainer) Find(ip string) (Client, bool) { + clients.lock.Lock() + defer clients.lock.Unlock() + + return clients.findByIP(ip) +} + +// Find searches for a client by IP (and does not lock anything) +func (clients *clientsContainer) findByIP(ip string) (Client, bool) { ipAddr := net.ParseIP(ip) if ipAddr == nil { return Client{}, false } - clients.lock.Lock() - defer clients.lock.Unlock() - c, ok := clients.idIndex[ip] if ok { return *c, true @@ -456,7 +461,7 @@ func (clients *clientsContainer) AddHost(ip, host string, source clientSource) ( defer clients.lock.Unlock() // check existing clients first - _, ok := clients.Find(ip) + _, ok := clients.findByIP(ip) if ok { return false, nil }