[Kimchi-devel] [PATCH] Github bug #326: run_command: killing all children processes

Aline Manera alinefm at linux.vnet.ibm.com
Mon Feb 24 14:11:09 UTC 2014


Reviewed-by: Aline Manera <alinefm at linux.vnet.ibm.com>

On 02/24/2014 10:55 AM, Daniel Barboza wrote:
> From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
>
> The subprocess.kill() call does not handle well the created children
> of the process created by the Popen() call. In some cases (for
> example, the mount command) the parent processes creates children
> processes that aren't killed, leaving the parent thread hanging out
> in an infinite loop waiting for kill() to finish.
>
> An attempt was made using the os.killpg() call, but to no success.
> Using psutils facilities to kill all the children before killing the
> opened process was easier and clearer than the alternatives (opening
> a new thread and trying to kill the thread, for example).
>
> Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
> ---
>   src/kimchi/utils.py | 9 +++++++++
>   1 file changed, 9 insertions(+)
>
> diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
> index a1410c0..6be1c04 100644
> --- a/src/kimchi/utils.py
> +++ b/src/kimchi/utils.py
> @@ -23,6 +23,7 @@
>
>   import cherrypy
>   import os
> +import psutil
>   import re
>   import subprocess
>   import urllib2
> @@ -137,8 +138,16 @@ def run_command(cmd, timeout=None):
>       timeout is a float number in seconds.
>       timeout default value is None, means command run without timeout.
>       """
> +    # subprocess.kill() can leave descendants running
> +    # and halting the execution. Using psutil to
> +    # get all descendants from the subprocess and
> +    # kill them recursively.
>       def kill_proc(proc, timeout_flag):
>           try:
> +            parent = psutil.Process(proc.pid)
> +            for child in parent.get_children(recursive=True):
> +                child.kill()
> +            # kill the process after no children is left
>               proc.kill()
>           except OSError:
>               pass




More information about the Kimchi-devel mailing list