device: wait for routines to stop before removing peers

Peers are currently removed after Device's goroutines are signaled to stop,
but without waiting for them to actually do so, which is racy.

For example, RoutineHandshake may be in Peer.SendKeepalive
when the corresponding peer is removed, which closes its nonce channel.
This causes a send on a closed channel, as observed in tailscale/tailscale#487.

This patch seems to be the correct synchronizing action:
Peer's goroutines are receivers and handle channel closure gracefully,
so Device's goroutines are the ones that should be fully stopped first.

Signed-Off-By: Dmytro Shynkevych <dmytro@tailscale.com>
This commit is contained in:
Dmytro Shynkevych 2020-06-24 01:35:41 -04:00 committed by David Crawshaw
parent b84f1d4db2
commit 4369db522b

View file

@ -383,10 +383,10 @@ func (device *Device) Close() {
device.isUp.Set(false) device.isUp.Set(false)
close(device.signals.stop) close(device.signals.stop)
device.state.stopping.Wait()
device.RemoveAllPeers() device.RemoveAllPeers()
device.state.stopping.Wait()
device.FlushPacketQueues() device.FlushPacketQueues()
device.rate.limiter.Close() device.rate.limiter.Close()