Repository navigation
Conversation
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]>
|
Thanks for the fix — I verified it against the reproducer from #15013: with this change applied to the One small note: the default handler's return value is ignored in the reuseTW path. In the normal delivery path, a default |
…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]>
|
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. |
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.
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.
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.
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.
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.
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.
|
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 I backported both commits of this PR onto the gVisor that Lima 2.1.3 vendors (
For contrast, the workaround from the issue (a 1 s 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 ( AI disclosure: Claude (Claude Code) did the backport, the builds, the runs and this write-up. |
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)
tamird
left a comment
There was a problem hiding this comment.
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]>
|
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. |
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
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.
|
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. @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? |
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.
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.
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)
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.
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.
|
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. |
… 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.
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.
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.
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