Skip to content

Commit

Permalink
Agent: Kill hung child processes on exit
Browse files Browse the repository at this point in the history
Plugins that hang can prevent the agent from closing. On agent shutdown,
we'll forcefully kill all child processes that have not shutdown on
their own.

Issue #3557
  • Loading branch information
mssalvatore committed Aug 14, 2023
1 parent 1609f55 commit 57aca1d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions monkey/infection_monkey/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from pathlib import Path
from typing import Sequence, Tuple, Union

import psutil

# dummy import for pyinstaller
# noinspection PyUnresolvedReferences
from common.common_consts import AGENT_OTP_ENVIRONMENT_VARIABLE
Expand Down Expand Up @@ -179,6 +181,22 @@ def _run_agent(
logger.exception(
"Exception thrown from monkey's cleanup function: More info: {}".format(err)
)
finally:
_kill_hung_child_processes(logger)


def _kill_hung_child_processes(logger: logging.Logger):
for p in psutil.Process().children(recursive=True):
if "multiprocessing.resource_tracker" in p.cmdline()[2]:
# This process will clean itself up, but no other processes should be running at
# this time.
continue

logger.warning(
"Killing hung child process: "
f"pid={p.pid}, name={p.name()}, status={p.status()}, cmdline={p.cmdline()}"
)
p.kill()


if "__main__" == __name__:
Expand Down

0 comments on commit 57aca1d

Please sign in to comment.