Skip to content

Commit b4030b0

Browse files
committed
Fix panic on concurrent map writes in unix_socket
1 parent 9f8a7d2 commit b4030b0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

notifier/unix_socket.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path"
77
"strings"
8+
"sync"
89

910
log "github.com/sirupsen/logrus"
1011
)
@@ -48,12 +49,15 @@ func SetupUnixSocketNotifier(notifiers map[string]chan Message, exits map[string
4849
notifiers["notifier/unix_socket"] = touch
4950

5051
touchListeners := make(map[*net.Conn]chan []byte)
52+
touchListenersMutex := sync.RWMutex{}
5153
go func() {
5254
for {
5355
value := <-touch
56+
touchListenersMutex.RLock()
5457
for _, listener := range touchListeners {
5558
listener <- []byte(value)
5659
}
60+
touchListenersMutex.RUnlock()
5761
}
5862
}()
5963

@@ -66,15 +70,19 @@ func SetupUnixSocketNotifier(notifiers map[string]chan Message, exits map[string
6670
return
6771
}
6872

69-
go notify(listener, touchListeners)
73+
go notify(listener, touchListeners, &touchListenersMutex)
7074
}
7175
}
7276

73-
func notify(listener net.Conn, touchListeners map[*net.Conn]chan []byte) {
77+
func notify(listener net.Conn, touchListeners map[*net.Conn]chan []byte, touchListenersMutex *sync.RWMutex) {
7478
values := make(chan []byte)
79+
touchListenersMutex.Lock()
7580
touchListeners[&listener] = values
81+
touchListenersMutex.Unlock()
7682
defer (func() {
83+
touchListenersMutex.Lock()
7784
delete(touchListeners, &listener)
85+
touchListenersMutex.Unlock()
7886
listener.Close()
7987
})()
8088

0 commit comments

Comments
 (0)