← 输入框|github.com|刷新
Skip to content

tkinter leaks per-thread Tcl resources when a thread exits #162724

Backlog

Description

@serhiy-storchaka

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.

Linked PRs

Activity

  1. added
    type-bug
    3.14
    3.15
    3.16
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.143.153.16topic-tkintertype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions