From 861d6be19a16136178245af30a6f41199593b0d2 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Tue, 22 Jun 2021 12:20:27 +0100 Subject: [PATCH] use detached context to fix #1059 #1059 It is important to use a detached context when creating shared transport connections that will outlive the initializing connection. Otherwise, once the original initializing connection is cancelled, the transport connection will be cancelled as well. This issue will be addressed in v5 by providing a transport session storage that also reduce the usage of globalDialerMap and other global variables. --- transport/internet/grpc/dial.go | 4 +++- transport/internet/http/dialer.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/transport/internet/grpc/dial.go b/transport/internet/grpc/dial.go index ae89176b5..40df80a84 100644 --- a/transport/internet/grpc/dial.go +++ b/transport/internet/grpc/dial.go @@ -4,6 +4,7 @@ package grpc import ( "context" + core "github.com/v2fly/v2ray-core/v4" gonet "net" "sync" "time" @@ -100,7 +101,8 @@ func getGrpcClient(ctx context.Context, dest net.Destination, dialOption grpc.Di return nil, err } address := net.ParseAddress(rawHost) - return internet.DialSystem(ctx, net.TCPDestination(address, port), nil) + detachedContext := core.ToBackgroundDetachedContext(ctx) + return internet.DialSystem(detachedContext, net.TCPDestination(address, port), nil) }), ) globalDialerMap[dest] = conn diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index 3f018021a..a940c1e0a 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -5,6 +5,7 @@ package http import ( "context" gotls "crypto/tls" + core "github.com/v2fly/v2ray-core/v4" "net/http" "net/url" "sync" @@ -51,7 +52,8 @@ func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.C } address := net.ParseAddress(rawHost) - pconn, err := internet.DialSystem(ctx, net.TCPDestination(address, port), nil) + detachedContext := core.ToBackgroundDetachedContext(ctx) + pconn, err := internet.DialSystem(detachedContext, net.TCPDestination(address, port), nil) if err != nil { return nil, err }