Skip to content

Commit 87cc458

Browse files
a-ayeshayesh-ahmed
andauthored
Add error message when terminating the MuJoCo renderer without calling env.close (#1283)
Co-authored-by: Ayesh Ahmad <ayesh.ahmed@cowlar.com>
1 parent 13d5ecc commit 87cc458

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

gymnasium/envs/mujoco/mujoco_rendering.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import mujoco
88
import numpy as np
99

10+
from gymnasium.logger import warn
11+
1012

1113
def _import_egl(width, height):
1214
from mujoco.egl import GLContext
@@ -356,11 +358,22 @@ def make_context_current(self):
356358
glfw.make_context_current(self.window)
357359

358360
def free(self):
359-
if self.window:
360-
if glfw.get_current_context() == self.window:
361-
glfw.make_context_current(None)
362-
glfw.destroy_window(self.window)
363-
self.window = None
361+
"""
362+
Safely frees the OpenGL context and destroys the GLFW window,
363+
handling potential issues during interpreter shutdown or resource cleanup.
364+
"""
365+
try:
366+
if self.window:
367+
if glfw.get_current_context() == self.window:
368+
glfw.make_context_current(None)
369+
glfw.destroy_window(self.window)
370+
self.window = None
371+
except AttributeError:
372+
# Handle cases where attributes are missing due to improper environment closure
373+
warn(
374+
"Environment was not properly closed using 'env.close()'. Please ensure to close the environment explicitly. "
375+
"GLFW module or dependencies are unloaded. Window cleanup might not have completed."
376+
)
364377

365378
def __del__(self):
366379
"""Eliminate all of the OpenGL glfw contexts and windows"""

0 commit comments

Comments
 (0)