lib/connections: Increase lock periods to prevent races (fixes #2899)

This commit is contained in:
Audrius Butkevicius
2016-04-01 07:23:11 +00:00
committed by Jakob Borg
parent f5f0e46016
commit 432c78079b

View File

@@ -189,8 +189,8 @@ next:
// If we have a relay connection, and the new incoming connection is // If we have a relay connection, and the new incoming connection is
// not a relay connection, we should drop that, and prefer the this one. // not a relay connection, we should drop that, and prefer the this one.
s.mut.RLock() s.mut.RLock()
skip := false
ct, ok := s.connType[remoteID] ct, ok := s.connType[remoteID]
s.mut.RUnlock()
if ok && !ct.IsDirect() && c.Type.IsDirect() { if ok && !ct.IsDirect() && c.Type.IsDirect() {
l.Debugln("Switching connections", remoteID) l.Debugln("Switching connections", remoteID)
s.model.Close(remoteID, fmt.Errorf("switching connections")) s.model.Close(remoteID, fmt.Errorf("switching connections"))
@@ -203,10 +203,14 @@ next:
// connections still established... // connections still established...
l.Infof("Connected to already connected device (%s)", remoteID) l.Infof("Connected to already connected device (%s)", remoteID)
c.Close() c.Close()
continue skip = true
} else if s.model.IsPaused(remoteID) { } else if s.model.IsPaused(remoteID) {
l.Infof("Connection from paused device (%s)", remoteID) l.Infof("Connection from paused device (%s)", remoteID)
c.Close() c.Close()
skip = true
}
s.mut.RUnlock()
if skip {
continue continue
} }
@@ -250,12 +254,12 @@ next:
l.Infof("Established secure connection to %s at %s", remoteID, name) l.Infof("Established secure connection to %s at %s", remoteID, name)
l.Debugf("cipher suite: %04X in lan: %t", c.ConnectionState().CipherSuite, !limit) l.Debugf("cipher suite: %04X in lan: %t", c.ConnectionState().CipherSuite, !limit)
s.mut.Lock()
s.model.AddConnection(model.Connection{ s.model.AddConnection(model.Connection{
c, c,
protoConn, protoConn,
c.Type, c.Type,
}, hello) }, hello)
s.mut.Lock()
s.connType[remoteID] = c.Type s.connType[remoteID] = c.Type
s.mut.Unlock() s.mut.Unlock()
continue next continue next
@@ -279,16 +283,16 @@ func (s *Service) connect() {
l.Debugln("Reconnect loop for", deviceID) l.Debugln("Reconnect loop for", deviceID)
if s.model.IsPaused(deviceID) {
continue
}
connected := s.model.ConnectedTo(deviceID)
s.mut.RLock() s.mut.RLock()
paused := s.model.IsPaused(deviceID)
connected := s.model.ConnectedTo(deviceID)
ct, ok := s.connType[deviceID] ct, ok := s.connType[deviceID]
relaysEnabled := s.relaysEnabled relaysEnabled := s.relaysEnabled
s.mut.RUnlock() s.mut.RUnlock()
if paused {
continue
}
if connected && ok && ct.IsDirect() { if connected && ok && ct.IsDirect() {
l.Debugln("Already connected to", deviceID, "via", ct.String()) l.Debugln("Already connected to", deviceID, "via", ct.String())
continue continue