make sure we buffer packets in order when waiting for a dial to finish, and make sure we can't leak the dial goroutine if the conn is closed before all buffered packets are sent

This commit is contained in:
Arceliar 2019-08-21 21:52:54 -05:00
parent 562a7d1f19
commit 8cb3ea8e3d

View file

@ -203,8 +203,6 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen") panic("Given empty dstNodeID and dstNodeIDMask - this shouldn't happen")
} }
// Dial to the remote node // Dial to the remote node
go func() {
// FIXME just spitting out a goroutine to do this is kind of ugly and means we drop packets until the dial finishes
tun.mutex.Lock() tun.mutex.Lock()
_, known := tun.dials[*dstNodeID] _, known := tun.dials[*dstNodeID]
tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], bs) tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], bs)
@ -213,6 +211,7 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:] tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:]
} }
tun.mutex.Unlock() tun.mutex.Unlock()
go func() {
if known { if known {
return return
} }
@ -232,7 +231,11 @@ func (tun *TunAdapter) readerPacketHandler(ch chan []byte) {
if tc != nil { if tc != nil {
for _, packet := range packets { for _, packet := range packets {
p := packet // Possibly required because of how range p := packet // Possibly required because of how range
tc.send <- p select {
case <-tc.stop:
util.PutBytes(p)
case tc.send <- p:
}
} }
} }
}() }()