From 902edd5adb2cd66a1a3d3dd53d10543cfe6cd9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Wed, 26 Nov 2025 21:11:54 +0000 Subject: [PATCH] Clear preConns in defer --- proxy/vless/outbound/outbound.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/proxy/vless/outbound/outbound.go b/proxy/vless/outbound/outbound.go index 6bdbe776..ea490f79 100644 --- a/proxy/vless/outbound/outbound.go +++ b/proxy/vless/outbound/outbound.go @@ -121,10 +121,6 @@ func New(ctx context.Context, config *Config) (*Handler, error) { func (h *Handler) Close() error { if h.preConnStop != nil { close(h.preConnStop) - for range h.testpre { - conn := <-h.preConns - common.CloseIfExists(conn) - } } if h.reverse != nil { return h.reverse.Close() @@ -147,6 +143,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte if h.testpre > 0 && h.reverse == nil { h.initConns.Do(func() { h.preConns = make(chan stat.Connection, h.testpre) + h.preConnWait = make(chan struct{}) h.preConnStop = make(chan struct{}) go h.preConnWorker(dialer, rec.Destination) }) @@ -472,7 +469,13 @@ func (h *Handler) preConnWorker(dialer internet.Dialer, dest net.Destination) { errors.LogError(context.Background(), "failed to dial VLESS pre connection: ", err) common.CloseIfExists(conn) } - conns <- conn + select { + case <-h.preConnStop: + common.CloseIfExists(conn) + return + default: + conns <- conn + } } go func() { go dial() // get a conn immediately @@ -485,6 +488,16 @@ func (h *Handler) preConnWorker(dialer internet.Dialer, dest net.Destination) { } } }() + defer func() { + close(h.preConns) + for { + conn, ok := <-h.preConns + if !ok { + break + } + common.CloseIfExists(conn) + } + }() for { select { case conn := <-conns: