Browse Source

tun: darwin: ignore ENOMEM errors

Coauthored-by: Andrej Mihajlov <and@mullvad.net>
Jason A. Donenfeld 2 months ago
parent
commit
caebdfe9d0
1 changed files with 18 additions and 1 deletions
  1. 18
    1
      tun/tun_darwin.go

+ 18
- 1
tun/tun_darwin.go View File

@@ -11,6 +11,7 @@ import (
11 11
 	"net"
12 12
 	"os"
13 13
 	"syscall"
14
+	"time"
14 15
 	"unsafe"
15 16
 
16 17
 	"golang.org/x/net/ipv6"
@@ -42,6 +43,22 @@ type NativeTun struct {
42 43
 
43 44
 var sockaddrCtlSize uintptr = 32
44 45
 
46
+func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
47
+	for i := 0; i < 20; i++ {
48
+		iface, err = net.InterfaceByIndex(index)
49
+		if err != nil {
50
+			if opErr, ok := err.(*net.OpError); ok {
51
+				if syscallErr, ok := opErr.Err.(*os.SyscallError); ok && syscallErr.Err == syscall.ENOMEM {
52
+					time.Sleep(time.Duration(i) * time.Second / 3)
53
+					continue
54
+				}
55
+			}
56
+		}
57
+		return iface, err
58
+	}
59
+	return nil, err
60
+}
61
+
45 62
 func (tun *NativeTun) routineRouteListener(tunIfindex int) {
46 63
 	var (
47 64
 		statusUp  bool
@@ -74,7 +91,7 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
74 91
 			continue
75 92
 		}
76 93
 
77
-		iface, err := net.InterfaceByIndex(ifindex)
94
+		iface, err := retryInterfaceByIndex(ifindex)
78 95
 		if err != nil {
79 96
 			tun.errors <- err
80 97
 			return

Loading…
Cancel
Save