server: Add a mutex to kill a very small, bad race

This commit is contained in:
Daniel Oaks 2016-10-22 21:01:19 +10:00
parent 835187a736
commit 1812edb2db
1 changed files with 35 additions and 26 deletions

View File

@ -68,6 +68,7 @@ type Server struct {
store buntdb.DB store buntdb.DB
idle chan *Client idle chan *Client
limits Limits limits Limits
listenerEventActMutex sync.Mutex
listenerUpdateMutex sync.Mutex listenerUpdateMutex sync.Mutex
listeners map[string]ListenerInterface listeners map[string]ListenerInterface
monitoring map[string][]Client monitoring map[string][]Client
@ -341,6 +342,11 @@ func (s *Server) createListener(addr string, tlsMap map[string]*tls.Config) {
select { select {
case event := <-s.listeners[addr].Events: case event := <-s.listeners[addr].Events:
// this is used to confirm that whoever passed us this event has closed the existing listener correctly (in an attempt to get us to notice the event).
// this is required to keep REHASH from having a very small race possibility of killing the primary listener
s.listenerEventActMutex.Lock()
s.listenerEventActMutex.Unlock()
if event.Type == DestroyListener { if event.Type == DestroyListener {
// listener should already be closed, this is just for safety // listener should already be closed, this is just for safety
listener.Close() listener.Close()
@ -922,6 +928,7 @@ func rehashHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
} }
} }
server.listenerEventActMutex.Lock()
if exists { if exists {
// update old listener // update old listener
fmt.Println("refreshing", addr) fmt.Println("refreshing", addr)
@ -938,6 +945,8 @@ func rehashHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
} }
// force listener to apply the event right away // force listener to apply the event right away
server.listeners[addr].Listener.Close() server.listeners[addr].Listener.Close()
server.listenerEventActMutex.Unlock()
} }
for _, newaddr := range config.Server.Listen { for _, newaddr := range config.Server.Listen {