summaryrefslogtreecommitdiff
path: root/tun/tun_darwin.go
diff options
context:
space:
mode:
Diffstat (limited to 'tun/tun_darwin.go')
-rw-r--r--tun/tun_darwin.go20
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
}