Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _kill_all_kids(self, sig: int) -> Set[int]:
"""
Kill all subprocesses (and its subprocesses) that executor started.
This function tries to kill all leftovers in process tree that current
executor may have left. It uses environment variable to recognise if
process have origin in this Executor so it does not give 100 % and
some daemons fired by subprocess may still be running.
:param int sig: signal used to stop process run by executor.
:return: process ids (pids) of killed processes
:rtype: set
"""
pids = processes_with_env(ENV_UUID, self._uuid)
for pid in pids:
log.debug("Killing process %d ...", pid)
try:
os.kill(pid, sig)
except OSError as err:
if err.errno in IGNORED_ERROR_CODES:
# the process has died before we tried to kill it.
pass
else:
raise
log.debug("Killed process %d.", pid)
return pids
def cleanup_subprocesses() -> None:
"""On python exit: find possibly running subprocesses and kill them."""
# pylint: disable=redefined-outer-name, reimported, import-outside-toplevel
# atexit functions tends to loose global imports sometimes so reimport
# everything what is needed again here:
import os
import errno
from mirakuru.base_env import processes_with_env
from mirakuru.compat import SIGKILL
# pylint: enable=redefined-outer-name, reimported, import-outside-toplevel
pids = processes_with_env(ENV_UUID, str(os.getpid()))
for pid in pids:
try:
os.kill(pid, SIGKILL)
except OSError as err:
if err.errno != errno.ESRCH:
print("Can not kill the", pid, "leaked process", err)