Skip to content

Commit

Permalink
Fix panic on concurrent map writes in unix_socket
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Dec 27, 2017
1 parent 9f8a7d2 commit b4030b0
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions notifier/unix_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"strings"
"sync"

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

touchListeners := make(map[*net.Conn]chan []byte)
touchListenersMutex := sync.RWMutex{}
go func() {
for {
value := <-touch
touchListenersMutex.RLock()
for _, listener := range touchListeners {
listener <- []byte(value)
}
touchListenersMutex.RUnlock()
}
}()

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

go notify(listener, touchListeners)
go notify(listener, touchListeners, &touchListenersMutex)
}
}

func notify(listener net.Conn, touchListeners map[*net.Conn]chan []byte) {
func notify(listener net.Conn, touchListeners map[*net.Conn]chan []byte, touchListenersMutex *sync.RWMutex) {
values := make(chan []byte)
touchListenersMutex.Lock()
touchListeners[&listener] = values
touchListenersMutex.Unlock()
defer (func() {
touchListenersMutex.Lock()
delete(touchListeners, &listener)
touchListenersMutex.Unlock()
listener.Close()
})()

Expand Down

0 comments on commit b4030b0

Please sign in to comment.