Skip to content

WireGuard inbound: Reduce memory held by idle UDP flows - #6856

Merged
RPRX merged 3 commits into
XTLS:mainfrom
nevalyashka-dev:fix/wireguard-inbound-memory
Oct 5, 2026
Merged

RPRX merged 3 commits into
XTLS:mainfrom
nevalyashka-dev:fix/wireguard-inbound-memory

Conversation

@kroticw

@kroticw kroticw commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Each UDP flow from a WireGuard peer allocates a 1024-slot channel up front and keeps it until the connection idle timeout. Clients that open many short flows (QUIC, DNS, a whole LAN behind a router) keep thousands of them alive at the same time, and these queues end up as a large share of the heap on small servers.

This replaces the channel with a slice-backed queue that only allocates for packets actually waiting and releases its storage once drained. The 1024-packet bound and the drop-when-full behaviour are unchanged. It also drops a pkt.Clone() in the UDP handler: ToSlice() already copies the payload and the clone was never released.

Measurements:

  • BenchmarkUDPManagerNewSession (new): about 10 KiB/op before, about 0.6 KiB/op after.
  • Local load test through the inbound (noKernelTun, 50 concurrent TCP downloads plus 50 new UDP flows per second for 4 minutes, about 12k live UDP flows): in-use heap after GC went from about 217 MB to about 139 MB. Throughput and the number of goroutines were unchanged.

The TUN inbound uses the same per-flow channel pattern in udp_fullcone.go; I kept this PR to the WireGuard inbound and can follow up there if this approach is acceptable.

Tests cover queue order, the bound, closing while a reader is blocked, and releasing drained storage; go test -race ./proxy/wireguard/ passes.

Every UDP flow from a peer allocated a 1024-slot channel up front (8 KiB of pointers) and kept it until the connection idle timeout. Clients that open many short flows, such as QUIC and DNS behind a router, keep thousands of them alive at once, and the queues alone took a large share of the heap.

Queue packets in a slice that only grows with packets actually waiting and is released once drained. The 1024-packet bound and the drop-when-full behaviour stay the same.

Also stop cloning the gVisor packet buffer in the UDP handler: ToSlice already copies the payload and the clone was never released.

Signed-off-by: denisdavydov <[email protected]>
@LjhAUMEM

Copy link
Copy Markdown
Contributor

pktClone 确实多余了,slice 模拟 chan 有点意思,是否更高效还存疑,现在这个如果停止了 push 而 slice 仍有数据 pop 应该会卡住,好奇会怎么解决

@LjhAUMEM

Copy link
Copy Markdown
Contributor

上面说错了,又看了一下 pop 只会为空时等待 notify,所以没问题

@kroticw

kroticw commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for double-checking pop. On efficiency I added benchmarks against a 1024 buffered channel (ns/op):

chan packetQueue
burst of 64 push+pop, 1 CPU 990 993
burst of 64 push+pop, 4 CPU 991 640
producer/consumer stream, 1 CPU 16.7 11.5
producer/consumer stream, 4 CPU 24.6 31.5

Per packet it is within a few ns either way, which is small next to the gVisor and WireGuard work per packet. The win is per flow: about 8 KiB less held by every idle flow until the idle timeout.

@kroticw
kroticw marked this pull request as ready for review September 29, 2026 04:19
@RPRX

RPRX commented Sep 29, 2026

Copy link
Copy Markdown
Member

@LjhAUMEM 可以 merge 时说一声

@LjhAUMEM

Copy link
Copy Markdown
Contributor

@kroticw 把 tun in 的 pkt clone 也删了吧,应该是在 tun/stack_gvisor.go

ToSlice() already copies the payload, and the clone was never released.

Signed-off-by: denisdavydov <[email protected]>
@kroticw

kroticw commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Done — removed the redundant pkt.Clone() in the TUN inbound's UDP handler as well: ToSlice() already copies the payload, and the clone was never released.

@RPRX
RPRX merged commit 08775af into XTLS:main Oct 5, 2026
39 of 40 checks passed
olivezane added a commit to olivezane/Xray-core-ai that referenced this pull request Oct 6, 2026
7 upstream commits: WireGuard kernelTun IPv6 table index fix (XTLS#6881),
WireGuard idle-flow memory and ICMP pong (XTLS#6856, XTLS#6855), QUIC sniffer
zero-filled tails (XTLS#6882), XDNS resolver deadlock (XTLS#7089), gRPC redial
context (XTLS#6877), scenario test process leaks (XTLS#6863).

Conflict in proxy/wireguard/tun_linux.go: took upstream's loop guard
while keeping the local errors.New lint fix.
fanymagnet pushed a commit to fanymagnet/Xray-core that referenced this pull request Oct 8, 2026
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.

4 participants