-
Notifications
You must be signed in to change notification settings - Fork 663
driver/kubernetes: close exec-stream pipes when the stream ends #3966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
crazy-max
merged 1 commit into
docker:master
from
pgimalac:pgimalac/kubernetes-execconn-close-on-stream-end
Jul 23, 2026
+128
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package execconn | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "k8s.io/client-go/tools/remotecommand" | ||
| ) | ||
|
|
||
| // fakeExecutor is a fake remotecommand.Executor whose StreamWithContext blocks until | ||
| // unblock is closed, then returns err. It stands in for the real SPDY exec stream | ||
| // to a builder pod, so the pipe-closing behavior in newExecConn can be tested | ||
| // without a real Kubernetes API server. | ||
| type fakeExecutor struct { | ||
| unblock chan struct{} | ||
| err error | ||
| } | ||
|
|
||
| func (f *fakeExecutor) Stream(_ remotecommand.StreamOptions) error { | ||
| panic("unimplemented") | ||
| } | ||
|
|
||
| func (f *fakeExecutor) StreamWithContext(ctx context.Context, _ remotecommand.StreamOptions) error { | ||
| select { | ||
| case <-f.unblock: | ||
| return f.err | ||
| case <-ctx.Done(): | ||
| return context.Cause(ctx) | ||
| } | ||
| } | ||
|
|
||
| func TestNewExecConnPropagatesStreamEnd(t *testing.T) { | ||
| t.Run("stream ends with an error", func(t *testing.T) { | ||
| streamErr := errors.New("exec stream terminated") | ||
| fe := &fakeExecutor{ | ||
| unblock: make(chan struct{}), | ||
| err: streamErr, | ||
| } | ||
|
|
||
| conn := newExecConn(context.Background(), fe) | ||
| close(fe.unblock) | ||
|
|
||
| _, err := conn.Read(make([]byte, 16)) | ||
| require.ErrorIs(t, err, streamErr) | ||
|
|
||
| _, err = conn.Write([]byte("test")) | ||
| require.ErrorIs(t, err, streamErr) | ||
| }) | ||
|
|
||
| t.Run("stream ends with no error", func(t *testing.T) { | ||
| fe := &fakeExecutor{unblock: make(chan struct{})} | ||
|
|
||
| conn := newExecConn(context.Background(), fe) | ||
| close(fe.unblock) // StreamWithContext returns nil | ||
|
|
||
| _, err := conn.Read(make([]byte, 16)) | ||
| require.ErrorIs(t, err, io.EOF) | ||
|
|
||
| _, err = conn.Write([]byte("test")) | ||
| require.ErrorIs(t, err, io.ErrClosedPipe) | ||
| }) | ||
|
|
||
| t.Run("stream still active: reads stay blocked, not closed early", func(t *testing.T) { | ||
| fe := &fakeExecutor{unblock: make(chan struct{})} | ||
| conn := newExecConn(context.Background(), fe) | ||
| defer close(fe.unblock) | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| buf := make([]byte, 16) | ||
| _, _ = conn.Read(buf) //nolint:errcheck | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| t.Fatal("Read returned before the exec stream ended; it should still be blocked") | ||
| case <-time.After(200 * time.Millisecond): | ||
| // expected: still blocked, exactly like a real in-progress build | ||
| } | ||
| }) | ||
|
|
||
| t.Run("stream still active: writes stay blocked, not closed early", func(t *testing.T) { | ||
| fe := &fakeExecutor{unblock: make(chan struct{})} | ||
| conn := newExecConn(context.Background(), fe) | ||
| defer close(fe.unblock) | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| _, _ = conn.Write([]byte("test")) //nolint:errcheck | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| t.Fatal("Write returned before the exec stream ended; it should still be blocked") | ||
| case <-time.After(200 * time.Millisecond): | ||
| // expected: still blocked, exactly like a real in-progress build | ||
| } | ||
| }) | ||
|
|
||
| t.Run("stream cancelled by context", func(t *testing.T) { | ||
| fe := &fakeExecutor{unblock: make(chan struct{})} | ||
| ctx, cancel := context.WithCancelCause(context.Background()) | ||
| conn := newExecConn(ctx, fe) | ||
| defer close(fe.unblock) | ||
|
|
||
| cancel(context.Canceled) | ||
|
|
||
| _, err := conn.Read(make([]byte, 16)) | ||
| require.ErrorIs(t, err, context.Canceled) | ||
|
|
||
| _, err = conn.Write([]byte("test")) | ||
| require.ErrorIs(t, err, context.Canceled) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.