From 4fea604979a9a74f992f3d3d24a94db76208102f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 25 Jan 2021 13:20:19 -0800 Subject: [PATCH] wgengine/router: stop setPrivateNetwork goroutine on configureInterface failure On Windows, configureInterface starts a goroutine reconfiguring the Windows firewall. But if configureInterface fails later, that goroutine kept running and likely failing forever, spamming logs. Make it stop quietly if its launching goroutine filed. --- wgengine/router/ifconfig_windows.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/wgengine/router/ifconfig_windows.go b/wgengine/router/ifconfig_windows.go index b4a8d406e..486915e6e 100644 --- a/wgengine/router/ifconfig_windows.go +++ b/wgengine/router/ifconfig_windows.go @@ -237,7 +237,7 @@ func interfaceFromLUID(luid winipcfg.LUID, flags winipcfg.GAAFlags) (*winipcfg.I return nil, fmt.Errorf("interfaceFromLUID: interface with LUID %v not found", luid) } -func configureInterface(cfg *Config, tun *tun.NativeTun) error { +func configureInterface(cfg *Config, tun *tun.NativeTun) (retErr error) { const mtu = 0 luid := winipcfg.LUID(tun.LUID()) iface, err := interfaceFromLUID(luid, @@ -251,6 +251,15 @@ func configureInterface(cfg *Config, tun *tun.NativeTun) error { return err } + // Send non-nil return errors to retErrc, to interupt our background + // setPrivateNetwork goroutine. + retErrc := make(chan error, 1) + defer func() { + if retErr != nil { + retErrc <- retErr + } + }() + go func() { // It takes a weirdly long time for Windows to notice the // new interface has come up. Poll periodically until it @@ -262,11 +271,18 @@ func configureInterface(cfg *Config, tun *tun.NativeTun) error { log.Printf("setPrivateNetwork(try=%d): %v", i, err) } else { if found { + if i > 0 { + log.Printf("setPrivateNetwork(try=%d): success", i) + } return } log.Printf("setPrivateNetwork(try=%d): not found", i) } - time.Sleep(1 * time.Second) + select { + case <-time.After(time.Second): + case <-retErrc: + return + } } log.Printf("setPrivateNetwork: adapter LUID %v not found after %d tries, giving up", luid, tries) }()