]> git.ipfire.org Git - thirdparty/wireguard-go.git/commit
device: overhaul device state management
authorJosh Bleecher Snyder <josh@tailscale.com>
Tue, 19 Jan 2021 17:02:16 +0000 (09:02 -0800)
committerJosh Bleecher Snyder <josh@tailscale.com>
Mon, 8 Feb 2021 18:32:07 +0000 (10:32 -0800)
commit0bcb822e5b4ee6408c5bcb5ad4d4e61b394a834e
treea7fc1d8ff7806e58104d06aee4859fbe89c8c25e
parentda956772030b8b1fcbd37f82f08863070c93aa0f
device: overhaul device state management

This commit simplifies device state management.
It creates a single unified state variable and documents its semantics.

It also makes state changes more atomic.
As an example of the sort of bug that occurred due to non-atomic state changes,
the following sequence of events used to occur approximately every 2.5 million test runs:

* RoutineTUNEventReader received an EventDown event.
* It called device.Down, which called device.setUpDown.
* That set device.state.changing, but did not yet attempt to lock device.state.Mutex.
* Test completion called device.Close.
* device.Close locked device.state.Mutex.
* device.Close blocked on a call to device.state.stopping.Wait.
* device.setUpDown then attempted to lock device.state.Mutex and blocked.

Deadlock results. setUpDown cannot progress because device.state.Mutex is locked.
Until setUpDown returns, RoutineTUNEventReader cannot call device.state.stopping.Done.
Until device.state.stopping.Done gets called, device.state.stopping.Wait is blocked.
As long as device.state.stopping.Wait is blocked, device.state.Mutex cannot be unlocked.
This commit fixes that deadlock by holding device.state.mu
when checking that the device is not closed.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/device.go
device/device_test.go
device/devicestate_string.go [new file with mode: 0644]
device/peer.go
device/receive.go
device/send.go
device/timers.go
device/uapi.go