]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: darwin: ignore ENOMEM errors
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 15 Jan 2020 18:39:37 +0000 (13:39 -0500)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 15 Jan 2020 18:39:37 +0000 (13:39 -0500)
Coauthored-by: Andrej Mihajlov <and@mullvad.net>
tun/tun_darwin.go

index 6d2e6dd4a9bc1da0f39b76f5a40d038393a12949..f19a7df9815cecd9f52187a96218d4d21e2fff7a 100644 (file)
@@ -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