Skip to content

Commit

Permalink
Avoid possible deadlock in util.RunCmdOut by using byte buffers inste…
Browse files Browse the repository at this point in the history
…ad of pipes (#5220)
  • Loading branch information
briandealwis authored Jan 20, 2021
1 parent 37ed158 commit dafb415
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions pkg/skaffold/util/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package util

import (
"bytes"
"fmt"
"io/ioutil"
"os/exec"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -70,46 +70,32 @@ type Commander struct{}
// RunCmdOut runs an exec.Command and returns the stdout and error.
func (*Commander) RunCmdOut(cmd *exec.Cmd) ([]byte, error) {
logrus.Debugf("Running command: %s", cmd.Args)
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}

stderrPipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdout := bytes.Buffer{}
cmd.Stdout = &stdout
stderr := bytes.Buffer{}
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("starting command %v: %w", cmd, err)
}

stdout, err := ioutil.ReadAll(stdoutPipe)
if err != nil {
return nil, err
}

stderr, err := ioutil.ReadAll(stderrPipe)
if err != nil {
return nil, err
}

if err := cmd.Wait(); err != nil {
return stdout, &cmdError{
return stdout.Bytes(), &cmdError{
args: cmd.Args,
stdout: stdout,
stderr: stderr,
stdout: stdout.Bytes(),
stderr: stderr.Bytes(),
cause: err,
}
}

if len(stderr) > 0 {
logrus.Debugf("Command output: [%s], stderr: %s", stdout, stderr)
if stderr.Len() > 0 {
logrus.Debugf("Command output: [%s], stderr: %s", stdout.String(), stderr.String())
} else {
logrus.Debugf("Command output: [%s]", stdout)
logrus.Debugf("Command output: [%s]", stdout.String())
}

return stdout, nil
return stdout.Bytes(), nil
}

// RunCmd runs an exec.Command.
Expand Down

0 comments on commit dafb415

Please sign in to comment.