Repository navigation
Description
What is the issue?
wgengine/netstack.(*Impl).Close can block forever, and tsnet.Server.Close with it.
Impl.Close cancels ns.ctx first, which stops the goroutines reading linkEP's outbound queues (injectToWireGuard and the others use ReadContext(ns.ctx, ...)). It then calls ipstack.Close(), which runs Abort() on every registered endpoint. Aborting a connected TCP endpoint writes a RST into an outbound queue (resetConnectionLocked). The queues are only closed later, from ipstack.Wait() (via removeNICLocked → linkEndpoint.Close).
Between those two calls nothing reads the queue. Once a queue is full (512 packets), queue.Write blocks on q.c <- pkt until closedCh is closed, and that never happens because Wait is never reached. The write blocks while holding the TCP endpoint's lock, so every other goroutine that touches that endpoint (retransmit timers, handshake timers, gonet Close) also parks on the endpoint's CrossGoroutineMutex.
Result: Close never returns and the whole tsnet.Server stays alive (engine, magicsock, DERP). Since WireGuard keeps delivering inbound packets to the zombie stack, the goroutine count keeps growing.
Steps to reproduce
The repro is a unit test in the linked PR (TestCloseWithFullOutboundQueue, in wgengine/netstack). It establishes a self-addressed TCP connection, cancels ns.ctx as Close does, fills the outbound queue, then calls ns.Close(). On current main Close never returns. It is stuck at:
gvisor.dev/gvisor/pkg/tcpip/transport/tcp.(*Endpoint).LockUser
gvisor.dev/gvisor/pkg/tcpip/transport/tcp.(*Endpoint).Abort
gvisor.dev/gvisor/pkg/tcpip/stack.(*Stack).Close
tailscale.com/wgengine/netstack.(*Impl).Close
with the aborting goroutine itself parked in tailscale.com/wgengine/netstack.(*queue).Write.
Seen in production
We run one tsnet.Server per peer in a long-lived proxy, and servers are closed and recreated as peers come and go. On tailscale v1.96.2, about 30% of Server.Close calls never returned. A single process reached ~75k goroutines and was OOM-killed at its 6 GiB limit. Of those goroutines, 22k were parked in queue.Write from retransmitTimerExpired, 4.7k in queue.Write from handshake timers, and 206 in Stack.Close → Abort → queue.Write. The count of servers stuck in Close (207) matched the extra tstun pump goroutines exactly. Peers behind saturated NATs made it far more frequent: more half-open connections meant more RSTs at teardown.
The same ordering is still there on main: injectWG.Wait() was added, but not a linkEP.Close() before ipstack.Close().
Are there any recent changes that introduced the issue?
Not that we know of. We first saw it on v1.96.2, and the ordering is unchanged on main.
OS
Linux
OS version
GKE (Kubernetes)
Tailscale version
1.96.2 (tsnet)
Activity
wgengine/netstack: close linkEP before aborting endpoints in Close
net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues
net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues
net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues
net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues
net/tstun,feature/conn25: test inbound replies against a full injecti…
net/tstun,feature/conn25: test inbound replies against a full injecti…
net/tstun,feature/conn25: test inbound replies against a full injecti…
net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues