Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition when initialzing p5 #5

Open
JohnEdChristensen opened this issue Feb 1, 2024 · 0 comments
Open

Race condition when initialzing p5 #5

JohnEdChristensen opened this issue Feb 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@JohnEdChristensen
Copy link
Contributor

I ran into an error when initialzing p5 with Sketch()
pyodide.ffi.JsException: TypeError: defaultCanvas0.canvas is undefined

from proceso import Sketch

p5 = Sketch()

It comes from binding.py

    def __init__(self, id: str = "defaultCanvas0"):
        import js
        from pyodide.code import run_js

        self.id = id
        remove_sketch(self.id)
        create_sketch = f"var {self.id} = " + "new p5(() => { });"
        run_js(create_sketch)
        set_canvas_id = f"{self.id}.canvas.setAttribute('id', '{self.id}');"
        run_js(set_canvas_id)
        self._p5js = getattr(js.window, self.id)
        self._init_constants()
        self._update_system_variables()

attempting to access the canvas on the p5 instance that was just initiated can sometimes fail if p5 takes a bit too long to initialize.

I ran into this when also importing p5 sound

  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script>

I think the addon might make p5 take longer than normal and exposes this problem.

Reproduction:
Starting from the starter template, load the p5js sound library, see crash on init.

example repo:
https://johnc14231.pyscriptapps.com/proceso-starter-copy-54206/

Suggested fix:
Wait till p5 initialization is complete before continuing setup. Not sure what the best approach is, since I don't see a "run_js_async" equivalent to "run_js". Maybe having some sort of callback that continues the python setup after p5 initialization.

@nickmcintyre nickmcintyre added the bug Something isn't working label Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants