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

os/exec: cmd.Wait does not return after process termination when stdin is attached #10338

Closed
sykesm opened this issue Apr 3, 2015 · 2 comments
Milestone

Comments

@sykesm
Copy link

sykesm commented Apr 3, 2015

The following code terminates with a deadlock error when run in isolation. If we replace the usage of io.Pipe with cmd.StdinPipe(), the program terminates normally with the expected error from cmd.Wait().

This is on Darwin 14.1.0 with go 1.4.2.

package main

import (
    "fmt"
    "io"
    "syscall"
)

import "os/exec"

func main() {
    pr, pw := io.Pipe()

    cmd := exec.Command("/bin/bash", "-c", "cat")
    cmd.Stdin = pr

    started := make(chan error)
    done := make(chan error)
    go func() {
        started <- cmd.Start()
        done <- cmd.Wait()
    }()

    <-started
    println("Started")
    pw.Write([]byte("hello"))

    cmd.Process.Signal(syscall.SIGTERM)

    err := <-done
    println("Done")
    if err != nil {
        fmt.Printf("wait err: %s\n", err.Error())
    }
}
@ianlancetaylor ianlancetaylor added this to the Go1.5 milestone Apr 3, 2015
@zombiezen
Copy link
Contributor

The root cause is that Wait blocks for all io.Copy operations to complete. This is correct, but in the test case here, the deadlock is on io.pipe.rwait. Adding a pw.Close after pw.Write will remove the deadlock. stdout/stderr won't have this problem since they will receive the broken pipe error.

Fixing this isn't straightforward. Ideally, there would be a way to interrupt the read call in stdin()'s copy goroutine. It could be emulated with a select and an interrupt channel, but then that can leak a copy goroutine. I'm unsure of a clean way of fixing this.

@rsc
Copy link
Contributor

rsc commented Jul 15, 2015

Will document. Note that if you use os.Pipe this does not happen.

@rsc rsc closed this as completed in d000e87 Jul 15, 2015
@golang golang locked and limited conversation to collaborators Jul 18, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants