Repository navigation
fix(egress): free a conn's slot before its tunnel event and its close - #834
Conversation
The per-VM connection slot (MaxConnsPerVM, the conn_cap) was freed in a deferred step of the conn's handler goroutine, after the conn's terminal tunnel event was emitted and after the guest conn was closed. A guest that reconnected as soon as it saw either could race that step and meet conn_cap, which is what connect_tunnel_idle_deadline hit intermittently in CI with a cap of one. The slot is now released by boundConn.Close, before the socket is closed, so every wrapper and exit path (normal end, refusal, Unregister's abort, Close) frees it before the guest can observe the close; and tunnel releases it before emitting its event, which covers a tunnel whose 200 could not be written. The slot is the conn's entry in vmBinding.conns, so the release is exactly-once however many paths call it; the handler keeps a deferred release as a backstop. TestTunnelSlotIsFreeOnceItsEventAndCloseArrive parks the first conn's handler inside its tunnel event, holding the old window open, and fails every time without the fix. Closes rossoctl#783 Assisted-By: Claude Code Signed-off-by: Rong Chang <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Summary
Moves the per-VM conn_cap slot release from a deferred step of the handler goroutine into boundConn.Close (before the socket close) and into tunnel (before its terminal event). The release is idempotent because it deletes the vmBinding.conns map entry under Proxy.mu. I checked lock ordering: release is never reached with Proxy.mu or pc.mu held, since admit's close paths run before c.release is set, and abort/finish drop pc.mu before closing. Refusals still hold their slot through writeAndClose. The new gated test exercises the race deterministically. One non-blocking gap: on intercepted (TLS) conns, tls.Conn.Close sends close_notify before the underlying close, so the guest can still see an orderly close before the slot is freed.
Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (remote-worker/internal/egress), tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (17/17 checks green, including DCO, lint, CodeQL, k8s-kind-e2e)
| // has seen its conn closed may connect again at once, and a cap of one must | ||
| // then admit it: freeing the slot after the close, as a deferred step of the | ||
| // handler, left a window in which the guest's next conn met conn_cap. Every | ||
| // wrapper the handlers put around the conn (the TLS session, the idle clock) |
There was a problem hiding this comment.
suggestion: this holds for the plain tunnel and forward paths, but not quite for an intercepted CONNECT. c.gtls is a *tls.Conn, and tls.Conn.Close sends close_notify first (it sets its own 5 s write deadline to do it) and only then closes the underlying conn. So on an intercepted conn that ends by the idle bound or next() returning false, the guest gets an orderly TLS EOF before boundConn.Close runs release. That is the same window #783 closes everywhere else, and a TLS client may reconnect as soon as it sees close_notify. The window is small (one alert write), and the PR body's claim that the slot is "free before the guest can see the conn closed" on every exit path doesn't hold here.
Cheap fix: call pc.release() in the interceptor's deferred cleanup (intercept.go, the defer func() { _ = c.gtls.Close(); ... }()) before c.gtls.Close(), and narrow this comment to match. A variant of TestTunnelSlotIsFreeOnceItsEventAndCloseArrive over an intercepted host would cover it, though there is no terminal event there to gate on, so it would need to gate on the guest-side read instead.
There was a problem hiding this comment.
Good catch, fixed in bfc63b4. The interceptor's deferred cleanup now calls pc.release() before c.gtls.Close(), so the slot is free before close_notify goes out. The boundConn.Close comment now says that this one wrapper speaks before it, and that the intercepting path releases first. A refusal on that path still holds its slot through its drain: the deferred release runs only after refuse returns.
No new test for it. As you note, there is no terminal event to gate on, and the window is one alert write. With no seam to hold it open, a guest-side read test would pass with or without the fix. go test -race ./internal/egress/... passes, and so do the intercept and slot tests at -count=3.
Assisted-By: Claude Code
…tify tls.Conn.Close writes close_notify before it closes the underlying conn, so on the intercepting path the guest could read an orderly EOF before boundConn.Close released the slot (review suggestion). The interceptor's cleanup now releases first; the boundConn.Close comment says which wrapper speaks before it. Part of rossoctl#783 Assisted-By: Claude Code Signed-off-by: Rong Chang <[email protected]>
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837, rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824), egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer (rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers (rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors (rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821). Conflicts (one): - deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843) rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA row after SBX_PROFILE, then re-padding the table with prettier. The fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly. Merged without markers, checked: setup.sh keeps the fork's SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS; both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases beside upstream's. The fork's RUNBOOK, README and template-onboarding docs say nothing about sandbox lifetime, refusal bursts, login errors or the create rollback, so none needed a correction; GUIDE.md is unchanged and GUIDE.pdf is not regenerated. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
Closes #783
What changed and why
MaxConnsPerVM, theconn_cap) was freed in a deferred step of the conn's handler goroutine inremote-worker/internal/egress/proxy.go, which ran after the conn's terminaltunnelevent was emitted and after the guest conn was closed. A guest that reconnected as soon as it saw either could race that step and get429 conn_cap. This is the intermittentconnect_tunnel_idle_deadlinefailure (cap of one, second CONNECT right after the first tunnel's event and close).boundConn.Close(identity.go) now releases the slot before it closes the socket. Every wrapper the handlers put around the guest conn (the idle clock, the TLS session) closes through it, so on every exit path (normal end, refusal, abort byUnregister,Proxy.Close) the slot is free before the guest can see the conn closed.tunnel(connect.go) releases the slot before emitting itstunnelevent, so the event is proof the slot is free. After a splice the close has already released it; this call covers a tunnel whose 200 could not be written.proxyConn.release(proxy.go) deletes the conn fromvmBinding.connsunderProxy.mu. The slot is that map entry, so the release happens exactly once however many paths call it. The handler goroutine keeps a deferredreleaseas a backstop. The lock order does not change:pc.muis never held around a close.writeAndClose) until the proxy closes the conn, so the cap still bounds the sockets a VM can hold open.Tests
TestTunnelSlotIsFreeOnceItsEventAndCloseArrive(remote-worker/internal/egress/slot_test.go). With a cap of one and a 200 ms idle bound, a tunnel to a silent upstream expires. The test sink parks the first conn's handler inside itstunnelevent, which holds the old window open. The test then checks that the guest conn is closed and that a second CONNECT gets 200.go test -race -count=5 -run TestTunnelSlotIsFree ./internal/egressfailed 5 of 5 withsecond CONNECT: status 429 "conn_cap: vm-1\n", want 200 (the ended tunnel still held its slot).-race -count=20passed 20 of 20.go test -race -count=200 -run 'TestConformance/inprocess/connect_tunnel_idle_deadline' ./internal/egress: passed 200 of 200 with the fix. Run concurrently with the same command on main and a fullgo test ./...for load. On this machine, main did not reproduce the CI failure in 200 runs under that load, or in another 200 (-cpu 1,2 -count=100). So the new unit test, not the conformance repeat, is the before/after evidence.remote-worker, with the fix:go vet ./...is clean.go test -race ./internal/egress/...passes.go test ./...passes. Threeinternal/vmpoolfirecracker-jailer timing tests failed once while the 200-run loops were loading the machine, then passed on a rerun alone and in a second full run.Every commit is signed off (DCO).
Assisted-By: Claude Code