After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 762164 - GSubprocess: add API to reliably kill descendant sub-process tree
GSubprocess: add API to reliably kill descendant sub-process tree
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: gsubprocess
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2016-02-16 22:00 UTC by Christian Hergert
Modified: 2018-05-24 18:33 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Christian Hergert 2016-02-16 22:00:13 UTC
When spawning a process using GSubprocess or GSubprocessLauncher, it would be convenient if we could reliably kill all of the descendant processes. One example would be to spawn `make', and then call g_subprocess_force_exit() on the make subprocess. It will not actually stop the `make' processes running, because they are all descendants of the make that was spawned.

One way to work around this, at least on Linux (and probably BSD), is to call `setsid(3p)` in the child setup function, and then kill the process tree with kill(-pid, SIGKILL). However there are side effects to this, like the children continuing to run when the parent exits. Even prctl for PR_SET_PDEATHSIG isn't enough here as it would only kill the session leader.

So, to avoid having to discover the "right" way to do this in every application, I think it makes sense to add API for this to GSubprocessLauncher.

On Linux, it might use cgroups. On BSD, we might be stuck with setsid() and kill(-pid, SIGKILL), and who knows on Windows.

But I'd love some feedback and possible API suggestions. I'd be willing to implement it if need be.
Comment 1 Colin Walters 2016-02-17 14:20:48 UTC
Really you want to use full containers (pid namespaces, etc.)

That said I bet one could implement this today with using PR_SET_CHILD_SUBREAPER - if the first child dies, loop and kill all children that get reparented to you, then exit after a few seconds of idle (yes racy, but hard to fix without pid namespaces or cgroup access).
Comment 2 Allison Karlitskaya (desrt) 2016-02-17 14:25:36 UTC
I agree that this would be a pretty nice feature, but I'm not sure about the implementation.

As far as I know, it is not possible for non-root to change the cgroup of a process on Linux without going through some kind of intermediary via (for example) D-Bus.  I'm not really interested in doing that...

There is also no way to kill all processes in a cgroup, from what I understand.  You rather have to list the processes that are in a cgroup and kill them one at a time, hoping that they don't fork() in the meantime.  A common trick (again, as I understand it) is to freeze the processes by using the CPU cgroup to ensure that they get scheduled on no CPUs, but again, I don't think non-root can do this.

Does anyone know more about this?
Comment 3 Michael Catanzaro 2016-05-13 13:27:37 UTC
I hope this API would work reliably *even if the parent process is SIGKILLed*
Comment 4 GNOME Infrastructure Team 2018-05-24 18:33:08 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/1135.