Skip to content

wgengine/netstack: close linkEP before aborting endpoints in Close - #21632

Closed
dcandrade wants to merge 1 commit into
tailscale:mainfrom
dcandrade:netstack-close-full-queue
Closed

dcandrade wants to merge 1 commit into
tailscale:mainfrom
dcandrade:netstack-close-full-queue

Conversation

@dcandrade

@dcandrade dcandrade commented Oct 2, 2026 •

Copy link
Copy Markdown

Impl.Close cancels ns.ctx, which stops the goroutines that drain linkEP's outbound queues. It then calls ipstack.Close, which aborts every endpoint, and aborting a connected TCP endpoint writes a RST into an outbound queue. The queues were only closed later, from ipstack.Wait. So once a queue was full, queue.Write blocked forever while holding the endpoint's lock, and Close never returned. Details, with a production goroutine dump, are in #21631.

This closes linkEP right after canceling the context. Writes during the abort then fail with ErrClosedForSend instead of blocking. linkEndpoint.Close is idempotent, so the later close from ipstack.Wait does nothing.

TestCloseWithFullOutboundQueue establishes a self-addressed TCP connection, stops the queue readers, fills the outbound queue, then calls Close. Without the fix it fails after 10s (Close did not return with a full outbound queue). With the fix it passes in under 0.5s, including 100 runs under -race.

Tested: go test -race ./wgengine/netstack/... and go test ./tsnet/.

Fixes #21631

Impl.Close cancels ns.ctx, which stops the goroutines reading linkEP's
outbound queues, and then calls ipstack.Close, which aborts every
endpoint. Aborting a connected TCP endpoint writes a RST into an
outbound queue. The queues were only closed later, from ipstack.Wait,
so in between nothing drained them: once a queue was full, queue.Write
blocked forever while holding the endpoint's lock, and Close never
returned.

Close linkEP right after canceling the context, so writes during the
abort fail with ErrClosedForSend instead of blocking. linkEndpoint.Close
is idempotent, so the later close from ipstack.Wait is a no-op.

Fixes tailscale#21631

Change-Id: I908307db6c76cff938a4dfacd98dfe8e4305f939
Signed-off-by: Daniel Coelho de Andrade <[email protected]>
@dcandrade
dcandrade force-pushed the netstack-close-full-queue branch from 2987e90 to 0201fe6 Compare October 2, 2026 13:19
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]>
@dcandrade

Copy link
Copy Markdown
Author

Superseded by 3c0508f ("net/tstun,wgengine/netstack: avoid deadlocks on full outbound queues"), which fixes #21631 and already closes linkEP right after cancelling the context in Impl.Close. Closing this one. Thanks!

@dcandrade dcandrade closed this Nov 12, 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.

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

1 participant