Skip to content

Commit f16c44b

Browse files
fix(notify): Fixed race condition in listener. (#1963)
This resolves a race condition in listener where `ln.closed` was being read without a mutex at the same time it could be written. Just move the mutex unlock for `Unlisten` down, **but not defer it** so that the mutex still holds the lock properly. But does not cause a deadlock with release conn.
1 parent 8f360bc commit f16c44b

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

listener.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ func (ln *Listener) listen(ctx context.Context, cn *pool.Conn, channels ...strin
185185
func (ln *Listener) Unlisten(ctx context.Context, channels ...string) error {
186186
ln.mu.Lock()
187187
ln.channels = removeIfExists(ln.channels, channels...)
188-
ln.mu.Unlock()
189188

190189
cn, err := ln.conn(ctx)
190+
// I don't want to defer this unlock as the mutex is re-acquired in the `.releaseConn` function. But it is safe to
191+
// unlock here regardless of an error.
192+
ln.mu.Unlock()
191193
if err != nil {
192194
return err
193195
}

0 commit comments

Comments
 (0)