From b22eeb86e29fdf964454902a7a50949033ddc702 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 9 Dec 2021 22:52:11 +0800 Subject: [PATCH] [Fixed] Add retry to open tun --- listener/tun/tun_adapter.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/listener/tun/tun_adapter.go b/listener/tun/tun_adapter.go index ce771c42..3f232dab 100644 --- a/listener/tun/tun_adapter.go +++ b/listener/tun/tun_adapter.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "time" "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/config" @@ -24,7 +25,17 @@ func New(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.Pack device, err := dev.OpenTunDevice(tunAddress, autoRoute) if err != nil { - return nil, fmt.Errorf("can't open tun: %v", err) + for i := 1; i < 3; i++ { + time.Sleep(time.Second * 1) + device, err = dev.OpenTunDevice(tunAddress, autoRoute) + if err == nil { + break + } + } + + if err != nil { + return nil, fmt.Errorf("can't open tun: %v", err) + } } mtu, err := device.MTU()