#6870·Xray-core

Mux client: data race on Session.input/output between ClientWorker.Dispatch and SessionManager.Close

Author: SakuraBurstCreated Sep 30, 2026Updated Sep 30, 2026

Integrity requirements

  • I have read all the comments in the issue template and ensured that this issue meet the requirements.
  • I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • I provided the complete config and logs, rather than just providing the truncated parts based on my own judgment.
  • I searched issues and did not find any similar issues.
  • The problem can be successfully reproduced in the latest Release

Description

A race-enabled build of Xray reports a data race in the Mux.Cool client (common/mux). It happens when the mux connection fails right after a new sub-connection has been dispatched onto it. For example, the server, or anything on the path, accepts the TCP connection and closes it right away.

ClientWorker.Dispatch gets a new session from SessionManager.Allocate. Allocate puts the session into m.sessions under the manager lock and returns. After that, with no lock held, Dispatch sets the session's I/O fields:

go
// common/mux/client.go:317-322 (v26.3.27; same lines on main)
s := sm.Allocate(&m.strategy)
if s == nil {
	return false
}
s.input = link.Reader
s.output = link.Writer

When the mux connection dies, monitor() calls SessionManager.Close() (client.go:234). It calls Session.Close(true) for every session in the map, and that reads the same fields (session.go:183-184):

go
if s.XUDP == nil {
	common.Interrupt(s.input)
	common.Close(s.output)

The manager lock orders this read after Allocate, but not after the two assignments in Dispatch. The worker is closed from another goroutine: either the p.Process goroutine started in DialingWorkerFactory.Create (client.go:151-166) or fetchOutput on EOF. That goroutine was started before Dispatch and does not synchronize with the dispatching goroutine afterwards.

The server side does not have this problem: ServerWorker.handleStatusNew (server.go:271-281) fills input/output before SessionManager.Add.

Effect beyond the race report: if SessionManager.Close runs between Allocate and the two assignments, it sees nil input/output, so common.Interrupt and common.Close do nothing. The deferred s.Close(false) in fetchInput (client.go:272) then returns early, because the session is already closed.

  • Inbounds that use Dispatcher.Dispatch (pipe links, e.g. VMess, Trojan, Shadowsocks, plain HTTP): nothing else closes the pipe link, so the connection stays open until the inbound's idle timeout.
  • Inbounds that use DispatchLink (e.g. SOCKS, VLESS, TUN, dokodemo-door, HTTP CONNECT): Dispatch waits for s.done, and the inbound closes the connection itself.

The window is a few instructions wide, and I have not hit it on its own. With time.Sleep(50 * time.Millisecond) added right after Allocate in Dispatch, the regression test from the fix PR (TestClientWorkerDispatchRace) fails on main without -race in 10 of 10 runs, because the downlink is never closed. With the fix and the same sleep it passes in 10 of 10.

This looks different from #5901 and #6822, which are about the reused UDP destination (b.UDP / meta.Target).

Versions, 10 runs of the steps below on each, go1.27.1 linux/amd64:

  • v26.3.27 (latest release, d2758a0): reproduced in 10 of 10 runs.
  • v26.9.9 (pre-release, 52a412d): 10 of 10.
  • main at 6243d2a: 10 of 10. Dispatch, SessionManager.Allocate and Session.Close are unchanged there, with the same line numbers.

Expected: no data race. A session's input/output are set before SessionManager.Close can see the session.

Actual: WARNING: DATA RACE on Session.input (write client.go:321, read session.go:183) and on Session.output (write client.go:322, read session.go:184).

Reproduction Method

  1. Build Xray with the race detector: go build -race -o xray ./main.
  2. Start a TCP listener on 127.0.0.1:10809 that accepts every connection and closes it right away (see Server config).
  3. Run ./xray run -c config.json with the client config below.
  4. Send a few requests through the SOCKS inbound. Each of them fails, which is expected here:
    for i in 1 2 3 4 5; do curl -s -o /dev/null -x socks5h://127.0.0.1:10808 http://example.com/; sleep 0.2; done
  5. The race report shows up on stderr while the requests run. Stop Xray with Ctrl+C: the race detector prints Found 2 data race(s), and the exit code is 66.

Keep the default log level. With "loglevel": "debug" I did not get the report. Every log line is a write syscall, and the race detector treats syscall reads and writes as synchronization (ioSync in package syscall), which hides this race.

Client config


{
  "inbounds": [{"listen": "127.0.0.1", "port": 10808, "protocol": "socks"}],
  "outbounds": [{"protocol": "vless", "mux": {"enabled": true},
    "settings": {"vnext": [{"address": "127.0.0.1", "port": 10809,
      "users": [{"id": "b831381d-6324-4d53-ad4f-8cda48b30811", "encryption": "none"}]}]}}]
}

Server config


No Xray server. 127.0.0.1:10809 is a plain TCP listener that accepts every connection and closes it right away, for example:

python3 -c 'import socket
s = socket.create_server(("127.0.0.1", 10809))
while True:
    s.accept()[0].close()'

Client log

Complete output of the race-enabled v26.3.27 build for the steps above. Only the checkout and GOROOT prefixes of file paths are removed.


Xray 26.3.27 (Xray, Penetrates Everything.) d2758a0 (go1.27.1-X:nodwarf5 linux/amd64)
A unified platform for anti-censorship.
2026/09/30 00:15:21.960992 [Info] infra/conf/serial: Reading config: &{Name:config.json Format:json}
2026/09/30 00:15:21.962530 [Warning] core: Xray 26.3.27 started
==================
WARNING: DATA RACE
Read at 0x00c00013e100 by goroutine 37:
  github.com/xtls/xray-core/common/mux.(*Session).Close()
      common/mux/session.go:183 +0x1ca
  github.com/xtls/xray-core/common/mux.(*SessionManager).Close()
      common/mux/session.go:149 +0x208
  github.com/xtls/xray-core/common/mux.(*ClientWorker).monitor()
      common/mux/client.go:234 +0x295
  github.com/xtls/xray-core/common/mux.NewClientWorker.gowrap2()
      common/mux/client.go:200 +0x2e

Previous write at 0x00c00013e100 by goroutine 35:
  github.com/xtls/xray-core/common/mux.(*ClientWorker).Dispatch()
      common/mux/client.go:321 +0xc4
  github.com/xtls/xray-core/common/mux.(*ClientManager).Dispatch()
      common/mux/client.go:36 +0x86
  github.com/xtls/xray-core/app/proxyman/outbound.(*Handler).Dispatch()
      app/proxyman/outbound/handler.go:237 +0x1526
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).routedDispatch()
      app/dispatcher/default.go:518 +0x107d
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).DispatchLink()
      app/dispatcher/default.go:358 +0x55a
  github.com/xtls/xray-core/common/mux.(*Server).DispatchLink()
      common/mux/server.go:64 +0x10c
  github.com/xtls/xray-core/proxy/socks.(*Server).processTCP()
      proxy/socks/server.go:163 +0x1395
  github.com/xtls/xray-core/proxy/socks.(*Server).Process()
      proxy/socks/server.go:96 +0x604
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:130 +0x10e6
  context.(*cancelCtx).propagateCancel()
      context/context.go:479 +0xa5
  context.withCancel()
      context/context.go:279 +0xa4
  context.WithCancel()
      context/context.go:242 +0x26
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:63 +0x74
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1.gowrap1()
      app/proxyman/inbound/worker.go:151 +0x46

Goroutine 37 (running) created at:
  github.com/xtls/xray-core/common/mux.NewClientWorker()
      common/mux/client.go:200 +0x3e4
  github.com/xtls/xray-core/common/mux.(*DialingWorkerFactory).Create()
      common/mux/client.go:143 +0x112
  github.com/xtls/xray-core/common/mux.(*IncrementalWorkerPicker).pickInternal()
      common/mux/client.go:103 +0x759
  github.com/xtls/xray-core/common/mux.(*IncrementalWorkerPicker).PickAvailable()
      common/mux/client.go:120 +0x2e
  github.com/xtls/xray-core/common/mux.(*ClientManager).Dispatch()
      common/mux/client.go:32 +0x6d
  github.com/xtls/xray-core/app/proxyman/outbound.(*Handler).Dispatch()
      app/proxyman/outbound/handler.go:237 +0x1526
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).routedDispatch()
      app/dispatcher/default.go:518 +0x107d
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).DispatchLink()
      app/dispatcher/default.go:358 +0x55a
  github.com/xtls/xray-core/common/mux.(*Server).DispatchLink()
      common/mux/server.go:64 +0x10c
  github.com/xtls/xray-core/proxy/socks.(*Server).processTCP()
      proxy/socks/server.go:163 +0x1395
  github.com/xtls/xray-core/proxy/socks.(*Server).Process()
      proxy/socks/server.go:96 +0x604
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:130 +0x10e6
  context.(*cancelCtx).propagateCancel()
      context/context.go:479 +0xa5
  context.withCancel()
      context/context.go:279 +0xa4
  context.WithCancel()
      context/context.go:242 +0x26
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:63 +0x74
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1.gowrap1()
      app/proxyman/inbound/worker.go:151 +0x46

Goroutine 35 (running) created at:
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1()
      app/proxyman/inbound/worker.go:151 +0xd9
  github.com/xtls/xray-core/transport/internet/tcp.(*Listener).keepAccepting.func1()
      transport/internet/tcp/hub.go:128 +0x6ee
==================
==================
WARNING: DATA RACE
Read at 0x00c00013e110 by goroutine 37:
  github.com/xtls/xray-core/common/mux.(*Session).Close()
      common/mux/session.go:184 +0x21b
  github.com/xtls/xray-core/common/mux.(*SessionManager).Close()
      common/mux/session.go:149 +0x208
  github.com/xtls/xray-core/common/mux.(*ClientWorker).monitor()
      common/mux/client.go:234 +0x295
  github.com/xtls/xray-core/common/mux.NewClientWorker.gowrap2()
      common/mux/client.go:200 +0x2e

Previous write at 0x00c00013e110 by goroutine 35:
  github.com/xtls/xray-core/common/mux.(*ClientWorker).Dispatch()
      common/mux/client.go:322 +0x144
  github.com/xtls/xray-core/common/mux.(*ClientManager).Dispatch()
      common/mux/client.go:36 +0x86
  github.com/xtls/xray-core/app/proxyman/outbound.(*Handler).Dispatch()
      app/proxyman/outbound/handler.go:237 +0x1526
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).routedDispatch()
      app/dispatcher/default.go:518 +0x107d
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).DispatchLink()
      app/dispatcher/default.go:358 +0x55a
  github.com/xtls/xray-core/common/mux.(*Server).DispatchLink()
      common/mux/server.go:64 +0x10c
  github.com/xtls/xray-core/proxy/socks.(*Server).processTCP()
      proxy/socks/server.go:163 +0x1395
  github.com/xtls/xray-core/proxy/socks.(*Server).Process()
      proxy/socks/server.go:96 +0x604
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:130 +0x10e6
  context.(*cancelCtx).propagateCancel()
      context/context.go:479 +0xa5
  context.withCancel()
      context/context.go:279 +0xa4
  context.WithCancel()
      context/context.go:242 +0x26
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:63 +0x74
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1.gowrap1()
      app/proxyman/inbound/worker.go:151 +0x46

Goroutine 37 (running) created at:
  github.com/xtls/xray-core/common/mux.NewClientWorker()
      common/mux/client.go:200 +0x3e4
  github.com/xtls/xray-core/common/mux.(*DialingWorkerFactory).Create()
      common/mux/client.go:143 +0x112
  github.com/xtls/xray-core/common/mux.(*IncrementalWorkerPicker).pickInternal()
      common/mux/client.go:103 +0x759
  github.com/xtls/xray-core/common/mux.(*IncrementalWorkerPicker).PickAvailable()
      common/mux/client.go:120 +0x2e
  github.com/xtls/xray-core/common/mux.(*ClientManager).Dispatch()
      common/mux/client.go:32 +0x6d
  github.com/xtls/xray-core/app/proxyman/outbound.(*Handler).Dispatch()
      app/proxyman/outbound/handler.go:237 +0x1526
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).routedDispatch()
      app/dispatcher/default.go:518 +0x107d
  github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).DispatchLink()
      app/dispatcher/default.go:358 +0x55a
  github.com/xtls/xray-core/common/mux.(*Server).DispatchLink()
      common/mux/server.go:64 +0x10c
  github.com/xtls/xray-core/proxy/socks.(*Server).processTCP()
      proxy/socks/server.go:163 +0x1395
  github.com/xtls/xray-core/proxy/socks.(*Server).Process()
      proxy/socks/server.go:96 +0x604
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:130 +0x10e6
  context.(*cancelCtx).propagateCancel()
      context/context.go:479 +0xa5
  context.withCancel()
      context/context.go:279 +0xa4
  context.WithCancel()
      context/context.go:242 +0x26
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback()
      app/proxyman/inbound/worker.go:63 +0x74
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1.gowrap1()
      app/proxyman/inbound/worker.go:151 +0x46

Goroutine 35 (running) created at:
  github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1()
      app/proxyman/inbound/worker.go:151 +0xd9
  github.com/xtls/xray-core/transport/internet/tcp.(*Listener).keepAccepting.func1()
      transport/internet/tcp/hub.go:128 +0x6ee
==================
Found 2 data race(s)

Server log


N/A (no Xray server).