Skip to content

Commit 59bad84

Browse files
committed
bugfix: poolWatcher.Unregister() data race
You need to protect the poolWatcher.unregistered variable to avoid the data race. It does not look too critical because we don't expect that performance of poolWatcher.Unregister() is matter in cuncurrent calls case. It could also lead to crashes. Part of #218
1 parent ffe84ee commit 59bad84

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

connection_pool/watcher.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *watcherContainer) remove(watcher *poolWatcher) bool {
3333
if watcher == c.head {
3434
c.head = watcher.next
3535
return true
36-
} else {
36+
} else if c.head != nil {
3737
cur := c.head
3838
for cur.next != nil {
3939
if cur.next == watcher {
@@ -85,7 +85,11 @@ type poolWatcher struct {
8585

8686
// Unregister unregisters the pool watcher.
8787
func (w *poolWatcher) Unregister() {
88-
if !w.unregistered && w.container.remove(w) {
88+
w.mutex.Lock()
89+
unregistered := w.unregistered
90+
w.mutex.Unlock()
91+
92+
if !unregistered && w.container.remove(w) {
8993
w.mutex.Lock()
9094
w.unregistered = true
9195
for _, watcher := range w.watchers {

0 commit comments

Comments
 (0)