MacOS M3 system interrupts rendering from thread

I’ve been working on a little framework to render from a thread. Both to allow to have multiple windows active at the same time, and also to render when the window is being resized (blocked by PollEvents).

I’m using pyGLFW, a Python wrapper for GLFW which does not change/add any code, it only wraps the functions so it can be used from Python.

What I’ve been dealing with is that everything seems to render smoothly and without problems, as I try to move or resize a window the application might crash. This does not happen all the time but it does happen more frequently the higher the FPS (can test this by capping/limiting the FPS). Sometimes I get a Metal-related error, but most of the time the application is closed by a signal error.

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Along with some errors:

UNSUPPORTED (log once): setPrimitiveRestartEnabled:index: unsupported!
-[AGXG15XFamilyCommandBuffer renderCommandEncoderWithDescriptor:]:964: failed assertion `A command encoder is already encoding to this command buffer’

This does not seem to happen on Windows 10/11 and MacOS on my Intel Macbook Pro. It only happens on my new Macbook Pro M3. Might this be related to the emulation between OpenGL and Metal?

I was hoping someone could help me out in the right direction. I’ve made a “little” test script to run on which the error occurs. It’s gotten a bit more advanced that I’d like it to be, but the code is pretty self explainatory. It has an Application, that starts one or more initialized Windows. And then these windows each have their own update and render thread. Application has a loop on the main thread that deals with WaitEvents.

One more note is that it seems to be related to OpenGL code execution as if I remove the glDrawArray() function call, the error goes away, but then nothing is being rendered ofcourse.

Test file: test.py — Codefile [REQUIREMENTS: glfw, pyopengl, numpy]

I can’t see any calls to glfwMakeContextCurrent (which I would guess would be glfw.MakeContextCurrent in pyGLFW).

Without this the context is still current on another thread, which could explain this error.

There is on line 353: glfw.make_context_current(self._window).

I do know about making the context current. What I did find out though is that there seems to be a “bug” with NSContext (the MacOS) context, which does a glViewport on NSContextUpdate. Which is wrong, as I resize the window, the viewport size is automatically set, which is not desired. Maybe it is “stealing” the context when resizing cause this crash/error? Not sure if related though.