mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-18 19:15:03 +03:00
fix(amneziawg): stop the TUN handoff from parking a peer's receiver
stackTun.WriteNotify runs on whichever goroutine made the stack emit a packet; for echo replies and RSTs that is amneziawg-go's per-peer receiver. It waited without bound for room in the outbound queue, whose reader waits on that same receiver while the device rebinds, so a peer that flooded the stack while nothing drained the queue deadlocked the next reconfiguration of the interface. The handoff now tries once, then waits at most 5 ms and drops the packet, as channel.Endpoint already does when its own queue is full.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
awgtun "github.com/amnezia-vpn/amneziawg-go/v3/tun"
|
||||
|
||||
@@ -27,6 +28,10 @@ import (
|
||||
// 1024 starved simultaneous TCP slow-starts; channel.Endpoint drops silently when full.
|
||||
const tunQueueDepth = 8192
|
||||
|
||||
// tunQueueMaxWait bounds how long WriteNotify parks the goroutine that made the stack
|
||||
// emit a packet; past it the packet is dropped, as channel.Endpoint drops when full.
|
||||
const tunQueueMaxWait = 5 * time.Millisecond
|
||||
|
||||
// maxReceiveBuffer caps per-connection receive autotuning; gVisor's own 4 MB
|
||||
// cap held a single client upload near 310 Mbit/s at 50 ms RTT.
|
||||
const maxReceiveBuffer = 8 << 20
|
||||
@@ -191,10 +196,21 @@ func (t *stackTun) WriteNotify() {
|
||||
view := pkt.ToView()
|
||||
pkt.DecRef()
|
||||
|
||||
select {
|
||||
case t.incomingPacket <- view:
|
||||
return
|
||||
default:
|
||||
}
|
||||
// Echo replies and RSTs are emitted on amneziawg-go's per-peer receiver, which the
|
||||
// reader of this queue itself waits on during a rebind: never park here for good.
|
||||
timer := time.NewTimer(tunQueueMaxWait)
|
||||
defer timer.Stop()
|
||||
// Select against done so racing dispatch abandons packet on close
|
||||
// without blocking Close or panicking on closed channel.
|
||||
select {
|
||||
case t.incomingPacket <- view:
|
||||
case <-timer.C:
|
||||
view.Release()
|
||||
case <-t.done:
|
||||
view.Release()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user