Repository navigation
WireGuard inbound: Reduce memory held by idle UDP flows - #6856
Merged
Merged
Conversation
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]>
Contributor
|
pktClone 确实多余了,slice 模拟 chan 有点意思,是否更高效还存疑,现在这个如果停止了 push 而 slice 仍有数据 pop 应该会卡住,好奇会怎么解决 |
Contributor
|
|
Signed-off-by: denisdavydov <[email protected]>
Contributor
Author
|
Thanks for double-checking pop. On efficiency I added benchmarks against a 1024 buffered channel (ns/op):
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
marked this pull request as ready for review
September 29, 2026 04:19
Member
|
@LjhAUMEM 可以 merge 时说一声 |
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]>
Contributor
Author
|
Done — removed the redundant |
LjhAUMEM
approved these changes
Sep 30, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.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.