pythonpython-3.xmacostkintercrash

Python app crashed with "PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released"


I have a Python Program, and because it's too big, here's the link to its source code: Link

When I run it on Mac, sometimes I get this weird exception:

Fatal Python error: PyEval_RestoreThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Current thread 0x0000000115f8ce00 (most recent call first):
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1429 in mainloop
  File ".../PyPlusSource/pyplus.py", line 1435 in <module>

So why is this happenning? Please explain in simple words. Thanks for any ideas!

EDIT:
I'm on MacOS 11.2.3 Big Sur [Not beta], with Python 3.9.2 [Not beta again] installed. And this error is hard to reproduce


Solution

  • In general, this should only possibly happen in a code combining Python with components in other languages (typically C; see, e.g., When the GIL is released?).

    From your description, it's not clear which part of your code causes the error (the original question asked 11 days ago says it's line 1435, your video added 6 days ago shows it's line 1452, and the PyPlus code at GitHub you refer to was changed 2 days ago and currently only has 1441 lines) and therefore, you'll need to check yourself where exactly in your code the error is produced.

    The error further refers to line 1429 in tkinter/__init__.py which (as far as I can see in my Python 3.9 version of tkinter) is if string: in the following function:

    def _getints(self, string):
        """Internal function."""
        if string:
            return tuple(map(self.tk.getint, self.tk.splitlist(string)))
    

    So, it seems that the function is called with undefined variable string.

    I have got this PyEval_RestoreThread error a couple of times with various reasons: