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

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


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.