Gymnasium icon indicating copy to clipboard operation
Gymnasium copied to clipboard

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

Open yash-jhaveri opened this issue 1 year ago • 7 comments

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

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

yash-jhaveri avatar Jun 07 '23 20:06 yash-jhaveri

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

pseudo-rnd-thoughts avatar Jun 16 '23 10:06 pseudo-rnd-thoughts

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()

mariovas3 avatar Jul 02 '23 11:07 mariovas3

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

pseudo-rnd-thoughts avatar Jul 03 '23 09:07 pseudo-rnd-thoughts

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

  • Platform - Ubuntu 20.04
  • Interpreter - Python 3.8.10
  • Packages (venv): gymnasium==0.28.1 box2d-py==2.3.5 pygame==2.1.3 ipykernel==6.23.2 ipython==8.12.2 jupyter_client==8.2.0 jupyter_core==5.3.1

mariovas3 avatar Jul 03 '23 12:07 mariovas3

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

pseudo-rnd-thoughts avatar Jul 03 '23 13:07 pseudo-rnd-thoughts

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

mariovas3 avatar Jul 03 '23 13:07 mariovas3

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!

RogerJL avatar Jun 01 '24 18:06 RogerJL