Skip to content

WireGuard proxy: Fix potential startup races - #6852

Merged
RPRX merged 2 commits into
XTLS:mainfrom
LagPixelLOL:fix/wireguard-init-races
Sep 29, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
LagPixelLOL:fix/wireguard-init-races

Conversation

@LagPixelLOL

Copy link
Copy Markdown
Contributor

Note: Both the bug and the following is written by Claude Opus 5.5, but I DID review everything and do think what it found is valid.

Fixed #6850.

Fix: WireGuard startup races

Branch: fix/wireguard-init-races, based on origin/main e5e85ca9. It is independent of the freedom fix: the two touch no files in common.

Commits

5af3adac WireGuard outbound: Fix data race on bind fields during device creation

Files: proxy/wireguard/client.go, proxy/wireguard/bind.go

  • resolveFunc, listenFunc and reserved are set in the bind{} literal, before device.NewDevice.
  • downFunc needs the device, so it is set afterwards through a new setDownFunc() that holds bind.mu. The receive path reads it under the same lock.
  • No deadlock with Close(): Close closes closeCh before the socket, so the receive path's close branch returns without taking the lock.

e3156c50 WireGuard inbound: Install gVisor protocol handlers before the device is up

File: proxy/wireguard/server.go

  • CreateForwarder moved from the end of Start() into NewServer(), after the Server struct is built and before any WireGuard device exists. The MASQUE inbound already does it this way.
  • Goroutine creation then orders the handler writes before every device goroutine, as gVisor requires.

Tests

The tests are on branch tests/udp-fullcone-wireguard-races (https://github.com/LagPixelLOL/Xray-core); nothing was added to the fix branches. To run them:

./run.sh -r 5 /path/to/Xray-core v26.9.9 main fix/wireguard-init-races

Outbound race: -race on the WireGuard UDP tests, run as separate processes, because the race detector reports a given race only once per process.

Tree Processes reporting the race
v26.9.9, main 5/5
fix 0/5, plus 0 in 8 more processes (160 runs)

Inbound race: TestWireGuardInboundHandlersInstalledBeforeDeviceUp, with a patched gVisor that pauses the handler install.

before after (fix)
Handler installed during Instance.Start, device up core.New, no device yet
WireGuard port open during install true (20/20) false (20/20)
Packets reaching gVisor with no handler 30/30 dropped (20/20) 0 (20/20)
Test result FAIL PASS
-race, separate processes reported 15/15 0/15

Other:

  • Both fixes merged (run.sh -r 5 on a working tree): all 6 tests pass, and both race rows are 0/5.
  • Fix branch alone: the WireGuard UDP full-cone tests still fail, because traffic leaves through freedom, which is fixed separately on fix/freedom-udp-fullcone.
  • Repo suites pass, as does testing/scenarios TestWireguard (a startup-only test).

Not covered: WireGuard kernel-TUN mode (noKernelTun: false), which needs /dev/net/tun.

device.NewDevice may call Up -> BindUpdate -> bind.Open right away (e.g.
on the TUN's initial EventUp), but resolveFunc, listenFunc, downFunc and
reserved were assigned after NewDevice returned. Set them before creating
the device; downFunc needs the device, so set and read it under bind.mu.
… is up

CreateForwarder (SetTransportProtocolHandler) ran at the end of Start,
after IpcSet had started the peer goroutines and dev.Up had opened the
bind, so received packets could race with the handler setup. gVisor only
supports setting these handlers during stack initialization, so do it in
NewServer, before the device exists.
@RPRX
RPRX requested a review from LjhAUMEM September 28, 2026 04:51
@LjhAUMEM

Copy link
Copy Markdown
Contributor

出站修了个幻觉需要删掉,入站 CreateForwarder 移到和 tun 创建一起可以保留

@LjhAUMEM

LjhAUMEM commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

翻了一下 NewDevice,还真的可能自己 Up,https://github.com/WireGuard/wireguard-go/blob/ecfc5a8d54462e18e13c72173e2623d16d8e25a0/device/tun.go#L41-L44,之前以为只有外部 IpcSet/Up 才会动 bind 的 Open,那这修复没问题

奇怪之前翻的时候怎么没看到

patterniha pushed a commit to patterniha/Xray-core that referenced this pull request Sep 29, 2026
Early merge of XTLS#6852 (PR head e3156c5), squashed, ahead of
it landing upstream: XTLS#6852
@RPRX RPRX changed the title Fix: WireGuard startup races WireGuard proxy: Fix potential startup races Sep 29, 2026
@RPRX
RPRX merged commit 61c1068 into XTLS:main Sep 29, 2026
40 checks passed
GFW-knocker pushed a commit to GFW-knocker/Xray-core that referenced this pull request Oct 2, 2026
XTLS#6852 (comment)

Fixes XTLS#6850

(cherry picked from commit 08cb6e6)

[GFW-knocker: hand-merged. The fork's wnoise fields (Wnoise, Wheader and the
count/delay/payload ranges) were also assigned after device.NewDevice; they
are read by Get_extra_data for the first handshake, so they now go into the
bind literal together with resolveFunc/listenFunc/reserved. Server side calls
the fork's createForwarder (not yet renamed upstream's way).]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WireGuard startup races (outbound bind, inbound gVisor handlers)

3 participants