diff --git a/model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py b/model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py index cbbbe39..784420d 100644 --- a/model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py +++ b/model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py @@ -1,8 +1,11 @@ -import numpy as np +from contextlib import suppress +from subprocess import TimeoutExpired + import gymnasium as gym -from gymnasium import spaces import messages_pb2 as pb import ns3ai_gym_msg_py as py_binding +import numpy as np +from gymnasium import spaces from ns3ai_utils import Experiment @@ -306,6 +309,8 @@ def reset(self, seed=None, options=None): if not self.gameOver: self.rx_env_state() self.send_close_command() + with suppress(TimeoutExpired): + self.exp.proc.wait(2) self.msgInterface = None self.newStateRx = False @@ -332,6 +337,12 @@ def get_random_action(self): return act def close(self): + if not self.gameOver: + self.rx_env_state() + self.send_close_command() + with suppress(TimeoutExpired): + self.exp.proc.wait(2) + # environment is not needed anymore, so kill subprocess in a straightforward way self.exp.kill() # destroy the message interface and its shared memory segment