diff options
Diffstat (limited to 'tun/tun_openbsd.go')
-rw-r--r-- | tun/tun_openbsd.go | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/tun/tun_openbsd.go b/tun/tun_openbsd.go index 8fca1e3..7ef62f4 100644 --- a/tun/tun_openbsd.go +++ b/tun/tun_openbsd.go @@ -10,6 +10,7 @@ import ( "fmt" "net" "os" + "sync" "syscall" "unsafe" @@ -32,6 +33,7 @@ type NativeTun struct { events chan Event errors chan error routeSocket int + closeOnce sync.Once } func (tun *NativeTun) routineRouteListener(tunIfindex int) { @@ -245,15 +247,17 @@ 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) - tun.routeSocket = -1 - } 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) + tun.routeSocket = -1 + } else if tun.events != nil { + close(tun.events) + } + }) if err1 != nil { return err1 } |