Skip to content

Commit

Permalink
fix(notify): Fixed race condition in listener. (#1963)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elliotcourant authored Oct 8, 2022
1 parent 8f360bc commit f16c44b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ func (ln *Listener) listen(ctx context.Context, cn *pool.Conn, channels ...strin
func (ln *Listener) Unlisten(ctx context.Context, channels ...string) error {
ln.mu.Lock()
ln.channels = removeIfExists(ln.channels, channels...)
ln.mu.Unlock()

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

0 comments on commit f16c44b

Please sign in to comment.