Skip to content

Commit 00fbed0

Browse files
josharianzx2c4
authored andcommitted
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 <[email protected]>
1 parent 099f633 commit 00fbed0

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

device/peer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ func (peer *Peer) Start() {
213213

214214
// wait for routines to start
215215

216+
// RoutineNonce writes to the encryption queue; keep it alive until we are done.
217+
device.queue.encryption.wg.Add(1)
216218
go peer.RoutineNonce()
217219
go peer.RoutineSequentialSender()
218220
go peer.RoutineSequentialReceiver()

device/send.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ func (peer *Peer) RoutineNonce() {
352352
device := peer.device
353353
logDebug := device.log.Debug
354354

355-
// We write to the encryption queue; keep it alive until we are done.
356-
device.queue.encryption.wg.Add(1)
357-
358355
flush := func() {
359356
for {
360357
select {

0 commit comments

Comments
 (0)