Skip to content

Commit 3520cce

Browse files
Jonathan Harpervincentpierre
Jonathan Harper
authored andcommitted
Shorten timeout duration for environment close (#3679)
The timeout duration for closing an environment was set to the same duration as the timeout when waiting for a response from the still-running environment. This led to long waits for the error response when communication version wasn't matching. This change forces a timeout duration of 0 when handling errors.
1 parent 384bb1f commit 3520cce

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- Raise the wall in CrawlerStatic scene to prevent Agent from falling off. (#3650)
1111
- Fixed an issue where specifying `vis_encode_type` was required only for SAC. (#3677)
1212
- Fixed an issue where switching models using `SetModel()` during training would use an excessive amount of memory. (#3664)
13+
- Environment subprocesses now close immediately on timeout or wrong API version. (#3679)
1314

1415

1516
## [0.15.0-preview] - 2020-03-18

ml-agents-envs/mlagents_envs/environment.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ def __init__(
142142
aca_output = self.send_academy_parameters(rl_init_parameters_in)
143143
aca_params = aca_output.rl_initialization_output
144144
except UnityTimeOutException:
145-
self._close()
145+
self._close(0)
146146
raise
147147

148148
unity_communicator_version = aca_params.communication_version
149149
if unity_communicator_version != UnityEnvironment.API_VERSION:
150-
self._close()
150+
self._close(0)
151151
raise UnityEnvironmentException(
152152
f"The communication API version is not compatible between Unity and python. "
153153
f"Python API: {UnityEnvironment.API_VERSION}, Unity API: {unity_communicator_version}.\n "
@@ -228,7 +228,7 @@ def validate_environment_path(env_path: str) -> Optional[str]:
228228
def executable_launcher(self, file_name, docker_training, no_graphics, args):
229229
launch_string = self.validate_environment_path(file_name)
230230
if launch_string is None:
231-
self._close()
231+
self._close(0)
232232
raise UnityEnvironmentException(
233233
f"Couldn't launch the {file_name} environment. Provided filename does not match any environments."
234234
)
@@ -433,13 +433,21 @@ def close(self):
433433
else:
434434
raise UnityEnvironmentException("No Unity environment is loaded.")
435435

436-
def _close(self):
436+
def _close(self, timeout: Optional[int] = None) -> None:
437+
"""
438+
Close the communicator and environment subprocess (if necessary).
439+
440+
:int timeout: [Optional] Number of seconds to wait for the environment to shut down before
441+
force-killing it. Defaults to `self.timeout_wait`.
442+
"""
443+
if timeout is None:
444+
timeout = self.timeout_wait
437445
self._loaded = False
438446
self.communicator.close()
439447
if self.proc1 is not None:
440448
# Wait a bit for the process to shutdown, but kill it if it takes too long
441449
try:
442-
self.proc1.wait(timeout=self.timeout_wait)
450+
self.proc1.wait(timeout=timeout)
443451
signal_name = self.returncode_to_signal_name(self.proc1.returncode)
444452
signal_name = f" ({signal_name})" if signal_name else ""
445453
return_info = f"Environment shut down with return code {self.proc1.returncode}{signal_name}."

0 commit comments

Comments
 (0)