From b1905088c3195992c345dfd35d2fbef515c3bd49 Mon Sep 17 00:00:00 2001 From: Mishalski <100153948+wywywywycloud@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:14:26 +0300 Subject: [PATCH] transport/internet/udp/dispatcher.go: Avoid info-level read failure after local session termination; Fix race (#7411) https://github.com/XTLS/Xray-core/issues/4621#issuecomment-2803983542 https://github.com/XTLS/Xray-core/pull/7411#issuecomment-6893907446 https://github.com/XTLS/Xray-core/pull/7364#issuecomment-6915895563 --------- Co-authored-by: Meow <197331664+Meo597@users.noreply.github.com> --- transport/internet/udp/dispatcher.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/transport/internet/udp/dispatcher.go b/transport/internet/udp/dispatcher.go index 963ce662a..df8a8d74f 100644 --- a/transport/internet/udp/dispatcher.go +++ b/transport/internet/udp/dispatcher.go @@ -5,6 +5,7 @@ import ( goerrors "errors" "io" "sync" + "sync/atomic" "time" "github.com/xtls/xray-core/common" @@ -24,7 +25,7 @@ type connEntry struct { link *transport.Link timer *signal.ActivityTimer cancel context.CancelFunc - closed bool + closed atomic.Bool } func (c *connEntry) Close() error { @@ -33,10 +34,9 @@ func (c *connEntry) Close() error { } func (c *connEntry) terminate() { - if c.closed { + if c.closed.Swap(true) { panic("terminate called more than once") } - c.closed = true c.cancel() common.Interrupt(c.link.Reader) common.Interrupt(c.link.Writer) @@ -77,7 +77,7 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (* } if v.conn != nil { - if v.conn.closed { + if v.conn.closed.Load() { v.conn = nil } else { return v.conn, nil @@ -144,7 +144,9 @@ func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, cal mb, err := input.ReadMultiBuffer() if err != nil { - if !goerrors.Is(err, io.EOF) { + if conn.closed.Load() { + errors.LogDebugInner(ctx, err, "UDP input closed locally") + } else if !goerrors.Is(err, io.EOF) { errors.LogInfoInner(ctx, err, "failed to handle UDP input") } return