From da50e2969deec910bfac85853980249190a74d68 Mon Sep 17 00:00:00 2001 From: Shtorm <108103062+shtorm-7@users.noreply.github.com> Date: Sat, 8 Aug 2026 10:10:05 +0300 Subject: [PATCH] Fix XHTTP dialing --- transport/v2rayxhttp/dialer.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/transport/v2rayxhttp/dialer.go b/transport/v2rayxhttp/dialer.go index e2e197a1..464d4e65 100644 --- a/transport/v2rayxhttp/dialer.go +++ b/transport/v2rayxhttp/dialer.go @@ -3,6 +3,7 @@ package xhttp import ( "bytes" "context" + "errors" "fmt" "io" "net" @@ -110,15 +111,17 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, sessio if body != nil { method = c.options.GetNormalizedUplinkHTTPMethod() // stream-up/one } - req, _ := http.NewRequestWithContext(context.WithoutCancel(ctx), method, url, body) + reqCtx, cancel := context.WithCancel(context.WithoutCancel(ctx)) + req, _ := http.NewRequestWithContext(reqCtx, method, url, body) FillStreamRequest(req, sessionId, "", c.options) - wrc = &WaitReadCloser{Wait: make(chan struct{})} + wrc = &WaitReadCloser{Wait: make(chan struct{}), Cancel: cancel} go func() { resp, err := c.client.Do(req) if err != nil { - if !uploadOnly { // stream-down is enough + if !uploadOnly && !errors.Is(err, context.Canceled) { // stream-down is enough c.Close() } + cancel() gotConn.Close() common.Close(body) wrc.Close() @@ -128,6 +131,7 @@ func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, sessio io.Copy(io.Discard, resp.Body) resp.Body.Close() // if it is called immediately, the upload will be interrupted also common.Close(body) + cancel() wrc.Close() return } @@ -210,7 +214,8 @@ func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, sessio } type WaitReadCloser struct { - Wait chan struct{} + Wait chan struct{} + Cancel context.CancelFunc io.ReadCloser } @@ -233,6 +238,9 @@ func (w *WaitReadCloser) Read(b []byte) (int, error) { } func (w *WaitReadCloser) Close() error { + if w.Cancel != nil { + w.Cancel() + } if w.ReadCloser != nil { return w.ReadCloser.Close() }