From 1dbd5c247f2bb726522c29700a1a98d44cec2246 Mon Sep 17 00:00:00 2001 From: Ayesh Ahmad Date: Sat, 28 Dec 2024 21:29:18 +0500 Subject: [PATCH 1/5] Add docs and graceful Mujoco Shutdown ( #1224) --- gymnasium/envs/mujoco/mujoco_rendering.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/gymnasium/envs/mujoco/mujoco_rendering.py b/gymnasium/envs/mujoco/mujoco_rendering.py index d6b888103..17c4b582b 100644 --- a/gymnasium/envs/mujoco/mujoco_rendering.py +++ b/gymnasium/envs/mujoco/mujoco_rendering.py @@ -356,11 +356,19 @@ def make_context_current(self): glfw.make_context_current(self.window) def free(self): - if self.window: - if glfw.get_current_context() == self.window: - glfw.make_context_current(None) - glfw.destroy_window(self.window) - self.window = None + """ + Safely frees the OpenGL context and destroys the GLFW window, + handling potential issues during interpreter shutdown or resource cleanup. + """ + try: + if self.window: + if glfw.get_current_context() == self.window: + glfw.make_context_current(None) + glfw.destroy_window(self.window) + self.window = None + except AttributeError: + # Handle cases where attributes are missing due to improper environment closure + print("Warning: Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. GLFW module or dependencies are unloaded. Window cleanup might not have completed.") def __del__(self): """Eliminate all of the OpenGL glfw contexts and windows""" From 65674f3e99d79288b758e8d38556db5325371ca2 Mon Sep 17 00:00:00 2001 From: Ayesh Ahmad Date: Sat, 28 Dec 2024 21:46:42 +0500 Subject: [PATCH 2/5] Fix print statement line character limit --- gymnasium/envs/mujoco/mujoco_rendering.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gymnasium/envs/mujoco/mujoco_rendering.py b/gymnasium/envs/mujoco/mujoco_rendering.py index 17c4b582b..f471f8622 100644 --- a/gymnasium/envs/mujoco/mujoco_rendering.py +++ b/gymnasium/envs/mujoco/mujoco_rendering.py @@ -368,7 +368,8 @@ def free(self): self.window = None except AttributeError: # Handle cases where attributes are missing due to improper environment closure - print("Warning: Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. GLFW module or dependencies are unloaded. Window cleanup might not have completed.") + print("""Warning: Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. + GLFW module or dependencies are unloaded. Window cleanup might not have completed.""") def __del__(self): """Eliminate all of the OpenGL glfw contexts and windows""" From b8f7d344df7004c917a9a514070b4c4a9d487e0e Mon Sep 17 00:00:00 2001 From: Ayesh Ahmad Date: Tue, 31 Dec 2024 23:23:47 +0500 Subject: [PATCH 3/5] Improve warning logging --- gymnasium/envs/mujoco/mujoco_rendering.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gymnasium/envs/mujoco/mujoco_rendering.py b/gymnasium/envs/mujoco/mujoco_rendering.py index f471f8622..5f2f12b21 100644 --- a/gymnasium/envs/mujoco/mujoco_rendering.py +++ b/gymnasium/envs/mujoco/mujoco_rendering.py @@ -1,12 +1,13 @@ import os import time from typing import Dict, Optional - import glfw import imageio import mujoco import numpy as np +from gymnasium.logger import warn + def _import_egl(width, height): from mujoco.egl import GLContext @@ -366,10 +367,12 @@ def free(self): glfw.make_context_current(None) glfw.destroy_window(self.window) self.window = None - except AttributeError: + except AttributeError as e: # Handle cases where attributes are missing due to improper environment closure - print("""Warning: Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. - GLFW module or dependencies are unloaded. Window cleanup might not have completed.""") + warn( + "Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. " + "GLFW module or dependencies are unloaded. Window cleanup might not have completed." + ) def __del__(self): """Eliminate all of the OpenGL glfw contexts and windows""" From 7c4042aad92d9a43b02fdad1851c1f7aa71560cf Mon Sep 17 00:00:00 2001 From: Ayesh Ahmad Date: Tue, 31 Dec 2024 23:25:27 +0500 Subject: [PATCH 4/5] Remove 'as e' in exception --- gymnasium/envs/mujoco/mujoco_rendering.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gymnasium/envs/mujoco/mujoco_rendering.py b/gymnasium/envs/mujoco/mujoco_rendering.py index 5f2f12b21..f1a721290 100644 --- a/gymnasium/envs/mujoco/mujoco_rendering.py +++ b/gymnasium/envs/mujoco/mujoco_rendering.py @@ -367,7 +367,7 @@ def free(self): glfw.make_context_current(None) glfw.destroy_window(self.window) self.window = None - except AttributeError as e: + except AttributeError: # Handle cases where attributes are missing due to improper environment closure warn( "Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. " From 2855173ccb5c9a5d52a6c13636565a47c4f8cc3c Mon Sep 17 00:00:00 2001 From: Ayesh Ahmad Date: Sat, 4 Jan 2025 02:30:16 +0500 Subject: [PATCH 5/5] Improve code formatting --- gymnasium/envs/mujoco/mujoco_rendering.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gymnasium/envs/mujoco/mujoco_rendering.py b/gymnasium/envs/mujoco/mujoco_rendering.py index f1a721290..7427a1499 100644 --- a/gymnasium/envs/mujoco/mujoco_rendering.py +++ b/gymnasium/envs/mujoco/mujoco_rendering.py @@ -1,6 +1,7 @@ import os import time from typing import Dict, Optional + import glfw import imageio import mujoco @@ -358,7 +359,7 @@ def make_context_current(self): def free(self): """ - Safely frees the OpenGL context and destroys the GLFW window, + Safely frees the OpenGL context and destroys the GLFW window, handling potential issues during interpreter shutdown or resource cleanup. """ try: @@ -370,8 +371,8 @@ def free(self): except AttributeError: # Handle cases where attributes are missing due to improper environment closure warn( - "Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. " - "GLFW module or dependencies are unloaded. Window cleanup might not have completed." + "Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. " + "GLFW module or dependencies are unloaded. Window cleanup might not have completed." ) def __del__(self):