From 9d1901c4b5487caa8c5e0abb2f49120b5ffbaa4a Mon Sep 17 00:00:00 2001 From: amirejaz Date: Mon, 10 Aug 2026 18:56:22 +0100 Subject: [PATCH] Deflake the initialize dial-error proxy test TestRoundTripDoesNotReplayInitializeOnDialError failed intermittently in a full-suite run with "Received unexpected error" on the HTTP call, while passing every time in isolation. It used http.DefaultClient, whose shared transport pools keep-alive connections across this package's parallel tests. Reusing one whose proxy has since been closed surfaces as a transport error before the request is made. Use a dedicated client with keep-alives disabled. The HTTP outcome is also not what the test asserts. For a dial failure either result is legitimate -- the reverse proxy may synthesize a 502, or the connection may fail outright -- so requiring NoError pinned an incidental detail. The substantive assertions, that no re-initialize reached the target service and that the session was unpinned, are unchanged and still fail when the production guard is removed. Co-Authored-By: Claude Opus 5 (1M context) --- .../proxy/transparent/backend_routing_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/transport/proxy/transparent/backend_routing_test.go b/pkg/transport/proxy/transparent/backend_routing_test.go index 0b22f26496..7346105ad3 100644 --- a/pkg/transport/proxy/transparent/backend_routing_test.go +++ b/pkg/transport/proxy/transparent/backend_routing_test.go @@ -820,9 +820,17 @@ func TestRoundTripDoesNotReplayInitializeOnDialError(t *testing.T) { req.Header.Set("Content-Type", "application/json") req.Header.Set("Mcp-Session-Id", clientSessionID) - resp, err := http.DefaultClient.Do(req) - require.NoError(t, err) - _ = resp.Body.Close() + // A dedicated client, not http.DefaultClient: the shared transport pools + // keep-alive connections across the package's parallel tests, and reusing + // one whose proxy has since been closed surfaces here as a transport error. + client := &http.Client{Transport: &http.Transport{DisableKeepAlives: true}} + resp, err := client.Do(req) + // Either outcome is legitimate for a dial failure -- the reverse proxy may + // synthesize a 502, or the connection may fail outright. Neither is what + // this test is about; the assertions below are. + if err == nil { + _ = resp.Body.Close() + } assert.Zero(t, targetHits.Load(), "the proxy must not send its own initialize to the target service for an initialize dial error")