Squash a whole load of races (and mutex half the world)

This commit is contained in:
Neil Alexander 2019-04-20 11:53:38 +01:00
parent 24281d4049
commit f3e742a297
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
4 changed files with 66 additions and 20 deletions

View file

@ -291,6 +291,10 @@ func (r *router) sendPacket(bs []byte) {
if destSnet.IsValid() {
sinfo, isIn = r.core.sessions.getByTheirSubnet(&destSnet)
}
sinfo.timeMutex.Lock()
sinfo.initMutex.RLock()
defer sinfo.timeMutex.Unlock()
defer sinfo.initMutex.RUnlock()
switch {
case !isIn || !sinfo.init:
// No or unintiialized session, so we need to search first
@ -306,6 +310,7 @@ func (r *router) sendPacket(bs []byte) {
} else {
// We haven't heard about the dest in a while
now := time.Now()
if !sinfo.time.Before(sinfo.pingTime) {
// Update pingTime to start the clock for searches (above)
sinfo.pingTime = now
@ -315,6 +320,7 @@ func (r *router) sendPacket(bs []byte) {
sinfo.pingSend = now
r.core.sessions.sendPingPong(sinfo, false)
}
sinfo.timeMutex.Unlock()
}
fallthrough // Also send the packet
default: