On Sun, Jun 16, 2024 at 3:20 PM Salvatore Domenick Desiano
<neard...@gmail.com> wrote:
>
> I'm hoping y'all can help me get the correct semantics for code that sets up 
> an external process, replaces Stdin, Stderr, Stdout, runs the command, calls 
> exec.Command.Wait() and handles SIGKILL correctly.
>
> I've read more than two dozen threads, PRs, and bug reports about how 
> exec.Command interacts with io.Pipe and SIGKILL/cmd.Kill(). I've seen 
> comments from Ian that some problems can't be solved and a few regrets about 
> the current design but nothing that describes a working approach.
>
> What I want is a function that does this:
>
> ctx, cancelCmd := context.WithCancel(context.Background())
> cmd := exec.CommandContext(ctx, cmdName, cmdArgs...)
> cmd.Stdin = stdinPipe
> cmd.Stdout = stdoutPipe
> cmd.Stderr = stderrPipe
> if err := cmd.Run(); err != nil {
>
> g.log.Warnf("Run error: %w", err)
>
> }
> return nil
>
> and that returns when the command exits, gets an (externally issued) SIGKILL, 
> or gets a cancelCmd(). Every combination I've tried hangs because the pipes 
> aren't closed. Although the pipes are monitored and drained (not shown), that 
> doesn't help because the command doesn't get a chance to close them.
>
> What am I missing? Do I need a goroutine to watch the process table?

First I'll ask what is keeping the pipes open?  if the child process
is killed, the child's copy of the pipes will be closed.  Is the child
passing them along to some other process?  Or is the parent process
keeping them open?  Can either of those be changed?

That said, Go 1.20 added new Cancel and WaitDelay fields to
os/exec.Cmd.  These fields may be used to control how the parent
process should behave when waiting for a child with open pipes.

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/CAOyqgcW%2B3-f8edProCsXSy0_ziYxV5bki%3Dsb5_eUHbF1SGJ2-w%40mail.gmail.com.

Reply via email to