Skip to content

Kill Agent child processes #3557

@mssalvatore

Description

@mssalvatore

Description

The Agent uses multiprocessing to run plugins. It's possible that a plugin could hang indefinitely, preventing the parent (Agent) process from ever fully shutting down. Add some logic to main.py to attempt to forcefully kill any child processes still running. Watch out for the resource tracker.

Pseudocode

Be sure to add debug logging to the example below.

diff --git a/monkey/infection_monkey/main.py b/monkey/infection_monkey/main.py
index 9cf58b505..b12b84e44 100644
--- a/monkey/infection_monkey/main.py
+++ b/monkey/infection_monkey/main.py
@@ -179,6 +179,20 @@ def _run_agent(
         logger.exception(
             "Exception thrown from monkey's cleanup function: More info: {}".format(err)
         )
+    finally:
+        import psutil
+
+        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
+
+            p.kill()
 
 
 if "__main__" == __name__:

Tasks

Test Plugin

Mock1-exploiter.tar.gz

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions