From: Josh Bleecher Snyder Date: Sat, 19 Dec 2020 00:32:46 +0000 (-0800) Subject: device: call wg.Add outside the goroutine X-Git-Tag: 0.0.20210212~113 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b42e32047d16cc4389ef63550e525c7b08320172;p=thirdparty%2Fwireguard-go.git device: call wg.Add outside the goroutine 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 --- diff --git a/device/peer.go b/device/peer.go index 8006b9f..ac85c94 100644 --- a/device/peer.go +++ b/device/peer.go @@ -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() diff --git a/device/send.go b/device/send.go index c75d23e..55df4b3 100644 --- a/device/send.go +++ b/device/send.go @@ -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 {