From a8d54d7c14197ec00987d4b08864b7403e35688d Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 31 Oct 2024 15:20:09 +0200 Subject: [PATCH] fix(coderd/database/pubsub): prevent listeners read outside mutex lock --- coderd/database/pubsub/pubsub.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coderd/database/pubsub/pubsub.go b/coderd/database/pubsub/pubsub.go index fa4dc8b90b1d0..c0130e3deac04 100644 --- a/coderd/database/pubsub/pubsub.go +++ b/coderd/database/pubsub/pubsub.go @@ -278,10 +278,11 @@ func (p *PGPubsub) subscribeQueue(event string, newQ *msgQueue) (cancel func(), if len(listeners) == 0 { delete(p.queues, event) } + listenerCount := len(listeners) p.qMu.Unlock() // as above, we must not hold the lock while calling into pgListener - if len(listeners) == 0 { + if listenerCount == 0 { uErr := p.pgListener.Unlisten(event) p.closeMu.Lock() defer p.closeMu.Unlock()