Farama-Foundation / Gymnasium

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
https://gymnasium.farama.org
MIT License
7.26k stars 805 forks source link

Mujoco Rendering Error when render_mode='human' #325

Closed jospierce closed 1 year ago

jospierce commented 1 year ago

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

RedTachyon commented 1 year ago

It's most likely a problem with your local installation, it works for me with a nearly identical system, so check if you have all the dependencies installed, especially some problem seems to be present around glfw

pseudo-rnd-thoughts commented 1 year ago

@RedTachyon I can replicate the issue just with a different error.

Exception ignored in: <function WindowViewer.__del__ at 0x105ffa940>
Traceback (most recent call last):
  File "/Users/marktowers/Documents/farama/Gymnasium/gymnasium/envs/mujoco/mujoco_rendering.py", line 337, in __del__
  File "/Users/marktowers/Documents/farama/Gymnasium/gymnasium/envs/mujoco/mujoco_rendering.py", line 332, in free
AttributeError: 'NoneType' object has no attribute 'destroy_window'

FYI, to speed this up just set the range(20)

Adding a print statement to close() then it seems it seems to work fine but when env.__del__ is called which calls env.close() again then glfw is None causing the error above.

@rodrigodelazcano Do you know how to solve this?

jospierce commented 1 year ago

Thanks for the fast responses. More info, I am running glfw version 2.5.6 which appears to be old. I think it installed as the default in the set-up.

rodrigodelazcano commented 1 year ago

I can't replicate the error, does this happen when calling env.close()?. @jospierce I'm also using version 2.5.6 of glfw so that shouldn't be the problem.

The only difference from @jospierce 's system is that I'm running Linux. @pseudo-rnd-thoughts are you also running this on MacOS?

RedTachyon commented 1 year ago

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

pseudo-rnd-thoughts commented 1 year ago

The might the easiest solution is to check that glfw is not none then still run the function as is

Yes, I think that we can only replicate this on macos currently.

jospierce commented 1 year ago

@rodrigodelazcano Appreciate the glfw version confirm. The error happens before the env.close().

RedTachyon commented 1 year ago

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.

jospierce commented 1 year ago

@RedTachyon here is the error with env.close() commented out of the code

Exception ignored in: <function WindowViewer.del at 0x12ca33be0> 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'

Not sure if this is relevant to this situation but I get this error as well in all cases. I think it is something else you guys are already are aware of. /Users/joe/opt/anaconda3/envs/Gymnasium/lib/python3.10/site-packages/gymnasium/envs/registration.py:521: UserWarning: WARN: Overriding environment GymV26Environment-v0 already in registry. logger.warn(f"Overriding environment {new_spec.id} already in registry.") /Users/joe/opt/anaconda3/envs/Gymnasium/lib/python3.10/site-packages/gymnasium/envs/registration.py:521: UserWarning: WARN: Overriding environment GymV22Environment-v0 already in registry. logger.warn(f"Overriding environment {new_spec.id} already in registry.")

pseudo-rnd-thoughts commented 1 year ago

Yes, we are aware and going to fix the second issue

yhogewind commented 1 year ago

Yes, I think that we can only replicate this on macos currently.

Also happens for me on Ubuntu 22.04 and it does seem like this happens when the environment is cleaned up during interpreter shutdown. I'm currently checking "if glfw" in WindowViewer's free method as a workaround.

Globals like imported modules (glfw) might already be deleted during shutdown, so in general you cannot assume they exist in __del__ (docs) .

pseudo-rnd-thoughts commented 1 year ago

@rodrigodelazcano Do you know if we fixed this issue? There doesn't seem to be a pr linked to the issue so I'm guessing not. If so, could you make a PR to just remove the __del__ case and move any necessary code to the close function

jospierce commented 1 year ago

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

muchvo commented 1 year ago

@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.

pseudo-rnd-thoughts commented 1 year ago

@muchvo Hi, if you find a solution could you make a PR for it?

muchvo commented 1 year ago

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

Kallinteris-Andreas commented 1 year ago

@pseudo-rnd-thoughts close, has been fixed