Skip to content

tcp: forward SYN in TIME_WAIT to the protocol handler - #15019

Open
drakeo338 wants to merge 3 commits into
gvisor:masterfrom
drakeo338:claude/15013-timewait-forwarder-syn
Open

drakeo338 wants to merge 3 commits into
gvisor:masterfrom
drakeo338:claude/15013-timewait-forwarder-syn

Conversation

@drakeo338

@drakeo338 drakeo338 commented Sep 25, 2026 •

Copy link
Copy Markdown

With a tcp.Forwarder as the TCP protocol handler, a new SYN that reopens a 4-tuple still in TIME_WAIT was dropped, because handleTimeWaitSegments only hands it to a bound listening endpoint and a forwarder stack has none. Retransmissions hit the same endpoint, so the peer waited out the whole TIME_WAIT period.

This falls back to the stack's default handler for the protocol, through a small Stack.TransportProtocolDefaultHandler getter, when no listener claims the SYN. The TIME_WAIT endpoint is already closed and unlocked by the time the callback runs, same as the listener path.

I added TestForwarderReopenAfterTimeWait. It fails on master with "Packet wasn't written out" and passes with the change; bazel test //pkg/tcpip/transport/tcp/... and //pkg/tcpip/stack/... pass.

Only a SYN without ACK counts as a reopening SYN, as in Linux's tcp_timewait_state_process, so a SYN-ACK or a repeated FIN arriving in TIME_WAIT is still ACKed by the old endpoint with its numbers instead of reaching the forwarder and getting a RST. TestForwarderTimeWaitSynAckKeepsEndpoint covers that; TestForwarderTimeWaitDeclinedSynGetsReset covers the RST reply when the default handler declines a SYN, matching the normal delivery path.

AI disclosure: I wrote this with Claude via Claude Code and reviewed the change and the test results myself.

Fixes #15013

When a new SYN reopens a connection while the old incarnation is in
TIME_WAIT (RFC 1122), handleTimeWaitSegments only looks for a bound
listening endpoint to hand the segment to. A stack that accepts
connections through a tcp.Forwarder has no such endpoint, so the SYN
is dropped for the rest of the TIME_WAIT period. Fall back to the
protocol's default handler, giving the forwarder the same chance a
listening endpoint gets.

Fixes gvisor#15013

Signed-off-by: drakeo338 <[email protected]>
@github-actions
github-actions Bot requested review from carzh and nybidari September 25, 2026 23:47
@everanium

Copy link
Copy Markdown

Thanks for the fix — I verified it against the reproducer from #15013: with this change applied to the go branch
(v0.0.0-20260925204757-f6e2c02e9739), all three tests pass under -race, and our own netstack tests pass with the default 60
s TIME_WAIT.

One small note: the default handler's return value is ignored in the reuseTW path. In the normal delivery path, a default
handler that returns false leads to a RST (unknown destination); here such a SYN would be dropped silently. tcp.Forwarder
always returns true, so it doesn't affect the forwarder case, but it may be worth either honouring the result (e.g.
replying with RST when it returns false) or noting in a comment why it's safe to ignore.

…ment

The reuseTW closure in handleTimeWaitSegments discarded the handler's
return value. tcp.Forwarder.HandlePacket returns false for anything that
is not a bare SYN, but a SYN-ACK with a higher sequence number still
counts as a new SYN in TIME_WAIT, so it reached the handler, was
declined, and was dropped without a reply. Send a RST as
nic.DeliverTransportPacket does when the handler returns false.

Adds a forwarder e2e test that sends a SYN-ACK during TIME_WAIT and
expects a RST.

Signed-off-by: drakeo338 <[email protected]>
@drakeo338

Copy link
Copy Markdown
Author

Thanks, you were right. The reuseTW closure dropped the handler result, and Forwarder.HandlePacket returns false for anything that is not a bare SYN, so a SYN-ACK with a higher sequence number in TIME_WAIT was dropped without a reply. I pushed a follow-up commit that sends a RST in that case, as nic.DeliverTransportPacket does, plus an e2e test for it. The test fails without the change and the //pkg/tcpip/transport/tcp/... tests pass with it.

mafredri added a commit to coder/coder that referenced this pull request Oct 8, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes once the
fork carries gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which would leak into NewConn calls in
parallel tests.
mafredri added a commit to coder/coder that referenced this pull request Oct 8, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor dropped a SYN that reused a 4-tuple
the agent held in TIME_WAIT, and the client's dial hung for about 63s.
The fork now carries gvisor/gvisor#15019, which hands that SYN to the
forwarder.
mafredri added a commit to coder/coder that referenced this pull request Oct 8, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Oct 8, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
mafredri added a commit to coder/coder that referenced this pull request Oct 21, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Oct 21, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
@daaain

daaain commented Oct 23, 2026

Copy link
Copy Markdown

I ran into this issue in Lima and tested the fix together with different workarounds, but nothing really comes close to this so really hope someone from Google can review and merge!

Claude's writeup below:


A downstream data point for this change, measured. gvisor-tap-vsock accepts all TCP through tcp.Forwarder, and Lima runs it in-process for every vmType: vz VM on macOS (the default network), so a guest that makes short connections to a host service that closes first hits #15013 within seconds: the guest reuses the source port, and connect() hangs until gVisor's 60 s TIME_WAIT runs out (7–68 s, by how much is left).

I backported both commits of this PR onto the gVisor that Lima 2.1.3 vendors (a174eb65023f, through gvisor-tap-vsock v0.8.9). The PR's non-test diff (the stack.go getter and the connect.go hunk, 36 lines) applies there with patch -p1 at line offsets only. Then I built Lima v2.1.3 with and without it (replace gvisor.dev/gvisor) and ran both on the same VM (Fedora 44 guest, kernel 6.19, default sysctls; macOS host). The reproducer from #15013 fails on stock a174eb65023f and passes with the backport under -race.

guest→host, host closes first without with this PR
8 workers × 500 connections, three runs 8, 10, 8 stalls of ~68 s 0 connects over 1 s, max 7 ms
4 workers × 5,000, unthrottled 48.6 conn/s, 24 stalls of ~68 s, 412 s 8,032 conn/s, 0 over 1 s, max 10 ms, 2.5 s
1 worker at 10 conn/s, 5 min 12 stalls (three of ~68 s) 0 over 1 s, max 15 ms
SYNs that reopened a 4-tuple still in TIME_WAIT 62, every one stalled 9,506, every one answered

TCP.ForwardMaxInFlightDrop stayed 0 in every run. With the patch, CurrentConnected still reached 22,784 (TIME_WAIT endpoints), so TIME_WAIT is kept and only the reopen changes. The client saw no errors, and ResetsSent didn't move.

For contrast, the workaround from the issue (a 1 s TCPTimeWaitTimeoutOption, set in gvisor-tap-vsock's createStack) on the same workloads: 8 stalls per 8 × 500 run and 24 in the 4 × 5,000 run, each ~1.04 s, which is the guest's first SYN retransmission. It shortens the stall but doesn't remove it. This PR removes it.

Earlier, unpatched, on the same setup: one worker at only 2 conn/s stalled ~68 s four times in 15 minutes, and aborting from the guest (SO_LINGER {1,0}, so gVisor never enters TIME_WAIT) gave 0 stalls in 20,000 connections. Full record: https://github.com/twistco/foldyard/blob/07a824b680482e727f3c41e3b4212dcd6c0ec1b1/docs/lima-usernet-freeze.md

AI disclosure: Claude (Claude Code) did the backport, the builds, the runs and this write-up.

mafredri pushed a commit to coder/gvisor that referenced this pull request Oct 29, 2026
When a new SYN reopens a connection while the old incarnation is in
TIME_WAIT (RFC 1122), handleTimeWaitSegments only looks for a bound
listening endpoint to hand the segment to. A stack that accepts
connections through a tcp.Forwarder has no such endpoint, so the SYN
is dropped for the rest of the TIME_WAIT period. Fall back to the
protocol's default handler, giving the forwarder the same chance a
listening endpoint gets.

Fixes gvisor#15013

Signed-off-by: drakeo338 <[email protected]>
Cherry-picked from gvisor#15019 (unmerged)
mafredri pushed a commit to coder/gvisor that referenced this pull request Oct 29, 2026
…ment

The reuseTW closure in handleTimeWaitSegments discarded the handler's
return value. tcp.Forwarder.HandlePacket returns false for anything that
is not a bare SYN, but a SYN-ACK with a higher sequence number still
counts as a new SYN in TIME_WAIT, so it reached the handler, was
declined, and was dropped without a reply. Send a RST as
nic.DeliverTransportPacket does when the handler returns false.

Adds a forwarder e2e test that sends a SYN-ACK during TIME_WAIT and
expects a RST.

Signed-off-by: drakeo338 <[email protected]>
Cherry-picked from gvisor#15019 (unmerged)

@tamird tamird left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep the TIME_WAIT endpoint for this SYN-ACK? With these packet fields, Linux ACKs the SYN-ACK with the old connection’s sequence numbers. Repeating that connection’s last FIN gets the same ACK. On this head both packets get RSTs: the forwarder rejects the SYN-ACK, but by then the old endpoint is already gone.

Restricting TIME_WAIT reuse to a SYN without ACK or RST keeps both ACKs and still accepts a genuine reopening SYN. Here are the packet traces, the tested guard, and the corresponding Linux check.

Review by Codex on behalf of @tamird.

A SYN that also carries an ACK is not a reopening SYN: it belongs to the
old incarnation, a retransmitted SYN-ACK for instance. With the forwarder
fallback, handing it to the default handler tore down the TIME_WAIT
endpoint and answered it with a RST, and a FIN the peer repeated after
that got a RST too, whereas Linux ACKs both with the old connection's
numbers.

Match Linux (tcp_timewait_state_process reopens only for a SYN without
ACK or RST) by excluding a SYN with ACK from the reopening check, so the
TIME_WAIT endpoint keeps answering segments of the old incarnation and a
bare SYN still reaches a listener or the default handler.

The SYN-ACK test now expects the ACK with the old numbers, then a
repeated FIN acknowledged the same way and a bare SYN reopening through
the forwarder. A new test with a default handler that declines keeps the
RST reply for a declined SYN covered. Both share one TIME_WAIT setup
with the reopen test.

Suggested by Tamir Duberstein in review, with packet traces showing the
Linux behaviour.

Signed-off-by: drakeo338 <[email protected]>
@drakeo338

Copy link
Copy Markdown
Author

Pushed ed8a800 with your guard: a SYN carrying an ACK no longer reopens in TIME_WAIT, and the test now expects the SYN-ACK and a repeated FIN to be ACKed with the old numbers before a bare SYN reaches the forwarder.

@carzh

carzh commented Nov 3, 2026

Copy link
Copy Markdown
Member

Hello, could you also squash-merge your commits into one commit and rebase onto master? (can also do this step later! but you'll need another LGTM after that happens.)

}
// We explicitly do not DecRef the segment as it's still valid and
// being reflected to the default handler.
return false, reuseTW

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose a client queues two copies of a reopening SYN on the TIME_WAIT endpoint before the processor runs. When handleTimeWait receives reuseTW from here, it runs drainClosingSegmentQueue on the second SYN before calling reuseTW on the first SYN. Because tryDeliverSegmentFromClosedEndpoint only checks the demuxer and finds no listener, it replies to the second SYN with a RST before the forwarder sends a SYN-ACK, causing the client's connect() to fail with ECONNREFUSED.

In tryDeliverSegmentFromClosedEndpoint, when FindTransportEndpoint returns nil, could we call the stack's default TCP handler (via TransportProtocolDefaultHandler) and only send the RST if no handler is set or the handler returns false? Then the forwarder receives the drained SYN too and drops the duplicate in its in-flight table.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried delivering the first SYN before draining the queue. The client still got ECONNREFUSED: the forwarder records the request before its goroutine registers an endpoint.

This change tries the registered endpoint first and, if there is none, offers the queued packet to the handler that accepted the opening SYN. Both the client and the forwarder completed the handshake. The single-SYN and declined-SYN cases still passed. I’d limit the fallback to this unlocked path because outgoing port reuse drains the same queue while holding two endpoint locks. Packet traces.

Posted by Codex on behalf of @tamird.

mafredri added a commit to coder/coder that referenced this pull request Nov 3, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Nov 3, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
@adios-404

Copy link
Copy Markdown
Contributor

A Tailscale developer reproduced the stall on Tailscale's gVisor pin, found that this change fixes it, and suggested bumping the pin once it merges (tailscale/tailscale#22083).

Two things are left from carzh's 3 Nov comments: the squash and rebase, and the duplicate-SYN case, which tamird's 936ad90db fixes.

I tried both on master f98c31b. The squash applies cleanly. 936ad90 conflicts only with #15094's segment-leak fix in the listener path, and keeping both works. A new test that queues two copies of the reopening SYN gets a RST first without 936ad90 and reconnects with it. bazel test //pkg/tcpip/transport/tcp/... //pkg/tcpip/stack/... passes (37 of 37).

@drakeo338, will you update the PR, or are you happy for me to open a squashed, rebased copy with you kept as the author and tamird credited?

mafredri added a commit to coder/coder that referenced this pull request Nov 17, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Nov 17, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
mafredri pushed a commit to coder/gvisor that referenced this pull request Nov 18, 2026
When a new SYN reopens a connection while the old incarnation is in
TIME_WAIT (RFC 1122), handleTimeWaitSegments only looks for a bound
listening endpoint to hand the segment to. A stack that accepts
connections through a tcp.Forwarder has no such endpoint, so the SYN
is dropped for the rest of the TIME_WAIT period. Fall back to the
protocol's default handler, giving the forwarder the same chance a
listening endpoint gets.

Fixes gvisor#15013

Signed-off-by: drakeo338 <[email protected]>
Cherry-picked from gvisor#15019 (unmerged)
mafredri pushed a commit to coder/gvisor that referenced this pull request Nov 18, 2026
…ment

The reuseTW closure in handleTimeWaitSegments discarded the handler's
return value. tcp.Forwarder.HandlePacket returns false for anything that
is not a bare SYN, but a SYN-ACK with a higher sequence number still
counts as a new SYN in TIME_WAIT, so it reached the handler, was
declined, and was dropped without a reply. Send a RST as
nic.DeliverTransportPacket does when the handler returns false.

Adds a forwarder e2e test that sends a SYN-ACK during TIME_WAIT and
expects a RST.

Signed-off-by: drakeo338 <[email protected]>
Cherry-picked from gvisor#15019 (unmerged)
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
@adios-404

Copy link
Copy Markdown
Contributor

No reply in a week, so I opened the squashed and rebased copy as #17844, with you kept as the commit author. Happy to close it if you'd rather finish this one.

Ramblurr added a commit to Ramblurr/nixcfg that referenced this pull request Nov 23, 2026
… 1 s TIME_WAIT

Carry the non-test diff of gvisor/gvisor#15019 (head ed8a800c) unchanged
as patches/gvisor/forward-timewait-syn.patch, applied to
vendor/gvisor.dev/gvisor by package.nix and scripts/vendor.sh. A bare SYN
that no listener claims in TIME_WAIT now goes to the stack's default
handler, netstack's forwarder, so the tenant reconnects on its first SYN
instead of after a retransmit. Drop the patch once tailscale pins a
gVisor with the change.

The exit node no longer sets a 1 s TIME_WAIT and keeps gVisor's 60 s, so
a last FIN retransmitted later than 1 s is acked again.
TestExitNodeReopensATimeWaitFlow passes with one SYN, and the stale-SYN,
unused-destination and SYN-flood tests stay green.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
The agent netstack accepts TCP through tcp.Forwarder and has no
listening endpoint. gVisor only hands a SYN that reuses a 4-tuple in
TIME_WAIT to a listening endpoint, so the agent drops it and every
retransmit until TIME_WAIT expires, about 60s later (PLAT-717).

The test dials the agent twice from the same source port, after the
agent closed the first connection. The client is a gVisor stack behind
a channel TUN, because tailnet.Conn's own dialer cannot choose a source
port. It fails against the current coder/gvisor pin and passes with a
cherry-pick of gvisor/gvisor#15019.

The test does not call t.Parallel: setting TUNDev enables tailscale's
process-global netns setting, which sockets opened by Conns in parallel
tests would pick up.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
The agent netstack accepts connections through tcp.Forwarder and has
no listening endpoint, so gVisor drops a SYN that reuses a 4-tuple the
agent holds in TIME_WAIT, and the client's dial hangs for about 63s.

The new pin is the head of coder/gvisor#4, which cherry-picks
gvisor/gvisor#15019. That PR hands the SYN to the forwarder. It is
open, from an outside contributor, and no gVisor maintainer has
reviewed it.
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.

SYN in TIME_WAIT is dropped when the stack accepts through tcp.Forwarder

6 participants