From 4364f52066dcd971f4117973536268a65a734786 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 30 Nov 2026 14:44:24 +0000 Subject: [PATCH 1/2] ssh: fix flaky TestCreateWebsocketConnectionRetriesRequestTimeout The test server closed each websocket while the client's acknowledgment frames were still unread. Closing a socket with unread data resets the connection, and the client could then lose the server's "finished" close frame (Windows discards unread data on a reset; on any OS the client's ack write can fail and close its own side first). The client then saw a drop instead of the end of the session and reattached a third time. The handler now waits for the client to acknowledge "before" before it drops the first connection, and reads until the client's close reply after sending "finished". Co-authored-by: Isaac --- .../ssh/internal/client/websockets_test.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/experimental/ssh/internal/client/websockets_test.go b/experimental/ssh/internal/client/websockets_test.go index 05d15636688..468974813d6 100644 --- a/experimental/ssh/internal/client/websockets_test.go +++ b/experimental/ssh/internal/client/websockets_test.go @@ -94,14 +94,28 @@ func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { return } defer conn.Close() + // Read every frame the client sends before closing. Closing a socket with unread data + // makes the OS reset the connection, and on Windows the reset discards what the client + // had not read yet, so the frames written here could be lost and the test would see an + // extra drop. + assert.NoError(t, conn.SetReadDeadline(time.Now().Add(5*time.Second))) if !reattach { assert.NoError(t, conn.WriteMessage(websocket.BinaryMessage, []byte("before"))) + // Drop the connection only once the client has acknowledged "before". + waitForDelivered(t, conn, 6) return } assert.Equal(t, "6", r.URL.Query().Get("delivered")) assert.NoError(t, conn.WriteJSON(map[string]int{"delivered": 0})) assert.NoError(t, conn.WriteMessage(websocket.BinaryMessage, []byte("after"))) assert.NoError(t, conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "finished"))) + // Wait for the client's close frame, discarding its acknowledgments. + for { + if _, _, err := conn.ReadMessage(); err != nil { + assert.True(t, websocket.IsCloseError(err, websocket.CloseNormalClosure), "unexpected read error: %v", err) + return + } + } }) server := httptest.NewServer(mux) defer server.Close() @@ -122,6 +136,21 @@ func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { assert.Equal(t, "beforeafter", output.String()) } +// waitForDelivered reads the client's acknowledgments until one reports delivered bytes. +func waitForDelivered(t *testing.T, conn *websocket.Conn, delivered int) { + for { + var msg struct { + Delivered int `json:"delivered"` + } + if err := conn.ReadJSON(&msg); !assert.NoError(t, err) { + return + } + if msg.Delivered == delivered { + return + } + } +} + func TestBuildProxyWebsocketURL(t *testing.T) { tests := []struct { name string From 404b90da47c076e1fe81875461f20dd3c97da95b Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Mon, 30 Nov 2026 16:23:14 +0000 Subject: [PATCH 2/2] ssh: wait for the websocket test handlers before the test returns Address review: the server does not track hijacked connections, so a handler assertion could run after the test returned. Also clarify the waitForDelivered doc comment. Co-authored-by: Isaac --- .../ssh/internal/client/websockets_test.go | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/experimental/ssh/internal/client/websockets_test.go b/experimental/ssh/internal/client/websockets_test.go index 468974813d6..aa1f525993e 100644 --- a/experimental/ssh/internal/client/websockets_test.go +++ b/experimental/ssh/internal/client/websockets_test.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/http/httptest" + "sync" "sync/atomic" "testing" "time" @@ -82,8 +83,13 @@ func TestCreateWebsocketConnectionReattachRejected(t *testing.T) { func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { var reattachments atomic.Int32 + // The server does not track hijacked connections, so server.Close does not wait for the + // handlers. The test waits for them itself, so their assertions run before it returns. + var handlers sync.WaitGroup mux := http.NewServeMux() mux.HandleFunc("GET /driver-proxy-api/o/123/cluster/7772/ssh", func(w http.ResponseWriter, r *http.Request) { + handlers.Add(1) + defer handlers.Done() reattach := r.URL.Query().Get("reattach") == "1" if reattach && reattachments.Add(1) == 1 { w.WriteHeader(http.StatusRequestTimeout) @@ -130,13 +136,26 @@ func TestCreateWebsocketConnectionRetriesRequestTimeout(t *testing.T) { func(ctx context.Context, req proxy.DialRequest) (*websocket.Conn, error) { return createWebsocketConnection(ctx, client, req, "cluster", 7772, "") }) + // The client dials nothing more once RunClientProxy returns, so no handler can start after + // this Wait begins. + handlersDone := make(chan struct{}) + go func() { + handlers.Wait() + close(handlersDone) + }() + select { + case <-handlersDone: + case <-time.After(10 * time.Second): + t.Fatal("timed out waiting for the server handlers to return") + } require.NoError(t, err) assert.NoError(t, ctx.Err()) assert.Equal(t, int32(2), reattachments.Load()) assert.Equal(t, "beforeafter", output.String()) } -// waitForDelivered reads the client's acknowledgments until one reports delivered bytes. +// waitForDelivered reads the client's acknowledgments until one reports exactly delivered bytes +// written to its output, failing the test if the read deadline passes first. func waitForDelivered(t *testing.T, conn *websocket.Conn, delivered int) { for { var msg struct {