From 891898525c12630b0f896cb9142ff5274e07afc2 Mon Sep 17 00:00:00 2001 From: Dmytro Shynkevych Date: Thu, 16 Jul 2020 10:29:43 -0400 Subject: [PATCH] wgengine/magicsock: make receive from didCopy respect cancellation. Very rarely, cancellation occurs between a successful send on derpRecvCh and a call to copyBuf on the receiving side. Without this patch, this situation results in <-copyBuf blocking indefinitely. Signed-off-by: Dmytro Shynkevych --- wgengine/magicsock/magicsock.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 2be8b304b..8132e7f1b 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1160,7 +1160,15 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netaddr.IPPort, d case <-ctx.Done(): return case c.derpRecvCh <- res: - <-didCopy + 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 } } }