Skip to content

NSWindow main thread error when trying to setup Gymnasium (openapi gym) #257

Open

Description

@eoliphan

Hi, im trying to run some simple gymnasium stuff and getting the following error:

(require-python '[gymnasium :as gym])
(def env (gym/make "LunarLander-v2" :render_mode "human"))

; error (Temurin java 17) or crash (correto java 17)
(py. env reset :seed 42)

Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:707).
Traceback (most recent call last):
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/time_limit.py", line 75, in reset
    return self.env.reset(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/order_enforcing.py", line 61, in reset
    return self.env.reset(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/env_checker.py", line 57, in reset
    return env_reset_passive_checker(self.env, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/utils/passive_env_checker.py", line 186, in env_reset_passive_checker
    result = env.reset(**kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/box2d/lunar_lander.py", line 461, in reset
    self.render()
  File "/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/box2d/lunar_lander.py", line 699, in render
    self.screen = pygame.display.set_mode((VIEWPORT_W, VIEWPORT_H))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pygame.error: NSWindow should only be instantiated on the main thread!

I'm on OSX Sonoma (silicon), python 3.11.6, Clojure 1.11.1 and java 17. I was actually about to write this as a crash report, but decided to try another Java 17 jvm and was able to get the error output instead.

In any case, is there something one can do with libpython-clj to setup the call such that it's on the main thread?

Activity

  1. eoliphan commented on Oct 25, 2023

    @eoliphan
    Author

    Ok so i missed this #10 (comment) when i was looking through the issues. So i just tried putting the code in a -main and ran it with clojure -m but still seeing the error.

    But based on that comment is this something that even can be addressed on the clj-python side?

  2. cnuernber commented on Oct 25, 2023

    @cnuernber
    Collaborator

    Most likely you have to run in embedded mode. I am surprised clojure -m didn't solve the issue but especially with windowing systems there is a lot going on such as signal handling callbacks that the JVM does when it is the main process that potentially other systems do not work well with.

  3. eoliphan commented on Oct 25, 2023

    @eoliphan
    Author
  4. jjtolton commented on Oct 26, 2023

    @jjtolton
    Contributor

    I think there might be something else going on:
    image

    I was able to run this fine from repl w/o embedded.

    Clojure 1.11.1, Java 17.0.9, Ubuntu 22. I'm not sure if the silicon has something to do with it or could be local install issues.

  5. jjtolton commented on Oct 26, 2023

    @jjtolton
    Contributor

    @eoliphan did you try the equivalent code in Python? I tend to find that's a pretty good troubleshooting step for some of these issues.

    Make sure this works out for you from the directory where you are running your code:

    env/bin/python <(cat << eof 
    import gymnasium as gym
    env = gym.make("LunarLander-v2", render_mode="human")
    env.reset(seed=42)
    eof
    )
    
    # should return 0
    echo "$?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions