Description
Tcl allocates per-thread resources which are only freed by Tcl_FinalizeThread(), and tkinter never calls it. A thread leaks them when it exits if it called a Tcl interpreter that belongs to another thread, or if it created a Tcl interpreter of its own.
On Windows the leak includes an event handle: a call to an interpreter in another thread waits with Tcl_ConditionWait(), which creates a per-thread event (condEvent in win/tclWinThrd.c) that is only closed by a thread exit handler, which in turn only runs in Tcl_FinalizeThread(). So a program whose worker threads call tkinter leaks one handle per thread. On other platforms the threaded Tcl allocator's per-thread caches leak in the same way.
This was found by the refleak buildbots twice: #161353 (test_tcl) and the failure of test_nested_mainloop reported in #162651. Both were fixed or would be fixed by calling the private _tkinter._finalize_thread() at the end of the thread in the test. In a thread that created a Tk interpreter, finalization is also needed to avoid a crash in Tcl in the next thread which uses Tk (MainloopThreadsTest crashes without it).
tkinter should call Tcl_FinalizeThread() itself when the Python thread state is cleared at the end of such a thread, unless the thread still has a live Tcl interpreter.
Activity
gh-162724: Free per-thread Tcl resources at thread exit in tkinter (G…
vstinner commentedon Dec 4, 2026
See also #163151: test_tkinter leaked [1, 1, 1] handles, sum=3 on AMD64 Windows Server 2025 Refleaks 3.x.
serhiy-storchaka commentedon Dec 4, 2026
_tkinter._finalize_thread()has not been removed, but it is no longer used.[3.15] gh-162724: Free per-thread Tcl resources at thread exit in tki…
[3.14] gh-162724: Free per-thread Tcl resources at thread exit in tki…