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_windows.go | |
parent | fecb8f482ad8bc4d56fa6202fe15d2a221d0dbe5 (diff) |
tun: make NativeTun.Close well behaved, not crash on double close
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Diffstat (limited to 'tun/tun_windows.go')
-rw-r--r-- | tun/tun_windows.go | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 081b5e2..9d83db7 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -10,6 +10,7 @@ import ( "fmt" "log" "os" + "sync" "sync/atomic" "time" _ "unsafe" @@ -42,6 +43,7 @@ type NativeTun struct { rate rateJuggler session wintun.Session readWait windows.Handle + closeOnce sync.Once } var WintunPool, _ = wintun.MakePool("WireGuard") @@ -122,13 +124,15 @@ func (tun *NativeTun) Events() chan Event { } func (tun *NativeTun) Close() error { - tun.close = true - tun.session.End() var err error - if tun.wt != nil { - _, err = tun.wt.Delete(false) - } - close(tun.events) + tun.closeOnce.Do(func() { + tun.close = true + tun.session.End() + if tun.wt != nil { + _, err = tun.wt.Delete(false) + } + close(tun.events) + }) return err } |