Skip to content

wgengine/netstack: full outbound queue can deadlock the TUN reader in 1.98 #21638

Open

Description

@theskcd

What is the issue?

The netstack outbound packet queue can deadlock the sole TUN reader when synchronous packet processing generates a TCP reset while the queue is full.

The dependency cycle is:

  1. tstun.Wrapper.Read calls netstack.handleLocalPackets.
  2. gVisor synchronously generates a TCP reset and calls the link endpoint's queue.Write.
  3. The outbound queue is full, so queue.Write waits for space.
  4. Impl.inject, which drains that queue, is waiting in tstun.injectOutbound for the same TUN reader to consume vectorOutbound.

Neither side can progress. The expected behavior is bounded packet loss under saturation, allowing the reader and injector to keep processing subsequent traffic.

This is distinct from the shutdown-ordering failure in #21631: the reproduction here reaches the circular wait during ordinary packet processing, with the stack, wrapper, and queue still open and the context not canceled.

Steps to reproduce

A deterministic in-process reproduction uses a real tstun.Wrapper, a fake TUN device, the netstack local-packet hook, and a small real gVisor link endpoint queue. An ACK to an unhandled service VIP generates the synchronous reset; a SYN is unsuitable because the TCP forwarder handles it asynchronously.

Pause the sole reader on entry to the hook, fill its outbound injection slot, start the injector until it blocks on that reader, and fill the netstack queue. Release the reader. On the blocking implementation, both goroutines remain stuck until teardown. Making full-queue writes return ErrNoBufferSpace lets the reader return, the queue drain, and subsequent traffic progress before cancellation or close.

Tailscale version

Reproduced against v1.98.9 source commit 532ee02a0c57e5883f842d3e60bd37d7787617c3. The initial traffic condition that triggered queue saturation is not established.

The current release-branch/1.98 head, 36550d57f4a4055246ef7412f4e650a012a465f1, has identical link_endpoint.go and netstack.go files. Current main has since split outbound traffic into destination queues in #21122; the original dependency-cycle reproduction does not establish the same deadlock on that newer architecture. The proposed patch is scoped to the affected 1.98 release branch.

Proposed fix

Use a nonblocking full-queue branch in queue.Write, returning tcpip.ErrNoBufferSpace. The select evaluates pkt.IncRef() before selecting a case, so the rejected branch must balance it with pkt.DecRef().

Packet-reference and concurrent-close tests passed with this change. A real in-memory gVisor TCP transfer delivered all bytes unchanged after forced queue drops and retransmissions. The tradeoff is packet loss under saturation: TCP can retransmit, while non-retrying protocols may lose datagrams.

Blocking was deliberately introduced by 4903d6c80bba6de3e6b70f24ee80169ed4ac4e68 to improve burst behavior. This proposal trades that backpressure for forward progress on the affected single-queue architecture; it is not a throughput improvement claim.

OS

Observed on Linux; the in-process regression and race tests also run on macOS/arm64.

Activity

  1. added theissue type on Oct 5, 2026
  2. nickoneill commented on Oct 5, 2026

    @nickoneill
    Contributor

    This is a couple releases behind at this point, can you point your AI at a more modern branch for diagnosis? Or just try 1.102 or 1.104 when it's released later this week.

  3. theskcd commented on Oct 5, 2026

    @theskcd
    Author

    will do!

  4. bcreane commented on Oct 6, 2026

    @bcreane
    Contributor

    This issue should be fixed by #21668. When that PR lands, and when the decision is made about which ticket to backport the fix to, we can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions