From 02e5ee6cf6f2a1e779f174606e1e8039e49498f9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 10 Jan 2023 14:57:42 -0500 Subject: [PATCH] Agent: Spawn the manager process before acquiring the SystemSingleton The SystemSingleton uses an abstract unix socket as a "lock" to ensure only one agent at a time runs on a given machine. It seems that if a manager process is spawned after this unix socket is created, the manager process inherits this file handle, which leads to the socket never being properly closed. Spawning the manager before the socket is opened is a quick solution to this problem. A better solution (see https://github.com/guardicore/monkey/issues/2817) is to use a different method than a unix socket to achieve this goal, but, baby steps for now. --- monkey/infection_monkey/monkey.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index 57878e9df5d..85ef80ed425 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -101,11 +101,14 @@ def __init__(self, args, ipc_logger_queue: multiprocessing.Queue, log_path: Path logger.info(f"Agent ID: {self._agent_id}") logger.info(f"Process ID: {os.getpid()}") + # Spawn the manager before the acquiring the singleton in case the file handle gets copied + # over to the manager process + self._manager = multiprocessing.get_context("spawn").Manager() + self._singleton = SystemSingleton() self._opts = self._get_arguments(args) self._ipc_logger_queue = ipc_logger_queue - self._manager = multiprocessing.get_context("spawn").Manager() self._agent_event_forwarder = None self._agent_event_queue = self._setup_agent_event_queue()