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()). On Mon, Jun 17, 2024 at 10:45 PM Salvatore Domenick Desiano < neard...@gmail.com> wrote: > I've got some code that is basically Expect > <https://sourceforge.net/projects/expect/>-lite written in Go. I've got a > deadlock and I'm hoping someone here can help me untangle it. I posted this > yesterday but at the time I thought the Expect part of the code wasn't > relevant and I left it out. I now believe that part of the code *is* the > problem. > > I have: > > 1. exec.Command() with stdin and stdout replaced with io.Pipe() > 2. a Go routine listening to stdout, watching for strings, and writing > to stdin > > I know that Wait() won't return until stdin is closed. The problem is that > I can't tell when I need to close stdin. Since Wait() doesn't return, the > main thread can't do it. For some reason stdout doesn't get closed even > with the program exits (stdout.Read() is part of the deadlock) so the > Expect Go routine can't do it. > > The only thing I can think of is *another* Go routine monitoring > cmd.ProcessState but that doesn't feel right. > > What am I missing? > > Thank you! > > -- Salvatore > smile. > > -- 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/CAEQs7S_O0LkY6s2emzxPscrNpOU6tACimuHJgM84nGOuMxAqRw%40mail.gmail.com.