Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions internal/provision/k8s/exec_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading