wireguard-go/device
Josh Bleecher Snyder d8dd1f254f 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>
2021-02-08 13:02:52 -08:00
..
alignment_test.go device: use a waiting sync.Pool instead of a channel 2021-02-02 19:32:13 +01:00
allowedips.go global: bump copyright 2021-01-28 17:52:15 +01:00
allowedips_rand_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
allowedips_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
bind_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
channels.go device: remove mutex from Peer send/receive 2021-02-08 13:02:52 -08:00
constants.go global: bump copyright 2021-01-28 17:52:15 +01:00
cookie.go global: bump copyright 2021-01-28 17:52:15 +01:00
cookie_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
device.go device: create channels.go 2021-02-08 12:38:19 -08:00
device_test.go device: print direction when ping transit fails 2021-02-08 12:01:08 -08:00
devicestate_string.go device: overhaul device state management 2021-02-08 10:32:07 -08:00
endpoint_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
indextable.go global: bump copyright 2021-01-28 17:52:15 +01:00
ip.go global: bump copyright 2021-01-28 17:52:15 +01:00
kdf_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
keypair.go global: bump copyright 2021-01-28 17:52:15 +01:00
logger.go global: bump copyright 2021-01-28 17:52:15 +01:00
misc.go global: bump copyright 2021-01-28 17:52:15 +01:00
mobilequirks.go global: bump copyright 2021-01-28 17:52:15 +01:00
noise-helpers.go global: bump copyright 2021-01-28 17:52:15 +01:00
noise-protocol.go global: bump copyright 2021-01-28 17:52:15 +01:00
noise-types.go global: bump copyright 2021-01-28 17:52:15 +01:00
noise_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
peer.go device: remove mutex from Peer send/receive 2021-02-08 13:02:52 -08:00
pools.go device: use a waiting sync.Pool instead of a channel 2021-02-02 19:32:13 +01:00
pools_test.go device: benchmark the waitpool to compare it to the prior channels 2021-02-03 16:59:29 +01:00
queueconstants_android.go global: bump copyright 2021-01-28 17:52:15 +01:00
queueconstants_default.go global: bump copyright 2021-01-28 17:52:15 +01:00
queueconstants_ios.go global: bump copyright 2021-01-28 17:52:15 +01:00
receive.go device: remove mutex from Peer send/receive 2021-02-08 13:02:52 -08:00
send.go device: remove mutex from Peer send/receive 2021-02-08 13:02:52 -08:00
sticky_default.go device: do not include sticky sockets on android 2020-06-07 01:50:20 -06:00
sticky_linux.go global: bump copyright 2021-01-28 17:52:15 +01:00
timers.go device: separate timersInit from timersStart 2021-02-08 10:32:07 -08:00
tun.go device: don't track device interface state in RoutineTUNEventReader 2021-02-08 10:32:07 -08:00
tun_test.go global: bump copyright 2021-01-28 17:52:15 +01:00
uapi.go device: overhaul device state management 2021-02-08 10:32:07 -08:00
version.go version: bump snapshot 2020-11-18 14:24:17 +01:00