Skip to content

Commit

Permalink
bug: fix lock on pool watcher Unregister
Browse files Browse the repository at this point in the history
Fix was provided by @oleg-jukovec

Part of #214
  • Loading branch information
DifferentialOrange committed Dec 21, 2022
1 parent f903d44 commit 2faaa7d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions connection_pool/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ func (c *watcherContainer) add(watcher *poolWatcher) {
}

// remove removes a watcher from the container.
func (c *watcherContainer) remove(watcher *poolWatcher) {
func (c *watcherContainer) remove(watcher *poolWatcher) bool {
c.mutex.Lock()
defer c.mutex.Unlock()

if watcher == c.head {
c.head = watcher.next
return true
} else {
cur := c.head
for cur.next != nil {
if cur.next == watcher {
cur.next = watcher.next
break
return true
}
cur = cur.next
}
}
return false
}

// foreach iterates over the container to the end or until the call returns
Expand Down Expand Up @@ -83,15 +85,13 @@ type poolWatcher struct {

// Unregister unregisters the pool watcher.
func (w *poolWatcher) Unregister() {
w.mutex.Lock()
defer w.mutex.Unlock()

if !w.unregistered {
w.container.remove(w)
if !w.unregistered && w.container.remove(w) {
w.mutex.Lock()
w.unregistered = true
for _, watcher := range w.watchers {
watcher.Unregister()
}
w.mutex.Unlock()
}
}

Expand Down

0 comments on commit 2faaa7d

Please sign in to comment.