-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
tkinter objects garbage collected from non-tkinter thread cause crash #83274
Comments
|
All tkinter objects have a reference to the TCL interpreter object "self.tk". The current cleanup code does not remove these when a widget is destroyed. Garbage collection of the TCL interpreter object occurs only after all gui objects are garbage collected. This may be triggered from another thread causing TCL to panic and trigger a core dump. Error message:
Adding: I can't see any problem with removing the interpreter object from a widget when it is destroyed. There doesn't seem to be any way to reassign a widget to a new parent so this shouldn't affect anything. Doing this makes it safe(r) to use tkinter from a non-main thread since if the GUI cleans up properly no "landmines" are left to cause a crash when garbage collected in the wrong thread. |
|
A quick google search for "Tcl_AsyncDelete: async handler deleted by the wrong thread" yields plenty of users complaining about this error case. Usually they're trying add a gui to an existing application. They run it in a thread other than main and then leak a reference to a GUI widget into another thread. The GUI exits and then some time later, the last reference to a widget dissapears but this is triggered by the leaked reference and occurs in another thread context. The TK library panics and the application crashes. matplotlib/matplotlib#12085 The response time and time again is just "you can't run tkinter in any thread but main". Hopefully if this fix is implemented, that becomes less true. |
|
My proposed fix isn't compatible with a lot of current Tkinter using code. It was a naive first attempt and breaks existing code and doesn't completely solve the problem. Variables currently retain a reference to the TCL interpreter self._tk and aren't linked into the widget tree or otherwise accessible to any cleanup logic. Existing code: Existing code (after calling root.destroy()): Given this it might be better to track all references to the TCL interpreter in a separate data structure and add a Tk.cleanup() method that gets rid of them all with documentation indicating all calls after this will fail. Alternate implementation of this deallocates the TCL interpreter in the _tkinter.c bindings with the same results. One of the core issues here is that the cause of the crash is asynchronous signal handlers installed by the TCL library and necessary for the TCL interpreter to run at all. If you can run TCL commands, the crash can still be triggered. ###separate issue### |
|
My initial thoughts were just to give the standard lecture on why "tkinter + threads = bad". However, looking again at the problem, the async handler error causes the Python interpreter to crash, which, no matter how frowned upon the code is, shouldn't happen. Ultimately, the issue is that a Tkapp object is being deleted in any thread other than the one it was created in. I believe there are two parts to the solution, both of which are for the Tkapp object in the _tkinter module (as there is no guarantee that the user will go through the standard tkinter module):
I don't know how to implement this, but if someone has some ideas, I am more than willing to help solidify them into a PR. Antoine, I hope you are the right person to add to the nosy for this (apologies if not). |
|
I looked into this in greater depth a while back and never followed up. In "Modules/_tkinter.c" the Tkapp_Dealloc() function has a commented out check that would ensure it isn't called from the wrong thread. Tkapp_Dealloc(PyObject *self)
{
PyObject *tp = (PyObject *) Py_TYPE(self);
/*CHECK_TCL_APPARTMENT;*/
ENTER_TCL
Tcl_DeleteInterp(Tkapp_Interp(self));
LEAVE_TCL
PyObject_Del(self);
Py_DECREF(tp);
DisableEventHook();
}If this is uncommented, garbage collection from a non-main thread will fail and the object will be leaked. **Doing this makes the code safe.** No more crashes. It should be done a bugfix regardless of anything else. ####################
Restricting interpreter object creation/destruction to the main thread is very much an API change that would break any code that puts the interpreter in a non-main thread. Since we're keeping the objects around till the interpreter exits we still "leak" the memory and calling the destructor before the process exits has no benefits. Directly leaking the memory isn't any worse in terms of memory consumption and doesn't break currently working code. #################### A nice to have would be changing the python tkinter code to cleanup the references when the associated GUI exits (as it arguably should) implementations: For all implementations we need code to handle post-cleanup tk retrieval failure: The user can shoot themselves in the foot by grabbing an explicit reference to the TCL interpreter but I've never seen that done and now it only causes a memory leak (which we can warn about). Again, the python changes are nice to have but the "Modules/_tkinter.c" "CHECK_TCL_APPARTMENT;" macro needs to be uncommented. I'd be happy to do the python side-changes if that's likely to be accepted. |
I am not sure I agree with your statement that we would be keeping the interpreter exits, as that was only a 'fallback' if a delete was attempted in a thread. However, I agree with you that we should avoid breaking existing code where possible and so such a change shouldn't be added. As for uncommenting CHECK_TCL_APPARTMENT in Tkapp_Dealloc, I looked at the blame and found that it was added as a comment:
As for actually fixing the root of the problem, I suspect any of the changes you proposed, similar to mine, would be too controversial and not really go anywhere (being fairly large changes to fix just a handful of situations). |


Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: