mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-30 07:05:06 +03:00
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:
parent
562a7d1f19
commit
8cb3ea8e3d
1 changed files with 13 additions and 10 deletions
|
@ -203,16 +203,15 @@ 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
|
||||||
|
tun.mutex.Lock()
|
||||||
|
_, known := tun.dials[*dstNodeID]
|
||||||
|
tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], bs)
|
||||||
|
for len(tun.dials[*dstNodeID]) > 32 {
|
||||||
|
util.PutBytes(tun.dials[*dstNodeID][0])
|
||||||
|
tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:]
|
||||||
|
}
|
||||||
|
tun.mutex.Unlock()
|
||||||
go func() {
|
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()
|
|
||||||
_, known := tun.dials[*dstNodeID]
|
|
||||||
tun.dials[*dstNodeID] = append(tun.dials[*dstNodeID], bs)
|
|
||||||
for len(tun.dials[*dstNodeID]) > 32 {
|
|
||||||
util.PutBytes(tun.dials[*dstNodeID][0])
|
|
||||||
tun.dials[*dstNodeID] = tun.dials[*dstNodeID][1:]
|
|
||||||
}
|
|
||||||
tun.mutex.Unlock()
|
|
||||||
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:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue