From f7bbdc31a0065e3d5a68a3e6c7a7449954fdd339 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 15 Dec 2020 17:44:21 -0800 Subject: device: fix data race in peer.timersActive Found by the race detector and existing tests. To avoid introducing a lock into this hot path, calculate and cache whether any peers exist. Signed-off-by: Josh Bleecher Snyder --- device/device.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'device/device.go') diff --git a/device/device.go b/device/device.go index 99f5e60..e7d70c4 100644 --- a/device/device.go +++ b/device/device.go @@ -49,8 +49,9 @@ type Device struct { } peers struct { - sync.RWMutex - keyMap map[NoisePublicKey]*Peer + empty AtomicBool // empty reports whether len(keyMap) == 0 + sync.RWMutex // protects keyMap + keyMap map[NoisePublicKey]*Peer } // unprotected / "self-synchronising resources" @@ -129,6 +130,7 @@ func unsafeRemovePeer(device *Device, peer *Peer, key NoisePublicKey) { // remove from peer map delete(device.peers.keyMap, key) + device.peers.empty.Set(len(device.peers.keyMap) == 0) } func deviceUpdateState(device *Device) { -- cgit v1.2.3