Skip to content

Commit e465e25

Browse files
fm_agent: Wait for fastmodel subprocess to terminate when resetting or terminating it
Releasing the model via IRIS does not reliably kill the subprocess This gives the FVP up to 3 seconds to terminate gracefully, then force kills it Signed-off-by: Chris Swinchatt <[email protected]>
1 parent 8cf0a18 commit e465e25

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

fm_agent/fm_agent.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
limitations under the License.
1717
"""
1818

19+
import atexit
1920
import multiprocessing
20-
import sys
2121
import os
2222
from subprocess import Popen
2323
import time
@@ -86,10 +86,12 @@ def __init__(self, model_name=None, model_config=None, logger=None, enable_gdbse
8686
@param model_config is the config file to the fast model
8787
"""
8888

89+
atexit.register(self.__del__)
90+
8991
self.fastmodel_name = model_name
9092
self.config_name = model_config
9193
self.enable_gdbserver = enable_gdbserver
92-
self.subprocess = None
94+
self.subprocess:Popen = None
9395

9496
#If logging not provided, use default log
9597
if logger:
@@ -111,9 +113,10 @@ def __init__(self, model_name=None, model_config=None, logger=None, enable_gdbse
111113
pass
112114

113115
def __del__(self):
114-
if isinstance(self.subprocess, Popen):
115-
self.subprocess.terminate()
116-
self.subprocess.wait()
116+
atexit.unregister(self.__del__)
117+
self.__closeConnection()
118+
self.__releaseModel()
119+
self.__terminateSubprocess()
117120

118121
def setup_simulator(self, model_name, model_config):
119122
""" setup the simulator, this is crucial before you can start a simulator.
@@ -257,8 +260,8 @@ def reset_simulator(self):
257260
if self.is_simulator_alive():
258261
self.logger.prn_wrn("STOP and RESTART FastModel")
259262
self.__closeConnection()
260-
self.model.release(shutdown=True)
261-
time.sleep(1)
263+
self.__releaseModel()
264+
self.__terminateSubprocess()
262265

263266
self.__spawn_simulator()
264267

@@ -336,8 +339,6 @@ def __closeConnection(self):
336339
self.socket.close()
337340
self.logger.prn_inf("Closing terminal socket connection")
338341
self.socket = None
339-
else:
340-
self.logger.prn_inf("Terminal socket connection already closed")
341342

342343
def __run_to_breakpoint(self):
343344
try:
@@ -425,12 +426,26 @@ def shutdown_simulator(self):
425426
self.__CodeCoverage()
426427
self.logger.prn_inf("Fast-Model agent shutting down model")
427428
self.__closeConnection()
428-
self.model.release(shutdown=True)
429-
self.model=None
430-
time.sleep(1)
429+
self.__releaseModel()
430+
self.__terminateSubprocess()
431431
else:
432432
self.logger.prn_inf("Model already shutdown")
433433

434+
def __releaseModel(self):
435+
if self.model:
436+
self.model.release(shutdown=True)
437+
del self.model
438+
self.model = None
439+
440+
def __terminateSubprocess(self):
441+
if self.subprocess:
442+
self.subprocess.terminate()
443+
if self.subprocess.wait(3) is None:
444+
self.subprocess.kill()
445+
self.subprocess.wait()
446+
del self.subprocess
447+
self.subprocess = None
448+
434449
def list_avaliable_models(self):
435450
""" return a dictionary of models and configs """
436451
return self.configuration.get_all_configs()

0 commit comments

Comments
 (0)