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
6.25k stars 720 forks source link

[Bug Report] render window freezes when `render_mode="human"` (LunarLander) (MacOS) #543

Open yash-jhaveri opened 1 year ago

yash-jhaveri commented 1 year ago

Describe the bug

After running the example code for gymnasium from https://gymnasium.farama.org/ (displayed below) in a jupyter notebook and the simulation has finished, my cursor turns into a pinwheel when I hover over the pygame window where the render played. The pygame window cannot be closed or minimized. In my Force Quit Applications list (cmd+opt+esc), it shows python (not responding).

This happens with CartPole-v1 and FrozenLake-v1 as well.

Code example

import gymnasium as gym

env = gym.make("LunarLander-v2", render_mode="human")
observation, info = env.reset()

for _ in range(1000):
   action = env.action_space.sample()
   observation, reward, terminated, truncated, info = env.step(action)

   if terminated or truncated:
      observation, info = env.reset()
env.close()

System info

I installed gymnasium today (minutes before running the code above) with pip install gymnasium. When I first tried to run the code, I received an error asking me to 'pip install gymnasium[box2d]', which I did with pip install 'gymnasium[box2d]'. After this, I was able to run the code successfully.

My gymnasium version is 0.28.1.

My OS is macOS Ventura 13.2.1.

My python version is 3.10.

Additional context

No response

Checklist

pseudo-rnd-thoughts commented 1 year ago

Hey, thanks for reporting the issue, I thought we had solved this issue but apparently not. I can repeat the issue on my system (very similar) but don't think this is an issue with gymnasium, rather pygame I have made an issue on pygame-community to see if we can solve this issue - https://github.com/pygame-community/pygame-ce/issues/2256

mariovas3 commented 1 year ago

Hi, I had the same issue, but found that simply adding the pygame.display.quit() command at the end fixes the issue - the kernel doesn't die and the window is closed successfully.

import gymnasium as gym
import pygame

env = gym.make("LunarLander-v2", render_mode="human")
observation, info = env.reset()

for _ in range(1000):
   action = env.action_space.sample()
   observation, reward, terminated, truncated, info = env.step(action)

   if terminated or truncated:
      observation, info = env.reset()
pygame.display.quit()
pseudo-rnd-thoughts commented 1 year ago

@mariovas3 Is this on a mac? Testing locally on a mac, this hasn't changed anything for me

mariovas3 commented 1 year ago

@mariovas3 Is this on a mac? Testing locally on a mac, this hasn't changed anything for me

@pseudo-rnd-thoughts Apologies for not giving the spec. I tested the above code snippet in a VS Code notebook and running as a .py script. Both seemed to work for me. My spec is:

pseudo-rnd-thoughts commented 1 year ago

Thanks for testing @mariovas3 but to my knowledge this a mac specific issue

Is you get the same issue with other os by default, let us know and we can investigate them separately

mariovas3 commented 1 year ago

@pseudo-rnd-thoughts if I don't include the pygame.display.quit() line at the end, my kernel crashes and I get prompted to "force quit" or "wait" despite the fact I'm on Ubuntu (and not mac).

RogerJL commented 1 month ago

HumanRenderer.close() does both

pygame.display.quit()
pygame.quit()

so by closing the environment pygame should be terminated correctly - there should be no need for a direct call to pygame.display.quit()

If a LunarLander builtin human renderer is used then its close method should call these methods, and it does!