]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: call wg.Add outside the goroutine
authorJosh Bleecher Snyder <josh@tailscale.com>
Sat, 19 Dec 2020 00:32:46 +0000 (16:32 -0800)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 7 Jan 2021 13:49:44 +0000 (14:49 +0100)
One of the first rules of WaitGroups is that you call wg.Add
outside of a goroutine, not inside it. Fix this embarrassing mistake.

This prevents an extremely rare race condition (2 per 100,000 runs)
which could occur when attempting to start a new peer
concurrently with shutting down a device.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/peer.go
device/send.go

index 8006b9f3cc826126ebea15e093a3ebf6b1888d1c..ac85c94b77aed27fffccb9ff2524fdbd76e4a61f 100644 (file)
@@ -213,6 +213,8 @@ func (peer *Peer) Start() {
 
        // wait for routines to start
 
+       // RoutineNonce writes to the encryption queue; keep it alive until we are done.
+       device.queue.encryption.wg.Add(1)
        go peer.RoutineNonce()
        go peer.RoutineSequentialSender()
        go peer.RoutineSequentialReceiver()
index c75d23e6de743e74490af02062c10b95d0247b65..55df4b3a1984516d7c6e502c96a68b056b64c3bc 100644 (file)
@@ -352,9 +352,6 @@ func (peer *Peer) RoutineNonce() {
        device := peer.device
        logDebug := device.log.Debug
 
-       // We write to the encryption queue; keep it alive until we are done.
-       device.queue.encryption.wg.Add(1)
-
        flush := func() {
                for {
                        select {