Skip to content

wgengine/netstack: Impl.Close hangs forever when an outbound queue is full #21631

Closed

Description

@dcandrade

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

  1. added a commit that references this issue on Oct 2, 2026
    0201fe6
  2. self-assigned this
    on Oct 6, 2026
  3. added 6 commits that reference this issue on Oct 6, 2026
    ed9c9fd
    08b440e
    9b05d33
    de719c4
    093c222
    8a367df
  4. added a commit that references this issue on Oct 14, 2026
    3c0508f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions