diff options
author | Josh Bleecher Snyder <josh@tailscale.com> | 2021-02-08 13:02:52 -0800 |
---|---|---|
committer | Josh Bleecher Snyder <josh@tailscale.com> | 2021-02-08 13:02:52 -0800 |
commit | d8dd1f254fc42878970d764b046ec5789ab30259 (patch) | |
tree | 5921b86dc7d81bce2efa1a221e52f0e880e957a0 /device/receive.go | |
parent | 57aadfcb14776a792e629dd7c22be066b7e2ed20 (diff) |
device: remove mutex from Peer send/receive
The immediate motivation for this change is an observed deadlock.
1. A goroutine calls peer.Stop. That calls peer.queue.Lock().
2. Another goroutine is in RoutineSequentialReceiver.
It receives an elem from peer.queue.inbound.
3. The peer.Stop goroutine calls close(peer.queue.inbound),
close(peer.queue.outbound), and peer.stopping.Wait().
It blocks waiting for RoutineSequentialReceiver
and RoutineSequentialSender to exit.
4. The RoutineSequentialReceiver goroutine calls peer.SendStagedPackets().
SendStagedPackets attempts peer.queue.RLock().
That blocks forever because the peer.Stop
goroutine holds a write lock on that mutex.
A background motivation for this change is that it can be expensive
to have a mutex in the hot code path of RoutineSequential*.
The mutex was necessary to avoid attempting to send elems on a closed channel.
This commit removes that danger by never closing the channel.
Instead, we send a sentinel nil value on the channel to indicate
to the receiver that it should exit.
The only problem with this is that if the receiver exits,
we could write an elem into the channel which would never get received.
If it never gets received, it cannot get returned to the device pools.
To work around this, we use a finalizer. When the channel can be GC'd,
the finalizer drains any remaining elements from the channel and
restores them to the device pool.
After that change, peer.queue.RWMutex no longer makes sense where it is.
It is only used to prevent concurrent calls to Start and Stop.
Move it to a more sensible location and make it a plain sync.Mutex.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Diffstat (limited to 'device/receive.go')
-rw-r--r-- | device/receive.go | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/device/receive.go b/device/receive.go index c6a28f7..7acb7d9 100644 --- a/device/receive.go +++ b/device/receive.go @@ -166,7 +166,6 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind conn.Bind) { elem.Lock() // add to decryption queues - peer.queue.RLock() if peer.isRunning.Get() { peer.queue.inbound <- elem device.queue.decryption.c <- elem @@ -174,8 +173,6 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind conn.Bind) { } else { device.PutInboundElement(elem) } - peer.queue.RUnlock() - continue // otherwise it is a fixed size & handshake related packet @@ -406,6 +403,9 @@ func (peer *Peer) RoutineSequentialReceiver() { device.log.Verbosef("%v - Routine: sequential receiver - started", peer) for elem := range peer.queue.inbound { + if elem == nil { + return + } var err error elem.Lock() if elem.packet == nil { |