Skip to content

Commit 11e8e84

Browse files
msohailhussainMichael Ng
authored andcommitted
fix(notification): race detected send notification issue (#222)
1 parent 6895d35 commit 11e8e84

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

pkg/notification/manager.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -19,6 +19,7 @@ package notification
1919

2020
import (
2121
"fmt"
22+
"sync"
2223
"sync/atomic"
2324

2425
"github.com/optimizely/go-sdk/pkg/logging"
@@ -37,6 +38,7 @@ type Manager interface {
3738
type AtomicManager struct {
3839
handlers map[uint32]func(interface{})
3940
counter uint32
41+
lock sync.RWMutex
4042
}
4143

4244
// NewAtomicManager creates a new instance of the atomic manager
@@ -48,13 +50,19 @@ func NewAtomicManager() *AtomicManager {
4850

4951
// Add adds the given handler
5052
func (am *AtomicManager) Add(newHandler func(interface{})) (int, error) {
53+
am.lock.Lock()
54+
defer am.lock.Unlock()
55+
5156
atomic.AddUint32(&am.counter, 1)
5257
am.handlers[am.counter] = newHandler
5358
return int(am.counter), nil
5459
}
5560

5661
// Remove removes handler with the given id
5762
func (am *AtomicManager) Remove(id int) {
63+
am.lock.Lock()
64+
defer am.lock.Unlock()
65+
5866
handlerID := uint32(id)
5967
if _, ok := am.handlers[handlerID]; ok {
6068
delete(am.handlers, handlerID)
@@ -66,7 +74,19 @@ func (am *AtomicManager) Remove(id int) {
6674

6775
// Send sends the notification to the registered handlers
6876
func (am *AtomicManager) Send(notification interface{}) {
69-
for _, handler := range am.handlers {
77+
// copying handler to avoid race condition
78+
handlers := am.copyHandlers()
79+
for _, handler := range handlers {
7080
handler(notification)
7181
}
7282
}
83+
84+
// Return a copy of the given handlers
85+
func (am *AtomicManager) copyHandlers() (handlers []func(interface{})) {
86+
am.lock.RLock()
87+
defer am.lock.RUnlock()
88+
for _, v := range am.handlers {
89+
handlers = append(handlers, v)
90+
}
91+
return handlers
92+
}

pkg/notification/manager_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,56 @@ func TestAtomicManager(t *testing.T) {
5050
// Sanity check by calling remove with a incorrect handler id
5151
atomicManager.Remove(55)
5252
}
53+
54+
func TestSendRaceCondition(t *testing.T) {
55+
sync := make(chan interface{})
56+
payload := map[string]interface{}{
57+
"key": "test",
58+
}
59+
atomicManager := NewAtomicManager()
60+
result1, result2 := 0, 0
61+
listenerCalled := false
62+
63+
listener1 := func(interface{}) {
64+
}
65+
66+
listener2 := func(interface{}) {
67+
// Add listener2 internally to assert deadlock
68+
result2, _ = atomicManager.Add(listener1)
69+
// Remove all added listeners
70+
atomicManager.Remove(result1)
71+
atomicManager.Remove(result2)
72+
listenerCalled = true
73+
}
74+
result1, _ = atomicManager.Add(listener2)
75+
76+
go func() {
77+
atomicManager.Send(payload)
78+
// notifying that notification is sent.
79+
sync <- ""
80+
}()
81+
82+
atomicManager.Add(listener1)
83+
<-sync
84+
85+
assert.Equal(t, 1, result1)
86+
assert.Equal(t, len(atomicManager.handlers), 1)
87+
assert.Equal(t, true, listenerCalled)
88+
}
89+
90+
func TestAddRaceCondition(t *testing.T) {
91+
sync := make(chan interface{})
92+
atomicManager := NewAtomicManager()
93+
94+
listener1 := func(interface{}) {
95+
96+
}
97+
result1, _ := atomicManager.Add(listener1)
98+
go func() {
99+
atomicManager.Remove(result1)
100+
sync <- ""
101+
}()
102+
103+
<-sync
104+
assert.Equal(t, len(atomicManager.handlers), 0)
105+
}

0 commit comments

Comments
 (0)