Kill the child processes spawned by a run command

When the timeout occurs it is important to ensure clean up of child
processes. Killing only the direct process created by a command can
leave child processes running.

For example a pmbootstrap.py install will run apk add. This run command
creates multiple processes as follows:
(cmd line arguments snipped for readability)

  $ ps -e -o pid,ppid,pgid,cmd
  PID  PPID  PGID CMD
  31738 23247 31738 python3 ./pmbootstrap.py -t 15 install --no-fde
  31746 31738 31738 sudo env -i /bin/sh -c ... ;apk --no-progress add
  31747 31746 31738 /bin/sh -c ... ;apk --no-progress add
  31748 31747 31738 apk --no-progress add

The root process of the run command is PID 31746. We want to kill
the child processes too. Otherwise only running kill -9 31746 will leave
the processes 31747 and 31748 running.
This commit is contained in:
Robert Yang 2018-09-24 21:33:38 -04:00 committed by Oliver Smith
parent 3e7c95e8b4
commit 277854e80f
3 changed files with 66 additions and 6 deletions

View file

@ -93,6 +93,46 @@ def pipe_read(args, process, output_to_stdout=False, output_return=False,
return
def kill_process_tree(args, pid, ppids, kill_as_root):
"""
Recursively kill a pid and its child processes
:param pid: process id that will be killed
:param ppids: list of process id and parent process id tuples (pid, ppid)
:param kill_as_root: use sudo to kill the process
"""
if kill_as_root:
pmb.helpers.run.root(args, ["kill", "-9", str(pid)],
check=False)
else:
pmb.helpers.run.user(args, ["kill", "-9", str(pid)],
check=False)
for (child_pid, child_ppid) in ppids:
if child_ppid == str(pid):
kill_process_tree(args, child_pid, ppids, kill_as_root)
def kill_command(args, pid, kill_as_root):
"""
Kill a command process and recursively kill its child processes
:param pid: process id that will be killed
:param kill_as_root: use sudo to kill the process
"""
cmd = ["ps", "-e", "-o", "pid=,ppid=", "--noheaders"]
ret = subprocess.run(cmd, check=True, stdout=subprocess.PIPE)
ppids = []
proc_entries = ret.stdout.decode("utf-8").rstrip().split('\n')
for row in proc_entries:
items = row.split()
if len(items) != 2:
raise RuntimeError("Unexpected ps output: " + row)
ppids.append(items)
kill_process_tree(args, pid, ppids, kill_as_root)
def foreground_pipe(args, cmd, working_dir=None, output_to_stdout=False,
output_return=False, output_timeout=True,
kill_as_root=False):
@ -141,11 +181,7 @@ def foreground_pipe(args, cmd, working_dir=None, output_to_stdout=False,
str(args.timeout) + " seconds. Killing it.")
logging.info("NOTE: The timeout can be increased with"
" 'pmbootstrap -t'.")
if kill_as_root:
pmb.helpers.run.root(args, ["kill", "-9",
str(process.pid)])
else:
process.kill()
kill_command(args, process.pid, kill_as_root)
continue
# Read all currently available output