diff options
author | Brad Fitzpatrick <bradfitz@tailscale.com> | 2021-02-18 14:53:22 -0800 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-02-22 15:26:29 +0100 |
commit | 0f4809f366daa77c6e2f5b09d3f05771fe9bf188 (patch) | |
tree | 51fcba51b8d65b559e4ac2da16bd045ec8b6d730 /tun/tun_darwin.go | |
parent | fecb8f482ad8bc4d56fa6202fe15d2a221d0dbe5 (diff) | |
download | wireguard-go-0f4809f366daa77c6e2f5b09d3f05771fe9bf188.tar.gz wireguard-go-0f4809f366daa77c6e2f5b09d3f05771fe9bf188.zip |
tun: make NativeTun.Close well behaved, not crash on double close
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Diffstat (limited to 'tun/tun_darwin.go')
-rw-r--r-- | tun/tun_darwin.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 542f666..a703c8c 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -10,6 +10,7 @@ import ( "fmt" "net" "os" + "sync" "syscall" "time" "unsafe" @@ -26,6 +27,7 @@ type NativeTun struct { events chan Event errors chan error routeSocket int + closeOnce sync.Once } func retryInterfaceByIndex(index int) (iface *net.Interface, err error) { @@ -256,14 +258,16 @@ func (tun *NativeTun) Flush() error { } func (tun *NativeTun) Close() error { - var err2 error - err1 := tun.tunFile.Close() - if tun.routeSocket != -1 { - unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR) - err2 = unix.Close(tun.routeSocket) - } else if tun.events != nil { - close(tun.events) - } + var err1, err2 error + tun.closeOnce.Do(func() { + err1 = tun.tunFile.Close() + if tun.routeSocket != -1 { + unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR) + err2 = unix.Close(tun.routeSocket) + } else if tun.events != nil { + close(tun.events) + } + }) if err1 != nil { return err1 } |