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()