From caebdfe9d05901230dd7c6b7569be2903fd8ac87 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 15 Jan 2020 13:39:37 -0500 Subject: tun: darwin: ignore ENOMEM errors Coauthored-by: Andrej Mihajlov --- tun/tun_darwin.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'tun') diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 6d2e6dd..f19a7df 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -11,6 +11,7 @@ import ( "net" "os" "syscall" + "time" "unsafe" "golang.org/x/net/ipv6" @@ -42,6 +43,22 @@ type NativeTun struct { var sockaddrCtlSize uintptr = 32 +func retryInterfaceByIndex(index int) (iface *net.Interface, err error) { + for i := 0; i < 20; i++ { + iface, err = net.InterfaceByIndex(index) + if err != nil { + if opErr, ok := err.(*net.OpError); ok { + if syscallErr, ok := opErr.Err.(*os.SyscallError); ok && syscallErr.Err == syscall.ENOMEM { + time.Sleep(time.Duration(i) * time.Second / 3) + continue + } + } + } + return iface, err + } + return nil, err +} + func (tun *NativeTun) routineRouteListener(tunIfindex int) { var ( statusUp bool @@ -74,7 +91,7 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) { continue } - iface, err := net.InterfaceByIndex(ifindex) + iface, err := retryInterfaceByIndex(ifindex) if err != nil { tun.errors <- err return -- cgit v1.2.3