From a074973b5ade907e6bd200889e4daaf72007d731 Mon Sep 17 00:00:00 2001 From: Zachary Lowden Date: Fri, 4 Dec 2026 00:02:11 -0600 Subject: [PATCH] k8s exec test: the fake exec server waits for every SYN_REPLY before writing TestExecWithOneBufferForStdoutAndStderrIsSafe failed once in CI with "exec into termy: Timeout occurred" after 30s. The fault is in the fake server, not the driver. apimachinery's SPDY upgrader calls the new-stream handler BEFORE it sends that stream's SYN_REPLY, and hands it a replySent channel for exactly this reason; the kubelet waits on every replySent before it starts the command. The fake ignored it, so the stderr stream could reach ServeHTTP with its reply still unsent, and the flood started. On the client, client-go creates its streams one at a time and reads none until all exist, and spdystream's per-stream dataChan is unbuffered: the stdout frame worker blocks on the first data frame, its 50-frame queue fills, and the connection's single read loop blocks pushing the next one, never reaching stderr's SYN_REPLY behind it. CreateStream then times out after createStreamResponseTimeout (30s). The fake now collects each stream's replySent and waits for all of them before writing anything. The flood test also sets replyLag, which delays the stderr reply by 50ms, so the losing interleaving happens on every run instead of about once in a thousand. Measured with the race-enabled test binary on an 8-core host: - before, -count=200: 0 of 400 subtests failed - before, -count=300 -cpu 1,2: 1 of 1200 failed, "Timeout occurred" (the CI symptom) - before, with the 50ms reply delay: 2 of 2 failed, "Timeout occurred" after 30s each - after (delay on), -count=200: 0 of 400; -count=300 -cpu 1,2: 0 of 1200 - the fix with its wait loop removed (delay on): 2 of 2 failed, Timeout - the driver's exec sink removed: the test still fails under -race (16 DATA RACE reports plus a short byte count), so what it checks is unchanged Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/provision/k8s/exec_stream_test.go | 44 +++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/internal/provision/k8s/exec_stream_test.go b/internal/provision/k8s/exec_stream_test.go index 0557a6eb0..09989e68d 100644 --- a/internal/provision/k8s/exec_stream_test.go +++ b/internal/provision/k8s/exec_stream_test.go @@ -67,6 +67,11 @@ type execServer struct { // from the frame already buffered client-side — writes that happen whether // or not the connection is still open. big bool + // replyLag, when set, holds the stderr stream's SYN_REPLY back by this + // long after the stream reaches ServeHTTP: the scheduling gap in which a + // server that writes before every reply is sent deadlocks the client (see + // ServeHTTP). It turns a once-in-a-thousand interleaving into every run. + replyLag time.Duration } // floodChunk is one write of a flood. @@ -111,9 +116,16 @@ func (s *execServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if tty { want++ // resize } - streams := make(chan httpstream.Stream, want) - conn := spdy.NewResponseUpgrader().UpgradeResponse(w, r, func(st httpstream.Stream, _ <-chan struct{}) error { - streams <- st + type arrived struct { + st httpstream.Stream + replied <-chan struct{} + } + streams := make(chan arrived, want) + conn := spdy.NewResponseUpgrader().UpgradeResponse(w, r, func(st httpstream.Stream, replySent <-chan struct{}) error { + streams <- arrived{st, replySent} + if s.replyLag > 0 && st.Headers().Get(corev1.StreamType) == corev1.StreamTypeStderr { + time.Sleep(s.replyLag) + } return nil }) if conn == nil { @@ -122,15 +134,36 @@ func (s *execServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer conn.Close() byType := map[string]httpstream.Stream{} + var replies []<-chan struct{} for len(byType) < want { select { - case st := <-streams: - byType[st.Headers().Get(corev1.StreamType)] = st + case a := <-streams: + byType[a.st.Headers().Get(corev1.StreamType)] = a.st + replies = append(replies, a.replied) case <-time.After(5 * time.Second): s.t.Errorf("only %d of %d streams arrived: %v", len(byType), want, keys(byType)) return } } + // 🔴 NOTHING IS WRITTEN UNTIL EVERY STREAM'S SYN_REPLY HAS BEEN SENT, as a + // kubelet does (it waits on each replySent before it starts the command). + // The handler above returns BEFORE apimachinery sends the reply, so the last + // stream can reach this goroutine with its reply still unsent. Writing then + // deadlocks the client for createStreamResponseTimeout (30s): client-go + // creates its streams one at a time and reads none of them until all exist, + // spdystream's per-stream dataChan is unbuffered, so the stdout worker blocks + // on the first data frame, its 50-frame queue fills, and the connection's one + // read loop blocks pushing the next one — never reaching stderr's SYN_REPLY + // behind it ("Timeout occurred"). Stderr data that beats its reply is also + // dropped by the client as unreplied. + for _, replied := range replies { + select { + case <-replied: + case <-time.After(5 * time.Second): + s.t.Error("a stream's SYN_REPLY was never sent") + return + } + } if rs := byType[corev1.StreamTypeResize]; rs != nil { go func() { dec := json.NewDecoder(rs) @@ -402,6 +435,7 @@ func TestExecWithOneBufferForStdoutAndStderrIsSafe(t *testing.T) { eachMode(t, func(t *testing.T, m nsMode) { srv := newExecServer(t) srv.flood = 64 + srv.replyLag = 50 * time.Millisecond d, ref, _ := execRig(t, m, srv) for _, viaContainer := range []bool{false, true} { var out bytes.Buffer