Skip to content

Commit

Permalink
Add error message when terminating the MuJoCo renderer without callin…
Browse files Browse the repository at this point in the history
…g `env.close` (#1283)

Co-authored-by: Ayesh Ahmad <[email protected]>
  • Loading branch information
a-ayesh and ayesh-ahmed authored Jan 4, 2025
1 parent 13d5ecc commit 87cc458
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions gymnasium/envs/mujoco/mujoco_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import mujoco
import numpy as np

from gymnasium.logger import warn


def _import_egl(width, height):
from mujoco.egl import GLContext
Expand Down Expand Up @@ -356,11 +358,22 @@ 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
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"""
Expand Down

0 comments on commit 87cc458

Please sign in to comment.