From e83d02ffd1d9cb0c0961e7739ce6ace09275d634 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 10 May 2020 22:34:16 +0000 Subject: [PATCH] wgengine: don't double-close tundev on setup error. Part of #368. Signed-off-by: David Anderson --- wgengine/userspace.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wgengine/userspace.go b/wgengine/userspace.go index 53bb0b1dc..ea9e132ad 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -105,8 +105,6 @@ func NewUserspaceEngine(logf logger.Logf, tunname string, listenPort uint16) (En e, err := NewUserspaceEngineAdvanced(logf, tundev, router.New, listenPort) if err != nil { - logf("NewUserspaceEngineAdv: %v", err) - tundev.Close() return nil, err } return e, err @@ -134,6 +132,7 @@ func newUserspaceEngineAdvanced(logf logger.Logf, tundev tun.Device, routerGen R mon, err := monitor.New(logf, func() { e.LinkChange(false) }) if err != nil { + tundev.Close() return nil, err } e.linkMon = mon @@ -151,6 +150,7 @@ func newUserspaceEngineAdvanced(logf logger.Logf, tundev tun.Device, routerGen R } e.magicConn, err = magicsock.Listen(magicsockOpts) if err != nil { + tundev.Close() return nil, fmt.Errorf("wgengine: %v", err) } @@ -204,6 +204,7 @@ func newUserspaceEngineAdvanced(logf logger.Logf, tundev tun.Device, routerGen R SkipBindUpdate: true, } + // wgdev takes ownership of tundev, will close it when closed. e.wgdev = device.NewDevice(e.tundev, opts) defer func() { if reterr != nil {