Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition when resetting environment #115

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down