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