Skip to content

tcp: deliver TIME_WAIT reopen SYNs to the default handler - #4

Open
mafredri wants to merge 10 commits into
new-coder-mainfrom
mafredri/forward-timewait-syn
Open

mafredri wants to merge 10 commits into
new-coder-mainfrom
mafredri/forward-timewait-syn

Conversation

@mafredri

@mafredri mafredri commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Delivers a SYN that reopens a 4-tuple in TIME_WAIT to the stack's default handler when no listening endpoint matches it. Tailscale's netstack, which Coder workspace agents use, accepts TCP through a tcp.Forwarder and has no listening endpoint, so the SYN and every retransmit were dropped until TIME_WAIT expired, and the client's dial hung for up to about 63s (gvisor#15013). This is the reopen RFC 1122 allows, a SYN above the old connection's RcvNxt. A reopening SYN at or below it is a separate bug, tracked in CORE-809.

Upstream's forwarder e2e test and the helpers it imports are on new-coder-main since #8.

The first two commits cherry-pick gvisor#15019 at e503a89e8, an open PR from an outside contributor, with its tests. The other commits are ours, each with the test for its change:

  • When an endpoint closes, a segment still in its queue that matches no other endpoint is now delivered to the default handler, as in nic.DeliverTransportPacket. It gets a RST only if there is no default handler or the handler returns false. Before, a duplicate of the reopening SYN queued behind it got a RST, and the client's dial failed with "connection refused".
  • The TIME_WAIT fallback redelivers the SYN through that path instead of calling the handler and sending the RST itself.
  • The fallback only redelivers a SYN without ACK, as Linux only reopens TIME_WAIT for one. With the two cherry-picked commits alone, a SYN-ACK above RcvNxt ended TIME_WAIT for its 4-tuple and got a RST. Now it is dropped and TIME_WAIT continues.
  • handleTimeWait delivers the reopening SYN before draining the endpoint's queue. Before, a stale SYN queued behind it reached the handler first and got the SYN-ACK, so the client reset it and connected only on its 1s retransmit. The listener path and upstream master have the same order.
  • TransportProtocolDefaultHandler is renamed TransportProtocolHandler, so it pairs with SetTransportProtocolHandler as TransportProtocolOption pairs with SetTransportProtocolOption.

Fixes CORE-717

Testing

Nothing in this repository runs tests, so run them by hand. Every commit builds and passes its tests:

go test -count=1 ./pkg/tcpip/transport/tcp/test/e2e/forwarder_test/

coder/coder#30471 pins this branch's head.

When squash-merging, use this message instead of the default list of commit messages:

tcp: deliver TIME_WAIT reopen SYNs to the default handler

A SYN that reopens a 4-tuple in TIME_WAIT was only delivered to a
listening endpoint, so a stack that accepts connections through a
tcp.Forwarder dropped it until TIME_WAIT expired (google/gvisor#15013).
Deliver such a SYN without ACK to the protocol's default handler when no
listening endpoint matches it.

When an endpoint closes, a segment still in its queue that matches no
other endpoint is also delivered to the default handler, and gets a RST
only if there is no handler or the handler returns false. handleTimeWait
delivers the reopening SYN before it drains the queue.

Based on the unmerged google/gvisor#15019 at e503a89e8, with these
differences: the SYN is delivered through
tryDeliverSegmentFromClosedEndpoint, a SYN-ACK above RcvNxt is dropped
and TIME_WAIT continues where #15019 replies with RST, queued segments
go to the default handler before any RST, the reopening SYN is
delivered before the queue is drained, and the getter is named
TransportProtocolHandler. #15019's tests and the tests for these
differences are added to the forwarder e2e test.

🤖 This PR was created with the help of Coder Agents, and will be reviewed by a human. 🏂🏻

@linear-code

linear-code Bot commented Oct 7, 2026

Copy link
Copy Markdown

PLAT-717

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 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

This comment has been minimized.

@coder-agents-review

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

@f0ssel
f0ssel requested a review from spikecurtis October 27, 2026 15:10
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
coder/gvisor#4 now also hands a SYN queued on a closing TIME_WAIT
connection to the forwarder instead of answering it with RST.
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
@mafredri mafredri changed the title tcp: forward SYN in TIME_WAIT to the protocol handler tcp: hand TIME_WAIT reopen SYNs to the default handler Oct 29, 2026
@mafredri

This comment was marked as outdated.

mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
Picks up comment changes only.
@mafredri mafredri changed the title tcp: hand TIME_WAIT reopen SYNs to the default handler tcp: deliver TIME_WAIT reopen SYNs to the default handler Oct 29, 2026
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
coder/gvisor#4 now reopens TIME_WAIT only for a SYN without ACK, so a
SYN-ACK no longer ends TIME_WAIT with a RST.
@mafredri

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

This comment was marked as outdated.

mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
coder/gvisor#4 now delivers the reopening SYN before the segments
queued behind it, so a stale SYN in the queue no longer gets the
SYN-ACK.
@mafredri

This comment was marked as outdated.

@mafredri
mafredri marked this pull request as ready for review October 29, 2026 20:44
@mafredri
mafredri force-pushed the mafredri/forward-timewait-syn branch from 8fe8ec4 to e31c917 Compare October 29, 2026 21:06
mafredri added a commit to coder/coder that referenced this pull request Oct 29, 2026
coder/gvisor#4 was rebuilt with its tests committed; the code is
unchanged.
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.
mafredri added a commit to coder/coder that referenced this pull request Nov 3, 2026
coder/gvisor#4 now also hands a SYN queued on a closing TIME_WAIT
connection to the forwarder instead of answering it with RST.
mafredri added a commit to coder/coder that referenced this pull request Nov 3, 2026
mafredri added a commit to coder/coder that referenced this pull request Nov 3, 2026
Picks up comment changes only.
mafredri added a commit to coder/coder that referenced this pull request Nov 17, 2026
coder/gvisor#4 now delivers the reopening SYN before the segments
queued behind it, so a stale SYN in the queue no longer gets the
SYN-ACK.
mafredri added a commit to coder/coder that referenced this pull request Nov 17, 2026
coder/gvisor#4 was rebuilt with its tests committed; the code is
unchanged.
drakeo338 and others added 10 commits November 18, 2026 10:56
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)
…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)
tryDeliverSegmentFromClosedEndpoint replied with RST to any queued
segment that matched no other endpoint, while nic.DeliverTransportPacket
first offers such a segment to the protocol's default handler. On a
stack that accepts through a tcp.Forwarder, a SYN retransmit queued on a
TIME_WAIT endpoint behind the SYN that reopens it was drained with a RST
before the reopening SYN reached the forwarder, so the peer saw RST then
SYN-ACK and its connect failed with connection refused.
The default-handler fallback in handleTimeWaitSegments carried its own
handler call and RST. tryDeliverSegmentFromClosedEndpoint now offers a
segment that matches no endpoint to the default handler and replies with
RST if it declines, so route the reopening SYN through it. The reopening
SYN and the segments drained from the TIME_WAIT endpoint before it then
follow one delivery rule.
Call it TransportProtocolHandler so the getter pairs with
SetTransportProtocolHandler the way TransportProtocolOption pairs with
SetTransportProtocolOption.
A SYN-ACK above RcvNxt counts as a new SYN in TIME_WAIT, but it is not a
reopen. On a stack with no listener, redelivering it ended TIME_WAIT for
that 4-tuple and drew a RST. Drop it instead, as before the
default-handler fallback, so TIME_WAIT continues.
TestForwarderTimeWaitRejectedGetsReset becomes
TestForwarderTimeWaitSynAckKeepsTimeWait, and
TestForwarderTimeWaitDeclinedSYNGetsReset covers the RST a declined SYN
still gets.
A stale SYN queued behind the reopening SYN was drained first, reached
the listener or default handler, and got the SYN-ACK, so the client
reset it and connected only on its SYN retransmit. The listener path and
google/gvisor master use the same order.
@mafredri
mafredri force-pushed the mafredri/forward-timewait-syn branch from e31c917 to 327336a Compare November 18, 2026 10:57
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.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
coder/gvisor#4 now also hands a SYN queued on a closing TIME_WAIT
connection to the forwarder instead of answering it with RST.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
Picks up comment changes only.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
coder/gvisor#4 now reopens TIME_WAIT only for a SYN without ACK, so a
SYN-ACK no longer ends TIME_WAIT with a RST.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
coder/gvisor#4 now delivers the reopening SYN before the segments
queued behind it, so a stale SYN in the queue no longer gets the
SYN-ACK.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
coder/gvisor#4 was rebuilt with its tests committed; the code is
unchanged.
mafredri added a commit to coder/coder that referenced this pull request Nov 18, 2026
coder/gvisor#4 was rebased onto new-coder-main with an identical tree,
and the previous pinned commit is no longer on any coder/gvisor branch.
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.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
coder/gvisor#4 now also hands a SYN queued on a closing TIME_WAIT
connection to the forwarder instead of answering it with RST.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
Picks up comment changes only.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
coder/gvisor#4 now reopens TIME_WAIT only for a SYN without ACK, so a
SYN-ACK no longer ends TIME_WAIT with a RST.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
coder/gvisor#4 now delivers the reopening SYN before the segments
queued behind it, so a stale SYN in the queue no longer gets the
SYN-ACK.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
coder/gvisor#4 was rebuilt with its tests committed; the code is
unchanged.
mafredri added a commit to coder/coder that referenced this pull request Nov 25, 2026
coder/gvisor#4 was rebased onto new-coder-main with an identical tree,
and the previous pinned commit is no longer on any coder/gvisor branch.
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.

4 participants