From d74c9aa95b35af39c78cb401c238f50525a698e1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 16 Jul 2020 08:21:34 -0700 Subject: [PATCH] wgengine/magicsock: update comment, fix earlier commit https://github.com/tailscale/tailscale/commit/891898525c12630b0f896cb9142ff5274e07afc2 had a continue that meant the didCopy synchronization never ran. Signed-off-by: Brad Fitzpatrick --- wgengine/magicsock/magicsock.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index c14045b14..4c7139704 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1134,10 +1134,13 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d } c.ReSTUN("derp-close") c.logf("magicsock: [%p] derp.Recv(derp-%d): %v", dc, regionID, err) + + // Avoid excessive spinning. + // TODO: use a backoff timer, perhaps between 10ms and 500ms? + // Don't want to sleep too long. For now 250ms seems fine. select { case <-ctx.Done(): return - // Avoid excessive spinning. case <-time.After(250 * time.Millisecond): } continue @@ -1165,15 +1168,12 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d case <-ctx.Done(): return case c.derpRecvCh <- res: - continue - } - // The copy will not happen if connCtx is cancelled before we reach copyBuf. - // This has resulted in a rare inifite wait in practice. - select { - case <-ctx.Done(): - return - case <-didCopy: - continue + select { + case <-ctx.Done(): + return + case <-didCopy: + continue + } } } }