From bbd6f83a92a0f6d64984329bb1f37605afff202a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 4 Dec 2026 15:00:12 +0200 Subject: [PATCH] [3.14] gh-162724: Free per-thread Tcl resources at thread exit in tkinter (GH-162725) Call Tcl_FinalizeThread() when the Python thread state is cleared at the end of a thread which made calls to a Tcl interpreter in other thread or created a Tcl interpreter, unless the thread still has a Tcl interpreter. This fixes a leak of an event handle on Windows for every such thread. (cherry picked from commit f16b34a4c266245d6780d934f1d3de6783a656f0) Co-authored-by: Serhiy Storchaka --- ...-11-27-12-00-00.gh-issue-162724.Fq7Thd.rst | 3 + Modules/_tkinter.c | 61 ++++++++++++++++++- Tools/c-analyzer/cpython/ignored.tsv | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-27-12-00-00.gh-issue-162724.Fq7Thd.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-27-12-00-00.gh-issue-162724.Fq7Thd.rst b/Misc/NEWS.d/next/Library/2026-11-27-12-00-00.gh-issue-162724.Fq7Thd.rst new file mode 100644 index 00000000000000..781982af93f766 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-12-00-00.gh-issue-162724.Fq7Thd.rst @@ -0,0 +1,3 @@ +Fix a leak of per-thread Tcl resources (an event handle on Windows) in +:mod:`tkinter` when a thread which made calls to a Tcl interpreter in other +thread, or which created a Tcl interpreter, exits. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index d885705bbe2a81..0c5d88435fbbf7 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -460,6 +460,50 @@ static Tcl_ThreadDataKey excInCmd_key; #define excInCmd \ (*(PyObject **)Tcl_GetThreadData(&excInCmd_key, sizeof(PyObject *))) +/* The number of live Tcl interpreters created in the current thread. */ +static Tcl_ThreadDataKey interp_count_key; +#define interp_count \ + (*(int *)Tcl_GetThreadData(&interp_count_key, sizeof(int))) + +/* Tcl allocates per-thread resources (for example an event handle on + Windows for every thread which waits for a call to a Tcl interpreter in + other thread), which are only freed by Tcl_FinalizeThread(). Call it + when the Python thread state is cleared at the end of the thread, unless + the thread still has a Tcl interpreter. */ + +#define THREAD_FINALIZER_NAME "_tkinter.thread_finalizer" + +static void +thread_finalizer(PyObject *capsule) +{ + void *thread_id = PyCapsule_GetPointer(capsule, THREAD_FINALIZER_NAME); + if (thread_id == (void *)Tcl_GetCurrentThread() && interp_count == 0) { + Tcl_FinalizeThread(); + } +} + +static int +register_thread_finalizer(void) +{ + PyObject *dict = PyThreadState_GetDict(); + if (dict == NULL) { + return 0; + } + int r = PyDict_ContainsString(dict, THREAD_FINALIZER_NAME); + if (r != 0) { + return r < 0 ? -1 : 0; + } + PyObject *capsule = PyCapsule_New((void *)Tcl_GetCurrentThread(), + THREAD_FINALIZER_NAME, + thread_finalizer); + if (capsule == NULL) { + return -1; + } + r = PyDict_SetItemString(dict, THREAD_FINALIZER_NAME, capsule); + Py_DECREF(capsule); + return r; +} + static PyObject *Tkapp_UnicodeResult(TkappObject *); @@ -709,6 +753,7 @@ Tkapp_New(const char *screenName, const char *className, static PyMutex tcl_create_mutex; PyMutex_Lock(&tcl_create_mutex); v->interp = Tcl_CreateInterp(); + interp_count++; v->wantobjects = wantobjects; #if TCL_MAJOR_VERSION >= 9 v->threaded = 1; @@ -735,6 +780,10 @@ Tkapp_New(const char *screenName, const char *className, tcl_lock = NULL; } PyMutex_Unlock(&tcl_create_mutex); + if (register_thread_finalizer() < 0) { + Py_DECREF(v); + return NULL; + } v->OldBooleanType = Tcl_GetObjType("boolean"); { @@ -1641,6 +1690,9 @@ Tkapp_Call(PyObject *selfptr, PyObject *args) PyObject *exc = NULL; // init to make static analyzers happy if (!WaitForMainloop(self)) return NULL; + if (register_thread_finalizer() < 0) { + return NULL; + } ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent)); if (ev == NULL) { PyErr_NoMemory(); @@ -1922,6 +1974,9 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) if (!WaitForMainloop(self)) return NULL; + if (register_thread_finalizer() < 0) { + return NULL; + } ev = (VarEvent*)attemptckalloc(sizeof(VarEvent)); if (ev == NULL) { PyErr_NoMemory(); @@ -2603,7 +2658,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name, } if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && - !WaitForMainloop(self)) + (!WaitForMainloop(self) || register_thread_finalizer() < 0)) return NULL; TRACE(self, ("((ss()O))", "proc", name, func)); @@ -2679,6 +2734,9 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name) Tcl_Condition cond = NULL; CommandEvent *ev; + if (register_thread_finalizer() < 0) { + return NULL; + } ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent)); if (ev == NULL) { PyErr_NoMemory(); @@ -3249,6 +3307,7 @@ Tkapp_Dealloc(PyObject *op) ENTER_TCL Tcl_DeleteInterp(Tkapp_Interp(self)); LEAVE_TCL + interp_count--; } DisableEventHook(self); Py_XDECREF(self->trace); diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index df0629b6d7ac76..11370b5b930b6a 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -259,6 +259,7 @@ Modules/_struct.c - lilendian_table - Modules/_struct.c - native_table - Modules/_tkinter.c - state_key - Modules/_tkinter.c - excInCmd_key - +Modules/_tkinter.c - interp_count_key - Modules/_interpchannelsmodule.c - _channelid_end_recv - Modules/_interpchannelsmodule.c - _channelid_end_send - Modules/_zoneinfo.c - DAYS_BEFORE_MONTH -