On Mon, Jun 17, 2024 at 7:49 PM Salvatore Domenick Desiano
<neard...@gmail.com> wrote:
>
> Here's a hopefully semantically identical minimal example:
>
> func main() {
>
> cmdName := "/bin/bash"
> cmdArgs := []string{"-c", "sleep 5"}
> cmdStdinR, cmdStdinW := io.Pipe()
> cmdStdoutR, cmdStdoutW := io.Pipe()
> go func() {
>
> defer cmdStdinW.Close()
>
> io.Copy(cmdStdinW, cmdStdoutR)
>
> }()
> cmd := exec.Command(cmdName, cmdArgs...)
> cmd.Stdout = cmdStdoutW
> cmd.Stdin = cmdStdinR
> if err := cmd.Run(); err != nil {
>
> fmt.Println(err)
>
> }
>
> }
>
> This program deadlocks after seconds. The three locked Go routines are the 
> io.Copy( (stuck inside io.Copy because cmdStdoutR doesn't close), the 
> exec.Command Go routine (also stuck in an io.Copy) and the main routine 
> (stuck in cmd.Wait()).


I haven't checked whether this is the problem, but as someone else
pointed out this is a strange place to use io.Pipe.  Much better to
use os.Pipe.  Much better still to use exec.Cmd.StdinPipe and
exec.Cmd.StdoutPipe.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVtbc-77oZ5Pibm1Jg6DJKwgvCT7xXLyqU_fPz8%2B0F1dg%40mail.gmail.com.

Reply via email to