Repository navigation
Description
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.
Bug: WireGuard startup races (outbound bind, inbound gVisor handlers)
Affected: main and v26.9.9. Both are older than #6754.
1. Outbound: bind fields set after the device starts using them
In proxy/wireguard/client.go:291-310, (*Handler).init creates an empty bind{} and calls device.NewDevice(h.tun, bind, logger). Only after that does it assign resolveFunc, listenFunc, downFunc and reserved.
NewDevice starts goroutines that can bring the device up straight away (TUN EventUp → Up → BindUpdate → bind.Open). bind.Open then calls b.listenFunc() (bind.go:37), so it can:
- read these fields without synchronization (a data race);
- call a still-nil
listenFuncand panic.
Observed: go test -race reports (*bind).Open against (*Handler).init in every separate test process (5/5 on v26.9.9, main and the freedom branch). The race detector reports a given race only once per process, so repeating the test inside one process makes it look rarer than it is.
2. Inbound: gVisor handlers installed after the device is up
(*Server).Start (proxy/wireguard/server.go) runs in this order:
dev.IpcSet(:314) starts the peer goroutines.dev.Up(:318) opens the UDP socket.- Only then does
CreateForwarder(:323) callSetTransportProtocolHandler.
Packets can therefore reach the gVisor stack before its handlers are installed. gVisor says this function "must be called only during initialization of the stack. Changing it as the stack is operating is not supported."
Impact: a data race on the stack's handler field, and packets that arrive in the window are dropped (no handler, no endpoint).
Observed: in normal tests the window is only microseconds wide and incidental synchronization hides the race. The race detector caught it in only 2 of 32 test processes.
Deterministic reproduction: a test using a gVisor copy with test-only hooks. It pauses the server inside SetTransportProtocolHandler and runs a WireGuard client during the pause. Before the fix, 20 of 20 runs showed:
- the WireGuard UDP port was already open while the handler was being installed;
- all 30 UDP packets sent during the pause reached the stack with no handler and were dropped;
- with
-race, the race was reported in 15 of 15 separate processes.
How to check
Use the tests on branch tests/udp-fullcone-wireguard-races (https://github.com/LagPixelLOL/Xray-core). WireGuard runs in userspace, so no root or TUN device is needed.
./run.sh -r 5 /path/to/Xray-core v26.9.9 mainOn both refs:
TestWireGuardInboundHandlersInstalledBeforeDeviceUpfails. It useswginit/withpatches/gvisor-test-hooks.patch, whichrun.shapplies to a temporary gVisor copy.- Both race rows report 5/5:
- "WireGuard udp tests (outbound bind race)";
- "WireGuard inbound, paused (handler race)".
Activity
[-]Bug: WireGuard startup races (outbound bind, inbound gVisor handlers)[/-][+]WireGuard startup races (outbound bind, inbound gVisor handlers)[/+]LjhAUMEM commentedon Sep 28, 2026
这不纯幻觉么
WireGuard proxy: Fix potential startup races (#6852)
WireGuard proxy: Fix potential startup races (#6852)
WireGuard proxy: Fix potential startup races (XTLS#6852)