Thread#kill doesn't kill processes inside a thread

[This was first issued in ruby-de]

One thread uses system to run one (slow) program. The main thread kills
it but the forked program stays around after the script finishes.

Should Thread#kill not only mark the thread as dead but also kill its
children processes? Shouldn’t there be some better way to kill children
than taking the pid and using Process.kill?

The only reference I could find was [ruby-talk:64895].

···


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

MSDOS didn’t get as bad as it is overnight – it took over ten years
of careful development.
– dmeggins@aix1.uottawa.ca

Hi,

Should Thread#kill not only mark the thread as dead but also kill its
children processes? Shouldn’t there be some better way to kill children
than taking the pid and using Process.kill?

No. Threads are entities in the process, so it does not maintain
“child” processes. Child processes are children of the parent
“processes” not threads.

If you want to kill child processes when the thread is down,

th = Thread.new{
Thread.current[:children] =
begin
… add child processes to Thread.current[:children]
ensure
Process.kill(“TERM”, *Thread.current[:children])
end
}

is the way to go.

						matz.
···

In message “Thread#kill doesn’t kill processes inside a thread” on 03/05/15, Mauricio Fernández batsman.geo@yahoo.com writes: