tun: make NativeTun.Close well behaved, not crash on double close

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2021-02-18 14:53:22 -08:00
committed by Jason A. Donenfeld
parent fecb8f482a
commit 0f4809f366
5 changed files with 62 additions and 43 deletions

View File

@@ -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
}