carlos / platform Public

Fix the #471 tunnelclient flake in TestRoundTripUpgradeEndsWithItsContext/during_the_handshake (test only, no production defect). The test cancelled once the hub had the box's marked 200, but that does not mean the box's Hijack had returned: x/net (v0.60.0, serverConn.writeHeaders) queues the reply to the flush before the 200 reaches the wire, and the handler then selects on that reply and on the stream's close. When the handler goroutine had not run by the time the hub's RST_STREAM was processed, both were ready and Go picked at random; picking the close made Hijack fail with "http2: stream closed", the fixture returned before its conn Read, and the test's wait for that Read timed out. The hub did reset the stream on every run; the box only learned of it in Hijack rather than in Read, which is correct behaviour (the box edge's reverse proxy treats a failed hijack as an error and closes the app conn). The fixture now closes boxEnd.hijacked once Hijack returns, and the test waits for that event as well as the hub's handshake hook before cancelling. Before: 88 of 200 runs failed without -race on DOCS-T1. After: 200/200 under -race, 200/200 without, 500 per -cpu=1,2,4,8, package -count=20 and go vet clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from fix-tunnelclient-upgrade-flake into main.
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 62e19e362b1a0286a0a69f8c36cd2ce1d7b94707 parent 2c66e56
1 file changed, +19 −5
  • internal/tunnelclient/upgrade_hub_test.go +19 −5

CI cancelled — superseded by a newer push — run details

diff --git a/internal/tunnelclient/upgrade_hub_test.go b/internal/tunnelclient/upgrade_hub_test.go
index b5caef948..fc560a432 100644
--- a/internal/tunnelclient/upgrade_hub_test.go
+++ b/internal/tunnelclient/upgrade_hub_test.go
@@ -40,11 +40,12 @@ func upgradeRequest(tok string) *http.Request {
return req
}
-// boxEnd is one box handler's view of the test: what it saw and when it
-// returned.
+// boxEnd is one box handler's view of the test: what it saw, when its
+// Hijack returned the conn, and when it returned.
type boxEnd struct {
- req chan *http.Request
- done chan struct{}
+ req chan *http.Request
+ hijacked chan struct{}
+ done chan struct{}
}
// hijackingBox is the box's edge as the shim sees it: it hijacks the
@@ -65,13 +66,14 @@ func hijackingBox(t *testing.T, ends <-chan *boxEnd, play func(r *http.Request,
t.Errorf("hijack: %v", err)
return
}
+ close(e.hijacked)
defer conn.Close()
play(r, conn, brw)
}))
}
func newBoxEnd(ends chan<- *boxEnd) *boxEnd {
- e := &boxEnd{req: make(chan *http.Request, 1), done: make(chan struct{})}
+ e := &boxEnd{req: make(chan *http.Request, 1), hijacked: make(chan struct{}), done: make(chan struct{})}
ends <- e
return e
}
@@ -543,6 +545,18 @@ func TestRoundTripUpgradeEndsWithItsContext(t *testing.T) {
case err := <-errc:
t.Fatalf("RoundTripUpgrade returned before the handshake read: %v", err)
}
+ // And the box's Hijack has returned. The hub having the 200 does
+ // not mean the box's flush has: x/net queues the reply to the
+ // flush before the 200 goes out, but if the handler goroutine has
+ // not taken it when the hub's reset lands, its select may see the
+ // stream closed instead and Hijack fails ("http2: stream closed").
+ // That is the box learning of the reset early, not a missing
+ // reset; waiting here makes the conn's Read the one place it shows.
+ select {
+ case <-e.hijacked:
+ case <-e.done:
+ t.Fatal("the box handler returned without hijacking")
+ }
cancel()
select {
case err := <-errc: