Skip to content

wgengine/netstack: avoid blocking writes on a full queue - #21639

Closed
theskcd wants to merge 1 commit into
tailscale:release-branch/1.98from
theskcd:netstack-nonblocking-queue-1.98
Closed

theskcd wants to merge 1 commit into
tailscale:release-branch/1.98from
theskcd:netstack-nonblocking-queue-1.98

Conversation

@theskcd

@theskcd theskcd commented Oct 3, 2026 •

Copy link
Copy Markdown

Summary

Fixes #21638.

Make queue.Write reject a packet immediately with ErrNoBufferSpace when the outbound queue is full, and release the reference taken while evaluating the select send expression.

On the 1.98 single-queue implementation, synchronous TCP reset generation can block the sole TUN reader in queue.Write, while the queue-draining injector waits for that same reader to consume an outbound injection. Dropping the overflowing packet breaks that dependency cycle and allows subsequent traffic to progress.

This PR intentionally targets release-branch/1.98. Its current link_endpoint.go and netstack.go match the v1.98.9 source used for the dependency-cycle reproduction. main has since split traffic into separate destination queues in #21122; this PR does not claim the same cycle is reproduced on that newer architecture.

Scope and tradeoff

Only wgengine/netstack/link_endpoint.go and focused queue regression tests are changed. No routing, queue capacity, dependencies, configuration, or packaging changes are included.

Blocking was introduced in 4903d6c80bba6de3e6b70f24ee80169ed4ac4e68 for burst performance. This change trades that backpressure for progress on the affected release: TCP can retransmit, but saturation can reduce throughput and non-retrying protocols can lose packets. It is not a throughput improvement claim.

Validation

The new tests cover accepted packet ownership, full-queue rejection, draining, closed and close-signaled writes, and concurrent writes/close.

Before applying the production fix, GOMAXPROCS=2 ./tool/go test -race -p 2 ./wgengine/netstack -run '^TestQueueWriteFull$' -count=1 -timeout=120s failed with Write blocked on a full queue for 2s. Its failure path closes the queue and joins the writer.

With the fix, GOMAXPROCS=2 ./tool/go test -race -p 2 ./wgengine/netstack ./net/tstun -count=1 -timeout=120s passed (26.214s and 1.306s respectively). gofmt and git diff --check also passed.

The separately exercised v1.98.9 TUN-reader/injector reproduction stalled on the original implementation and progressed with this patch before any teardown. A real in-memory TCP transfer also delivered 16,384 bytes unchanged after forced queue drops and retransmissions.

Requested by @theskcd; for @tailscale/dev review. GitHub denied the publishing account permission to attach a formal team review request, so the requested-reviewer list is empty. The publishing account is @theskcd, so a self-review request is not possible.

A synchronous TCP reset can originate from the sole TUN reader while
outbound injection waits for that same reader. On the 1.98 single-queue
implementation, blocking on a full queue can deadlock both paths.

Reject overflow with ErrNoBufferSpace and release the speculative packet
reference taken by the select send expression. Add regression coverage for
full-queue rejection, packet ownership, draining, and concurrent close.

This trades backpressure under saturation for continued packet processing.
TCP can retransmit dropped packets; non-retrying protocols may lose them.

Fixes tailscale#21638

Change-Id: Ibd17c73c69c104ca156f7cf33e1b830308228eed
RELNOTE: Avoid a netstack packet-queue deadlock in the 1.98 release branch.
Signed-off-by: theskcd <[email protected]>
@sfllaw

sfllaw commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

This will probably be superceded by PR #21668, which fixes this problem more generally in the main branch.

raggi added a commit that referenced this pull request Oct 6, 2026
gVisor can synchronously generate replies on a TUN reader or loopback consumer. Make link endpoint queue writes nonblocking so saturation cannot stop the goroutine needed to drain the queue. TCP can retransmit; UDP and other non-retrying traffic may lose packets under load.

Count all PacketBuffers rejected by queue saturation, including the unaccepted batch tail. Expose the same per-netstack counter through user-facing and debug metrics, and sum it across netstacks for the client-metrics pipeline.

Cancel pending tstun injections during netstack shutdown, close link queues before aborting endpoints, and stop the WireGuard injection pump on a closed Wrapper. Release rejected and canceled references, drain unread injections on Wrapper close, and release capture buffers.

Keep PR #21665 multiqueue semantics: injections are multiplexed only onto queue 0. Cover independent native-reader progress, concurrent reply saturation and drop accounting, cancellation, shutdown, ownership, loopback progress, and TCP recovery.

Fixes #21631
Updates #21638
Related: #21632, #21639

Signed-off-by: James Tucker <[email protected]>
raggi added a commit that referenced this pull request Oct 7, 2026
gVisor can synchronously generate replies on a TUN reader or loopback consumer. Make link endpoint queue writes nonblocking so saturation cannot stop the goroutine needed to drain the queue. TCP can retransmit; UDP and other non-retrying traffic may lose packets under load.

Count all PacketBuffers rejected by queue saturation, including the unaccepted batch tail. Expose the same per-netstack counter through user-facing and debug metrics, and sum it across netstacks for the client-metrics pipeline.

Cancel pending tstun injections during netstack shutdown, close link queues before aborting endpoints, and stop the WireGuard injection pump on a closed Wrapper. Release rejected and canceled references, drain unread injections on Wrapper close, and release capture buffers.

Adapt cancellation and shutdown draining to PR #21681's independent
injection queue. Signal shutdown before acquiring the sender semaphore,
then close the queue under that semaphore so readers can receive directly
without a per-packet shutdown select. Drain unread packet references after
teardown, outside closeOnce, to preserve reentrant release callbacks.

Cover closure broadcasts before and after Start, sender/Close ordering,
late-send rejection, concurrent readers and drainers, native-reader
progress, reply saturation and drop accounting, cancellation, ownership,
loopback progress, and TCP recovery.

Fixes #21631
Updates #21638
Related: #21632, #21639

Copy injected PacketBuffer payloads directly from backing views into the
WireGuard slab instead of allocating and copying an intermediate slice.
Cover fragmented payloads, raw/pushed/consumed header layouts, reserved
header space, GSO segmentation, and non-mutating packet reuse.

Signed-off-by: James Tucker <[email protected]>
raggi added a commit that referenced this pull request Oct 7, 2026
Make link queue writes nonblocking: gVisor can generate replies on the
reader needed to drain the queue. A full queue drops only the rejected
packet and reports ErrNoBufferSpace, which gVisor counts and TCP handles
by arming its retransmit timer. UDP sockets do not see the error unless
IP_RECVERR is set, matching Linux. Count WireGuard-bound drops as
reason="queue_full" in tailscaled_outbound_dropped_packets_total, and
per-queue drops in debug and client metrics.

Add Wrapper.TryInjectOutbound for best-effort replies generated while
processing packets (TSMP rejects and pongs, conn25 rejects, fake-mode
echo replies), so those paths drop instead of stalling behind the
injection queue's reader. Its drops are also counted as queue_full.

Make outbound injection waits cancellable and close link queues before
abort-generated replies. Stop the WireGuard injection pump on ErrClosed.
Release injected packets still queued at Close, outside closeOnce to
allow reentrant release callbacks; a sender whose send races with Close
releases the packet itself. Release temporary capture buffers.

Copy injected payload views directly into the WireGuard slab, avoiding
an intermediate allocation and copy.

Test queue saturation, multiqueue progress, cancellation, concurrent
shutdown, reference ownership, fragmented payloads, and GSO.

Performance versus the parent commit, on a 4-core Xeon 6975P-C:

  inject+read 64B / 1280B       245ns / 479ns -> 187ns / 195ns, 0 allocs
  4 injectors, 1 reader         843ns -> 451ns
  64KiB GSO inject+split        10.9us -> 4.4us, 64KiB/op -> 0
  link queue write+read         81.4ns -> 81.5ns
  tsnet TCP, 1 / 8 streams      448 / 580 MB/s -> 492 / 674 MB/s

BenchmarkTCPQueueBottleneck shows that dropping on full queues keeps
throughput at the bottleneck rate (117 MB/s at 100k packets/s, as with
blocking writes), at the cost of more retransmits with many streams.

Fixes #21631
Updates #21638
Updates #21632
Updates #21639

Signed-off-by: James Tucker <[email protected]>
raggi added a commit that referenced this pull request Oct 9, 2026
Make link queue writes nonblocking: gVisor can generate replies on the
reader needed to drain the queue. A full queue drops only the rejected
packet and reports ErrNoBufferSpace, which gVisor counts and TCP handles
by arming its retransmit timer. UDP sockets do not see the error unless
IP_RECVERR is set, matching Linux. Count WireGuard-bound drops as
reason="queue_full" in tailscaled_outbound_dropped_packets_total, and
per-queue drops in debug and client metrics.

Add Wrapper.TryInjectOutbound for best-effort replies generated while
processing packets (TSMP rejects and pongs, conn25 rejects, fake-mode
echo replies), so those paths drop instead of stalling behind the
injection queue's reader. Its drops are also counted as queue_full.

Make outbound injection waits cancellable and close link queues before
abort-generated replies. Stop the WireGuard injection pump on ErrClosed.
Release injected packets still queued at Close, outside closeOnce to
allow reentrant release callbacks; a sender whose send races with Close
releases the packet itself. Release temporary capture buffers.

Copy injected payload views directly into the WireGuard slab, avoiding
an intermediate allocation and copy.

Test queue saturation, multiqueue progress, cancellation, concurrent
shutdown, reference ownership, fragmented payloads, and GSO.

Performance versus the parent commit, on a 4-core Xeon 6975P-C:

  inject+read 64B / 1280B       245ns / 479ns -> 187ns / 195ns, 0 allocs
  4 injectors, 1 reader         843ns -> 451ns
  64KiB GSO inject+split        10.9us -> 4.4us, 64KiB/op -> 0
  link queue write+read         81.4ns -> 81.5ns
  tsnet TCP, 1 / 8 streams      448 / 580 MB/s -> 492 / 674 MB/s

BenchmarkTCPQueueBottleneck shows that dropping on full queues keeps
throughput at the bottleneck rate (117 MB/s at 100k packets/s, as with
blocking writes), at the cost of more retransmits with many streams.

Fixes #21631
Updates #21638
Updates #21632
Updates #21639

Signed-off-by: James Tucker <[email protected]>
raggi added a commit that referenced this pull request Oct 13, 2026
Make link queue writes nonblocking: gVisor can generate replies on the
reader needed to drain the queue. A full queue drops only the rejected
packet and reports ErrNoBufferSpace, which gVisor counts and TCP handles
by arming its retransmit timer. UDP sockets do not see the error unless
IP_RECVERR is set, matching Linux. Count WireGuard-bound drops as
reason="queue_full" in tailscaled_outbound_dropped_packets_total, and
per-queue drops in debug and client metrics.

Add Wrapper.TryInjectOutbound for best-effort replies generated while
processing packets (TSMP rejects and pongs, conn25 rejects, fake-mode
echo replies), so those paths drop instead of stalling behind the
injection queue's reader. Its drops are also counted as queue_full.

Make outbound injection waits cancellable and close link queues before
abort-generated replies. Stop the WireGuard injection pump on ErrClosed.
Release injected packets still queued at Close, outside closeOnce to
allow reentrant release callbacks; a sender whose send races with Close
releases the packet itself. Release temporary capture buffers.

Copy injected payload views directly into the WireGuard slab, avoiding
an intermediate allocation and copy.

Test queue saturation, multiqueue progress, cancellation, concurrent
shutdown, reference ownership, fragmented payloads, and GSO.

Performance versus the parent commit, on a 4-core Xeon 6975P-C:

  inject+read 64B / 1280B       245ns / 479ns -> 187ns / 195ns, 0 allocs
  4 injectors, 1 reader         843ns -> 451ns
  64KiB GSO inject+split        10.9us -> 4.4us, 64KiB/op -> 0
  link queue write+read         81.4ns -> 81.5ns
  tsnet TCP, 1 / 8 streams      448 / 580 MB/s -> 492 / 674 MB/s

BenchmarkTCPQueueBottleneck shows that dropping on full queues keeps
throughput at the bottleneck rate (117 MB/s at 100k packets/s, as with
blocking writes), at the cost of more retransmits with many streams.

Fixes #21631
Updates #21638
Updates #21632
Updates #21639

Signed-off-by: James Tucker <[email protected]>
@sfllaw

sfllaw commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

This PR is now redundant with PR #21668. If not, please open a new pull request referencing that PR and its related bug report. Thanks!

@sfllaw sfllaw closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants