83
83
PluginSourceExtractor ,
84
84
)
85
85
from infection_monkey .puppet .puppet import Puppet
86
- from infection_monkey .system_singleton import SystemSingleton
87
86
from infection_monkey .utils import agent_process , environment
88
87
from infection_monkey .utils .file_utils import mark_file_for_deletion_on_windows
89
88
from infection_monkey .utils .ids import get_agent_id , get_machine_id
@@ -107,12 +106,9 @@ def __init__(self, args, ipc_logger_queue: multiprocessing.Queue, log_path: Path
107
106
logger .info (f"Agent ID: { self ._agent_id } " )
108
107
logger .info (f"Process ID: { os .getpid ()} " )
109
108
110
- # Spawn the manager before the acquiring the singleton in case the file handle gets copied
111
- # over to the manager process
112
109
context = multiprocessing .get_context ("spawn" )
113
110
self ._manager = context .Manager ()
114
111
115
- self ._singleton = SystemSingleton ()
116
112
self ._opts = self ._get_arguments (args )
117
113
118
114
self ._ipc_logger_queue = ipc_logger_queue
@@ -222,12 +218,10 @@ def start(self):
222
218
self ._agent_event_forwarder .start ()
223
219
self ._plugin_event_forwarder .start ()
224
220
225
- if self ._is_another_monkey_running ():
226
- logger .info ("Another instance of the monkey is already running" )
227
- return
228
-
229
221
logger .info ("Agent is starting..." )
230
222
223
+ # This check must be done after the agent event forwarder is started, otherwise the agent
224
+ # will be unable to send a shutdown event to the Island.
231
225
should_stop = self ._control_channel .should_agent_stop ()
232
226
if should_stop :
233
227
logger .info ("The Monkey Island has instructed this agent to stop" )
@@ -435,9 +429,6 @@ def _subscribe_events(self):
435
429
PropagationEvent , notify_relay_on_propagation (self ._relay )
436
430
)
437
431
438
- def _is_another_monkey_running (self ):
439
- return not self ._singleton .try_lock ()
440
-
441
432
def cleanup (self ):
442
433
logger .info ("Agent cleanup started" )
443
434
deleted = None
@@ -459,7 +450,9 @@ def cleanup(self):
459
450
self ._publish_agent_shutdown_event ()
460
451
461
452
self ._plugin_event_forwarder .flush ()
462
- self ._agent_event_forwarder .flush ()
453
+
454
+ if self ._agent_event_forwarder :
455
+ self ._agent_event_forwarder .flush ()
463
456
464
457
self ._heart .stop ()
465
458
@@ -471,13 +464,10 @@ def cleanup(self):
471
464
InfectionMonkey ._self_delete ()
472
465
finally :
473
466
self ._plugin_event_forwarder .stop ()
474
- self ._agent_event_forwarder .stop ()
467
+ if self ._agent_event_forwarder :
468
+ self ._agent_event_forwarder .stop ()
475
469
self ._delete_plugin_dir ()
476
470
self ._manager .shutdown ()
477
- try :
478
- self ._singleton .unlock ()
479
- except AssertionError as err :
480
- logger .warning (f"Failed to release the singleton: { err } " )
481
471
482
472
logger .info ("Agent is shutting down" )
483
473
0 commit comments