Skip to content
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

don't call into CombinedOutput for subprocesses #15

Merged
merged 1 commit into from
Mar 10, 2022

Conversation

rickystewart
Copy link
Contributor

The documentation for exec.Cmd says this about Stdout and Stderr:

// If either is an *os.File, the corresponding output from the process
// is connected directly to that file.
//
// Otherwise, during the execution of the command a separate goroutine
// reads from the process over a pipe and delivers that data to the
// corresponding Writer. In this case, Wait does not complete until the
// goroutine reaches EOF or encounters an error.

When calling CombinedOutput(), Stdout and Stderr are
bytes.Buffers and are therefore not *os.Files so they fall into
this second group. This resulted in a race condition where cancelling
the context when maxtime has passed could cause CombinedOutput() to
hang indefinitely waiting for the (finished) subprocess to "finish"
writing to its pipes.

This has been reported as an issue several times. The tracking issue is
golang/go#23019 which itself links to several
other issues that are duplicates.

To work around the issue we simply force the other behavior by creating
a temporary *os.File for the combined stdout and stderr.

The documentation for `exec.Cmd` says this about `Stdout` and `Stderr`:

    // If either is an *os.File, the corresponding output from the process
    // is connected directly to that file.
    //
    // Otherwise, during the execution of the command a separate goroutine
    // reads from the process over a pipe and delivers that data to the
    // corresponding Writer. In this case, Wait does not complete until the
    // goroutine reaches EOF or encounters an error.

When calling `CombinedOutput()`, `Stdout` and `Stderr` are
`bytes.Buffer`s and are therefore not `*os.File`s so they fall into
this second group. This resulted in a race condition where cancelling
the context when `maxtime` has passed could cause `CombinedOutput()` to
hang indefinitely waiting for the (finished) subprocess to "finish"
writing to its pipes.

This has been reported as an issue several times. The tracking issue is
golang/go#23019 which itself links to several
other issues that are duplicates.

To work around the issue we simply force the other behavior by creating
a temporary `*os.File` for the combined `stdout` and `stderr`.
@rickystewart rickystewart requested a review from rail March 8, 2022 18:47
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@rail rail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @rickystewart)

@rickystewart rickystewart merged commit 58fb462 into master Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants