From ea58e76332cbe854ab3b812678bbf68165795908 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Thu, 29 Sep 2022 07:02:58 -0500 Subject: [PATCH] worker: do not lock while callbacks are running Commit 716ade896871 ("worker: lock access to callback maps") introduced locks to the worker callback maps. The locks also locked the processing of the callback, which had the unintended side effect of deadlocking the worker if any callbacks attempted to post a new action or message. Refactor the locks to only lock the worker while accessing the maps. Fixes: 716ade896871 ("worker: lock access to callback maps") Signed-off-by: Tim Culverhouse Acked-by: Robin Jarry --- worker/types/worker.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/worker/types/worker.go b/worker/types/worker.go index 61b96da..ee2f9a3 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -87,13 +87,16 @@ func (worker *Worker) ProcessMessage(msg WorkerMessage) WorkerMessage { } if inResponseTo := msg.InResponseTo(); inResponseTo != nil { worker.Lock() - if f, ok := worker.actionCallbacks[inResponseTo.getId()]; ok { + f, ok := worker.actionCallbacks[inResponseTo.getId()] + worker.Unlock() + if ok { f(msg) if _, ok := msg.(*Done); ok { + worker.Lock() delete(worker.actionCallbacks, inResponseTo.getId()) + worker.Unlock() } } - worker.Unlock() } return msg } @@ -106,13 +109,16 @@ func (worker *Worker) ProcessAction(msg WorkerMessage) WorkerMessage { } if inResponseTo := msg.InResponseTo(); inResponseTo != nil { worker.Lock() - if f, ok := worker.messageCallbacks[inResponseTo.getId()]; ok { + f, ok := worker.messageCallbacks[inResponseTo.getId()] + worker.Unlock() + if ok { f(msg) if _, ok := msg.(*Done); ok { + worker.Lock() delete(worker.messageCallbacks, inResponseTo.getId()) + worker.Unlock() } } - worker.Unlock() } return msg }