Gymnasium: Mujoco Rendering Error when render_mode='human'

Describe the bug

This error occurs when rendering a mujoco environment in render_mode=‘human’. render_mode='rgb_array" works fine. Thanks for the help.

Exception ignored in: <function WindowViewer.del at 0x122863be0> Traceback (most recent call last): File “/Users/joe/opt/anaconda3/envs/Gymnasium/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py”, line 337, in del File “/Users/joe/opt/anaconda3/envs/Gymnasium/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py”, line 330, in free AttributeError: ‘NoneType’ object has no attribute ‘get_current_context’

Code example

import gymnasium as gym

env = gym.make('Ant-v4', render_mode="human")
observation, info = env.reset()

total_reward=0
for _ in range(1000):
    action = env.action_space.sample()  # agent policy that uses the observation and info
    observation, reward, terminated, truncated, info = env.step(action)
    total_reward = total_reward + reward
    if terminated or truncated:
        observation, info = env.reset()
        

env.close()
print(f"Total Reward: {total_reward}")

System info

pip install gymnasium version 27.1 MacOS Ventura 13.2 Python 3.10.9

Additional context

No response

Checklist

  • I have checked that there is no similar issue in the repo

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

@pseudo-rnd-thoughts Yes, I will continue to try figuring it out, If I find the solution, I will make a PR for it.

@pseudo-rnd-thoughts Hi, I am encountering the same issue, when developing Safety-Gymnasium according to this issue. This is very strange, because when I test the same code on the gymnasium, it works fine, while in safety-gymnasium, I encounter the same question proposed in this issue.

AttributeError: 'NoneType' object has no attribute 'get_current_context'

Because I am upgrading the version of the gymnasium which is one dependency of safety-gymansium from 0.26.3 to 0.28.1. I notice that there is no __del__ method in Viewer in 0.26.3, but in 0.28.1, the viewer is changed to WindowViewer and __del__ method is added. And __del__ method is also added into OffScreenViewer which is called RenderContextOffscreen before. But when I call render using render_mode="rgb_array" it works fine. I try to figure out why and fix it but fail temporarily These are something that I noticed, hopefully helpful for you to fix it. And any advice is appreciated. Thanks.

Thanks for keeping this in focus. I found a work around that got me past this. I will test on Macos when ready.

The error happens before the env.close().

Are you sure about that? From my testing it seems the error is in the final cleanup, after all the top level code is executed.

Alright, I managed to replicate it, I’m about 90% sure it’s because the env is trying to free up some resources twice, and it doesn’t check if that has already been done (combine this with some weird C++ code, and you get the untraceable error). First it cleans them up at env.close(), and then again at __del__ when the env goes out of scope.

It might be a good choice to just remove __del__ completely from these envs (and possible elsewhere in gymnasium - it only seems to be present here and in vector envs), there was a discussion about adding it everywhere back in gym, but the conclusion was that __del__ eats babies, kidnaps cats and does all sorts of evil things: https://github.com/openai/gym/pull/2897