Repository navigation
Closed
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixestopic-tkintertype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Description
| BPO | 39093 |
|---|---|
| Nosy | @pitrou, @serhiy-storchaka, @E-Paine |
| Files |
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:
assignee = None
closed_at = None
created_at = <Date 2019-12-19.02:56:56.653>
labels = ['expert-tkinter', '3.9', '3.10', '3.8', '3.7', 'type-crash']
title = 'tkinter objects garbage collected from non-tkinter thread cause crash'
updated_at = <Date 2020-06-18.10:04:23.683>
user = 'https://bugs.python.org/obserience'bugs.python.org fields:
activity = <Date 2020-06-18.10:04:23.683>
actor = 'epaine'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Tkinter']
creation = <Date 2019-12-19.02:56:56.653>
creator = 'obserience'
dependencies = []
files = ['48789', '48791']
hgrepos = []
issue_num = 39093
keywords = []
message_count = 6.0
messages = ['358652', '358654', '358685', '371536', '371773', '371804']
nosy_count = 5.0
nosy_names = ['pitrou', 'gpolo', 'serhiy.storchaka', 'obserience', 'epaine']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue39093'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']Linked PRs
- gh-83274: Stop deallocation of Tkapp causing Python interpreter crash #21532
- gh-83274: Don't crash when a Tcl interpreter is deallocated in the wrong thread #152323
- [3.15] gh-83274: Don't crash when a Tcl interpreter is deallocated in the wrong thread (GH-152323) #153271
- [3.14] gh-83274: Don't crash when a Tcl interpreter is deallocated in the wrong thread (GH-152323) #153277
- [3.13] gh-83274: Don't crash when a Tcl interpreter is deallocated in the wrong thread (GH-152323) #153278
Reacted by Amund Eggen Svandal and Martin Coll
Metadata
Metadata
Assignees
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixestopic-tkintertype-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Projects
- StatusShow more project fieldsDone
Activity
obserience commentedon Dec 19, 2019
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:
"self.tk = None"
to the end of Misc.destroy() (tkinter/init.py line:439) should fix this by removing these reference when widgets are destroyed. (Note:destroy is recursive on the widget tree and called on the root object when a Tkinter GUI exits)
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.
obserience commentedon Dec 19, 2019
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
gboeing/osmnx#75
https://stackoverflow.com/questions/27073762/tcl-asyncdelete-error-multithreading-python
(and so on)
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.
obserience commentedon Dec 19, 2019
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:
-may expect the self.tk attribute of destroyed widgets to still be set
Existing code (after calling root.destroy()):
-calls methods like Tk.quit() which runs Tcl commands requiring self.tk to still be there
--Idle does this "# (Needed for clean exit on Windows 98)" <--apparently
---is this still the case??
-may call get() and set() on Variable instances (this currently works)
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###
The Tk.Quit() doc is wrong. Current inline docs say that it destroys all widgets, which it does not. All it seems to do is cause mainloop to return after which mainloop can be called again and the GUI resumes functioning. This has caused problems when the main thread doesn't exit since the GUI continues to exist but is frozen. (See quit_behavior.py)
E-Paine commentedon Jun 15, 2020
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).
8 remaining items
gh-83274: Don't crash when a Tcl interpreter is deallocated in the wr…
[3.15] gh-83274: Don't crash when a Tcl interpreter is deallocated in…
serhiy-storchaka commentedon Jul 7, 2026
Fixed by #152323:
Tkapp_Deallocnow leaks the interpreter and emits aRuntimeWarninginstead of aborting when it's deallocated in the wrong thread — the minimal, safe fix agreed on here. Thanks @E-Paine for the diagnosis and the original patch.Making tkinter generally thread-safe is out of scope and not pursued here.
[3.13] gh-83274: Don't crash when a Tcl interpreter is deallocated in…
[3.14] gh-83274: Don't crash when a Tcl interpreter is deallocated in…