Repository navigation
Conversation
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
force-pushed
the
netstack-close-full-queue
branch
from
October 2, 2026 13:19
2987e90 to
0201fe6
Compare
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]>
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Impl.Closecancelsns.ctx, which stops the goroutines that drain linkEP's outbound queues. It then callsipstack.Close, which aborts every endpoint, and aborting a connected TCP endpoint writes a RST into an outbound queue. The queues were only closed later, fromipstack.Wait. So once a queue was full,queue.Writeblocked forever while holding the endpoint's lock, andClosenever 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
ErrClosedForSendinstead of blocking.linkEndpoint.Closeis idempotent, so the later close fromipstack.Waitdoes nothing.TestCloseWithFullOutboundQueueestablishes a self-addressed TCP connection, stops the queue readers, fills the outbound queue, then callsClose. 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/...andgo test ./tsnet/.Fixes #21631