Killing an exec subprocess

1,275 views
Skip to first unread message

Brad Fitzpatrick

unread,
Jan 25, 2011, 10:33:53 AM1/25/11
to golang-dev
I need to be able to kill a process started by the 'exec' package.  (if it misbehaves)

The closest thing I see is syscall.Tgkill which isn't quite right.  Am I missing something else?

Should we add a portable method to exec.Cmd?  I don't know Windows and its equivalent to signals and whether they're catchable / etc.  Any API proposals?  Simplest is just a niladic Kill that sends a SIGKILL.  We could add other signals (of the catchable variety) if needed in the future?

- Brad

Russ Cox

unread,
Jan 25, 2011, 10:36:28 AM1/25/11
to Brad Fitzpatrick, golang-dev
syscall.Kill?

That isn't good enough for Windows
but that's a separate issue. (We need
to have some kind of Process abstraction
to make Wait etc work on Windows.)

Russ

Adam Langley

unread,
Jan 25, 2011, 10:53:07 AM1/25/11
to r...@golang.org, Brad Fitzpatrick, golang-dev
On Tue, Jan 25, 2011 at 10:36 AM, Russ Cox <r...@golang.org> wrote:
> syscall.Kill?

It's worse than you think (it usually is).

If the child may have children if its own, then KILLing the parent
might not stop them. You have to create a new process group (setpgrp)
after you fork and KILL the whole group (with kill(2) and a negative
PID argument).

Also, signal(7) says that the default disposition of SIGCHLD is
Ignore. However, POSIX 2001 says that you cannot wait on children if
you ignore SIGCHLD. And, if you don't wait on children then you'll end
up with zombies. Also, there's no timed wait/waitpid, you have to use
NOHANG in a loop. Also, you can race the kernel between seeing EOF on
a child's fd, or getting SIGCHLD, and calling waitpid with NOHANG.

Different UNIXs may, of course, differ. Even assuming that all the
above is correct!


AGL

Brad Fitzpatrick

unread,
Jan 25, 2011, 12:01:13 PM1/25/11
to r...@golang.org, golang-dev
On Tue, Jan 25, 2011 at 7:36 AM, Russ Cox <r...@golang.org> wrote:
syscall.Kill?

Wow, I'm not sure how I missed that.

I'll use that for now until there's a more portable way.

Rob 'Commander' Pike

unread,
Jan 25, 2011, 1:29:13 PM1/25/11
to Adam Langley, r...@golang.org, Brad Fitzpatrick, golang-dev
I remember when it was possible to understand Unix.

-rob

brainman

unread,
Jan 25, 2011, 5:32:34 PM1/25/11
to golang-dev
I also have some problems with current os.ForkExec implementation
failing on win2000. I was planning to make os.ForkExec() (or maybe new
os.CreateProcess()) return an *os.Process (instead of pid int), that
implements Wait(), Close() and Kill(). We could also have
os.FindProcess(pid int) return *os.Process to get hold of already
running process. This should fit well in Windows land.

Alex
Reply all
Reply to author
Forward
0 new messages