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

Question:

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/[email protected]/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

Asked By: Andy Zhang

||

Answers:

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:

  • When a function from a cythonized Python module (.so on Mac) calls a function from another module that was imported only indirectly. For example, if module a.py imports b.py, b.py imports c.py, and a function in a.py uses a function from c.py. When running from PyCharm or from terminal via python3 myprogram.py, there are no errors but as soon as the modules are cythonized, the functions in a.so cannot found the functions from c.so anymore and I get this error.

  • On an uncaught exception in a secondary thread; for example when I haven’t checked the type of a variable read from a file and was trying to update a string in my (PySimpleGUI = tkinter-based) GUI with its value that was no string.

Answered By: Lenka Čížková

I get a similar error intermittently and I run on a Intel ASUS under Windows 10.
It is sure disheartening.

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 0x00003a2c (most recent call first):
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libsite-packagespyttsx3driverssapi5.py", line 127 in startLoop
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libsite-packagespyttsx3driver.py", line 192 in runAndWait
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libsite-packagespyttsx3engine.py", line 180 in runAndWait
  File "C:Usersraysmworkspacepythonresource_lib_projsrcaudio_draw_window.py", line 381 in speak_text_line
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 814 in callit
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1892 in __call__
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1314 in update
  File "C:Usersraysmworkspacepythonresource_lib_projsrcaudio_draw_window.py", line 2552 in update
  File "C:Usersraysmworkspacepythonresource_lib_projsrcaudio_draw_window.py", line 447 in on_button_1
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1892 in __call__
  File "C:UsersraysmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1429 in mainloop
  File "C:Usersraysmworkspacepythonresource_lib_projsrccanvas_grid.py", line 566 in test4a
  File "C:Usersraysmworkspacepythonresource_lib_projsrccanvas_grid.py", line 571 in <module>
Answered By: crs