Skip to content

fix: forward TIME_WAIT reopen SYNs to the agent netstack's forwarder - #30471

Open
mafredri wants to merge 21 commits into
mainfrom
mafredri/plat-717/forward-timewait-syn
Open

mafredri wants to merge 21 commits into
mainfrom
mafredri/plat-717/forward-timewait-syn

Conversation

@mafredri

@mafredri mafredri commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

The workspace agent's tailnet netstack accepts TCP through tcp.Forwarder and has no gVisor listening endpoint. gVisor only delivers a SYN that reopens a 4-tuple in TIME_WAIT to a listening endpoint, so it dropped the SYN and every retransmit until TIME_WAIT expired, and the client's dial hung for up to about 63s (gvisor/gvisor#15013). The agent holds TIME_WAIT whenever it closes first, which is every agent API request, and any forwarded port whose server closes first.

This bumps the gVisor fork to coder/gvisor@327336af3995, the head of coder/gvisor#4. That branch is based on gvisor/gvisor#15019, an open PR from an outside contributor, plus our fixes for a duplicate or stale SYN queued behind the reopening SYN and for a SYN-ACK that ended TIME_WAIT. It also re-enables the two tests that #30465 skips, TestOpenVSCodeDevContainer, which #32058 skips, and TestACPTransport, which #32752 skips.

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.

The fix only takes effect on upgraded agents.

Merge order: this PR pins 327336af3995, the unmerged head of coder/gvisor#4. coder/gvisor only allows squash merges, so re-bump go.mod to the squash commit of coder/gvisor#4 before merging this PR, as main does for coder/gvisor#3 (7a658db7b714).

Fixes CORE-717, CODAGT-1331, CODAGT-1336, CODAGT-1495

Evidence and decisions

Reproduction

  • A tailnet test with two tailnet.Conns, where the client reuses its source port after the agent closed the connection, fails 3 of 3 on the old pin, with the reused-port dial timing out, and passes 50 of 50 with the bump. A control using a fresh source port passes on the old pin.
  • 3000 sequential ListProcesses calls through AgentConn against agenttest: 29 stalls of about 63s on the old pin, 0 with the bump. Every stall reused a source port the agent held in TIME_WAIT.

Production (dogfood, one real agent)

Test scenarios against an agent without the fix. The fix has only run in the test suite and CI, not on dogfood.

  • coderd listening-ports, which the dashboard polls every 5s: 5 of 2617 requests failed with a 500 after the handler's 30s timeout.
  • The app proxy, with a backend that closes every connection: 1 of 3000 requests hung 63.6s.
  • coder port-forward to a server that closes first: 0.7% to 1.0% of connections stalled for 7, 15, 31 or 63s, the SYN retransmit times. With the client closing first there were 0 stalls in 6000.
  • coder ssh is not exposed: one connection per process, from a new tailnet IP.
  • Details are on CORE-717.

Alternatives considered

Not included: tests of the gVisor behavior. They are in coder/gvisor#4, which lists their results against the pinned branch.

🤖 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

@mafredri

This comment has been minimized.

@coder-agents-review

coder-agents-review Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-10-26 15:13 UTC by @mafredri

Review history
  • R1 (2026-10-07): 12 reviewers, 1 Nit, 3 P3, 5 P4, COMMENT. Review
  • R2 (2026-10-26): 9 reviewers, 1 Nit, 3 P3, 5 P4, COMMENT. Review

deep-review v0.13.0 | Round 2 | 95e6a73..0b03c70

Last posted: Round 2, 9 findings (3 P3, 5 P4, 1 Nit), COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 Note Note (body) go.mod:50 Pin is the unmerged coder/gvisor#4 head; the re-bump after squash merge is not enforced R1 Netero No
CRF-2 P3 Author fixed (b82937b, ab6a320) tailnet/conn_test.go:617 RegisteredEndpoints() empties before ReleasePort, so the next bind can fail with port in use R1 Komugi P3, Takumi P4 Yes
CRF-3 P3 Open (author narrowed title and description; panel re-raised R2: go.mod:48 comment still unconditional) go.mod:50 Reopening SYN with seq not above old RcvNxt is still dropped; PR claims a full fix R1 Pariston Yes
CRF-4 P3 Author fixed (b82937b) tailnet/conn_test.go:604 NIC ID 1 literal shared between test and newTUNStack R1 Gon Yes
CRF-5 P4 Author contested; panel closed R2 (1/1 accept) tailnet/conn_test.go:623 newTUNStack doc comment restates the signature R1 Gon P2 Yes
CRF-6 P4 Author contested; panel closed R2 (1/1 accept) tailnet/conn_test.go:604 Reused client source port 40000 is an unnamed literal R1 Gon Yes
CRF-7 P4 Author contested; panel closed R2 (1/1 accept) tailnet/conn_test.go:630 channel.New queue size 64 is unexplained R1 Gon Yes
CRF-8 P4 Author fixed (b82937b) tailnet/conn_test.go:610 Dial failure message says "reused" on the first dial R1 Leorio P4, Gon Nit Yes
CRF-9 P4 Author fixed (b82937b, ab6a320) tailnet/conn_test.go:552 Test doc comment cites only private PLAT-717 and omits the cause R1 Leorio P4, Gon Nit Yes
CRF-10 Nit Author fixed (b82937b) tailnet/conn_test.go:555 nolint:paralleltest reason names the wrong effect R1 Zoro Yes
CRF-11 OOS Out of scope (body) gvisor rcv.go:586 (fork) TIME_WAIT reopen accepts by sequence only, no RFC 6191 timestamp path, RST ignored R1 Pariston No
CRF-12 Note Note (body) go.mod:50 Reopen path goes to the same forwarder and filters as a normal SYN; no new exposure R1 Kurapika No
CRF-13 OOS Out of scope (body) tailnet/conn.go:172 NewConn sets process-global netns from TUNDev, affecting every other Conn and persisting after the Conn closes R2 Meruem No
CRF-14 Note Note (body) tailnet/conn_test.go:607 Test relies on unasserted agent-closes-first and ISN-above-RcvNxt preconditions; old-pin failure shows they hold today R2 Meruem No

Contested and acknowledged

CRF-5 (P4, tailnet/conn_test.go:623) - newTUNStack doc comment restates the signature

  • Finding: The newTUNStack doc comment repeats parameter names and return type and omits that callers must set FullAddress.NIC to the helper's fixed NIC ID; proposed rewriting it to name the NIC ID constant.
  • Author defense: After the CRF-4 fix dropped NIC from the bind address (0 binds to the NIC that owns the address), callers no longer depend on the NIC ID, so there is no contract left for the comment to state.
  • Panel closure (R2, 1/1): Gon, the agent that raised it (resumed via message_agent), verified local no longer sets NIC, so callers do not need the NIC ID; the remaining comment restates the signature of a single-caller test helper, at no cost to readers.

CRF-6 (P4, tailnet/conn_test.go:604) - Reused client source port 40000 is an unnamed literal

  • Finding: The reused client source port 40000 is a literal inside a struct field; proposed const clientPort = 40000 next to const port = 35565.
  • Author defense: Port 40000 is used once, as local.Port, and the failure message prints local.Port, so a constant would name a single use.
  • Panel closure (R2, 1/1): Gon, the agent that raised it, verified 40000 appears once as local.Port and the failure message prints local.Port; the field name already says what the value is.

CRF-7 (P4, tailnet/conn_test.go:630) - channel.New queue size 64 is unexplained

  • Finding: channel.New(64, ...) gives no indication whether the queue size matters; proposed a comment or named constant.
  • Author defense: One connection is in flight at a time, so the size does not matter, and a comment saying so would not change how anyone edits the test.
  • Panel closure (R2, 1/1): Gon, the agent that raised it, verified the loop dials one connection at a time, so any small queue size works; the check belongs in whatever edit makes the test concurrent.

Round log

Round 1

Full panel (round 1). Netero: no findings, 1 Note. Law not run (135 effective additions). Panel: ging-go, ryosuke, takumi, killua, kurapika (security-sensitive network input path, multi-domain size target), pariston, mafuuu, bisky, komugi, gon, leorio, wildcard zoro. 3 P3, 5 P4, 1 Nit, 2 Notes, 1 out of scope. CRF-5 downgraded P2 to P4: keep-argument was that the comment hides the NIC ID contract; downgraded because the comment is accurate, the helper has one caller in the same file, and CRF-4's constant carries the missing fact. CRF-2 set to P3 (Komugi) over P4 (Takumi): both reproduced; Komugi line used (Takumi's 613 points at require.Equal). Reviewed against 4e827d7..9636abc.

Round 2

Fix round (no files added or renamed, no exported signature change, effective additions +10; branch rebased onto 95e6a73, gVisor pin unchanged). Churn guard PROCEED: CRF-2, 4, 8, 9, 10 addressed; CRF-3 addressed in title and description; CRF-5, 6, 7 contested. Netero: no new findings. Panel: gon (resumed R1 agent for dispute check-in), komugi, takumi (CRF-2 raisers), pariston (CRF-3), leorio (CRF-8, 9), zoro (CRF-10), ging-go, bisky (Go test changes), wildcard meruem. CRF-5, 6, 7 closed 1/1. CRF-3 re-raised by Pariston and Leorio (both P4): the go.mod comment added in 70a41a1 still describes the forward unconditionally; reply on the existing thread, no new ID. CRF-2 fix verified by Komugi, Takumi, Netero, Bisky against stack.go:1688-1694 and endpoint.go:1224,1242. New: CRF-13 OOS, CRF-14 Note. Reviewed against 95e6a73..0b03c70.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T15:16:32.795153Z 0b03c70 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@mafredri
mafredri added this pull request to stack #30474 October 7, 2026 14:19
@chatgpt-codex-connector

This comment has been minimized.

ibetitsmike
ibetitsmike previously approved these changes Oct 7, 2026

@coder-agents-review coder-agents-review Bot 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.

This PR bumps the gVisor fork so the agent netstack hands a SYN that reopens a TIME_WAIT 4-tuple to tcp.Forwarder, adds a tailnet regression test for it, and re-enables the two tests that #30465 skipped. 3 P3, 5 P4, 1 Nit.

Out of scope (needs a ticket or explicit acceptance by a human):

  • coder/gvisor pkg/tcpip/transport/tcp/rcv.go:586: in TIME_WAIT, gVisor accepts a reopening SYN only when its sequence number is above the old connection's RcvNxt, has no RFC 6191 timestamp path, and drops RSTs (rcv.go:566), so a SYN that fails the sequence test still stalls the dial for the full TIME_WAIT with no way for the client to recover.

Notes:

  • go.mod:50: ebe33a4c2cb5 exists only on the coder/gvisor#4 branch (and refs/pull/4/head), and nothing in CI enforces the re-bump to the squash commit that the description requires before merge.
  • go.mod:50: the reopen path calls the same wrapProtoHandler(tcpFwd.HandlePacket) as a normal SYN, after the packet filter and the WireGuard AllowedIPs check, so a peer can only reopen 4-tuples with its own source IP.

🤖 This review was automatically generated with Coder Agents.

Comment thread tailnet/conn_test.go Resolved
Comment thread go.mod Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
Comment thread tailnet/conn_test.go Resolved
@mafredri mafredri changed the title fix: accept SYNs that reopen a TIME_WAIT connection on the agent netstack fix: forward TIME_WAIT reopen SYNs to the agent netstack's forwarder Oct 7, 2026
@mafredri

mafredri commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

In reply to CRF-1 (Note, go.mod:50): #30471 (review)

No CI check: this PR stays in draft until coder/gvisor#4 squash-merges and go.mod points at that commit, as the merge-order note in the description says.


In reply to CRF-11 (OOS, go.mod:50): #30471 (review)

Same cause as CRF-3, tracked on PLAT-717.

🤖 Posted using /amend-review skill via Coder Agents.

@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from 8d09eaf to bcb7405 Compare October 8, 2026 06:50
Base automatically changed from mafredri/skip-agent-timewait-flakes to main October 8, 2026 07:42
@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from bcb7405 to b094a26 Compare October 8, 2026 07:42
@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from b094a26 to 0892aef Compare October 8, 2026 07:49
@mafredri
mafredri marked this pull request as ready for review October 9, 2026 13:03
@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from 8f2145f to 0b03c70 Compare October 21, 2026 08:09
@mafredri

This comment has been minimized.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri

This comment has been minimized.

@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from dfc393d to 0933acc Compare November 3, 2026 11:31
@github-actions github-actions Bot added the stale This issue is like stale bread. label Nov 11, 2026
@github-actions github-actions Bot removed the stale This issue is like stale bread. label Nov 16, 2026

@spikecurtis spikecurtis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These tests are not appropriate for coder/coder.

The issue is with gvisor and any tests for it should live there.

Copy link
Copy Markdown
Member Author

Fair, these tests were added before I pulled in the gvisor test suite needed to test it there. I'll drop them from this PR, whether or not it gets merged.

@mafredri
mafredri temporarily deployed to main November 17, 2026 12:35 — with GitHub Actions Inactive
@mafredri
mafredri temporarily deployed to main November 17, 2026 12:36 — with GitHub Actions Inactive
@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from 1bbde36 to 223369e Compare November 18, 2026 11:03
mafredri added a commit that referenced this pull request Nov 25, 2026
… TIME_WAIT fix (#32752)

Temporarily skip `TestACPTransport`
([CODAGT-1495](https://linear.app/codercom/issue/CODAGT-1495)) until
[CORE-717](https://linear.app/codercom/issue/CORE-717) is fixed, as
#30465 and #32058 do for the other tests it hits. Remove this skip with
#30471.

A scratch test looping this test's calls through the real `AgentConn`
shows the cause: on main's gVisor pin, every dial that reused a source
port the agent held in TIME_WAIT stalled (8 of about 1,316 dials). With
#30471's pin, all 24 such reuses in 2,100 dials connected and nothing
stalled.

> 🤖 Created with the help of Coder Agents.
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.
They were skipped because the agent netstack dropped SYNs that reused
a source port in TIME_WAIT. The coder/gvisor bump makes the agent
netstack accept those SYNs.
The test's WaitLong deadline started before RunDERPAndSTUN and both
NewConn calls, so setup time ate into it. Create it right before
AwaitReachable instead.

newTUNStack's packet pumps used that deadline context as their
lifetime. Tie them to t.Context so they run until the test ends.
gVisor removes a closing endpoint from RegisteredEndpoints before it
calls ReleasePort, so the second dial could race the port release and
fail to bind. The endpoint stays in CleanupEndpoints until after the
release, so wait for both sets to drain.

Also bind to any NIC, drop "reused" from the dial failure message, and
reword the doc comment and nolint reason to cite the gVisor issue the
test covers.
The doc comment now names the RFC 1122 reopen condition gVisor checks,
and the wait comment matches the order in which gVisor adds the endpoint
to CleanupEndpoints, unregisters it, and releases the port.
Run the accept loop and the TUN pumps with testutil.Go so the test
waits for them to exit instead of letting them outlive it. The pumps
are registered after the stack cleanup, so they finish before the link
endpoint and stack close.
coder/gvisor#4 now also hands a SYN queued on a closing TIME_WAIT
connection to the forwarder instead of answering it with RST.
NewConn with a TUN device enables netns and soft isolation for the
whole process, and closing the Conn does not undo that, so later tests
in the package ran with both on. Reset them to their defaults, which
are off, when the test returns.
Picks up comment changes only.
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.
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.
These behaviors exist only in the coder/gvisor fork, so this test fails
a pin bump that drops them.
coder/gvisor#4 was rebuilt with its tests committed; the code is
unchanged.
These tests exercise gVisor's TCP behavior rather than coder/coder code,
so they belong in coder/gvisor next to the change they cover.
It was skipped because the agent netstack dropped SYNs that reused a
source port in TIME_WAIT. The coder/gvisor pin makes the agent netstack
accept those SYNs.
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.
It was skipped because the agent netstack dropped SYNs that reused a
source port in TIME_WAIT. The coder/gvisor pin makes the agent netstack
accept those SYNs.
@mafredri
mafredri force-pushed the mafredri/plat-717/forward-timewait-syn branch from 223369e to c295a6c Compare November 25, 2026 13:14

This branch was previously deployed

1 inactive (outdated) deployment
main — 223369ea Deployed Nov 18, 2026 by mafredri via cla #259312
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.

3 participants