Repository navigation
fix(exec): stop RunProcess from hanging on large stderr output #882
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
Open
lbarrosby
wants to merge
2
commits into
helm:main
Choose a base branch
from
lbarrosby:fix-runprocess-pipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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,144 @@ | ||
| // Copyright The Helm Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package exec | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // runProcess runs a shell script through RunProcess and fails the test if it | ||
| // does not return within the timeout. | ||
| func runProcess(t *testing.T, timeout time.Duration, script string) error { | ||
| t.Helper() | ||
|
|
||
| p := NewProcessExecutor(false) | ||
| errCh := make(chan error, 1) | ||
| go func() { | ||
| errCh <- p.RunProcess("sh", "-c", script) | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-errCh: | ||
| return err | ||
| case <-time.After(timeout): | ||
| t.Fatal("RunProcess did not return") | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func TestRunProcessDoesNotBlock(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| }{ | ||
| { | ||
| // More stderr than a pipe buffer holds, written before any stdout. | ||
| name: "large stderr", | ||
| script: `yes warning | head -n 200000 >&2; echo done`, | ||
| }, | ||
| { | ||
| // A single line longer than bufio.Scanner's default limit. | ||
| name: "long line", | ||
| script: `head -c 1048576 /dev/zero | tr '\0' 'x'; echo; echo done`, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if err := runProcess(t, 30*time.Second, tt.script); err != nil { | ||
| t.Fatalf("RunProcess returned an error: %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // startBackgroundSleep returns a script fragment that starts a long sleep in | ||
| // the background, inheriting the command's stdout and stderr, and kills it | ||
| // when the test ends. | ||
| func startBackgroundSleep(t *testing.T) string { | ||
| t.Helper() | ||
|
|
||
| pidFile := filepath.Join(t.TempDir(), "pid") | ||
| t.Cleanup(func() { | ||
| b, err := os.ReadFile(pidFile) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if pid, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil { | ||
| _ = syscall.Kill(pid, syscall.SIGKILL) | ||
| } | ||
| }) | ||
| return "sleep 60 & echo $! > " + pidFile + ";" | ||
| } | ||
|
|
||
| func TestRunProcessDoesNotWaitForBackgroundChildren(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| script string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "command succeeds", | ||
| script: "echo started", | ||
| }, | ||
| { | ||
| name: "command fails", | ||
| script: "exit 3", | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| script := startBackgroundSleep(t) + tt.script | ||
| start := time.Now() | ||
| err := runProcess(t, 20*time.Second, script) | ||
| if tt.wantErr != (err != nil) { | ||
| t.Fatalf("RunProcess returned error %v, want error: %t", err, tt.wantErr) | ||
| } | ||
| if elapsed := time.Since(start); elapsed > 10*time.Second { | ||
| t.Fatalf("RunProcess took %s, it waited for the background child", elapsed) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRunProcessReturnsWhenStdoutIsBroken(t *testing.T) { | ||
| reader, writer, err := os.Pipe() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| reader.Close() | ||
| stdout := os.Stdout | ||
| os.Stdout = writer | ||
| t.Cleanup(func() { | ||
| os.Stdout = stdout | ||
| writer.Close() | ||
| }) | ||
|
|
||
| // Much more than a pipe buffer holds, so the command blocks if nothing | ||
| // drains or closes its end of the pipe. | ||
| err = runProcess(t, 20*time.Second, `head -c 2097152 /dev/zero`) | ||
| if !errors.Is(err, syscall.EPIPE) { | ||
| t.Fatalf("RunProcess returned error %v, want the broken pipe error from writing its output", err) | ||
| } | ||
| } |
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.