From 2f6aa07e1ab9c58afb81cdac8065816f01093936 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Nov 2026 23:30:21 +0200 Subject: [PATCH] [3.15] gh-153020: Fix the tkinter event hook with Tcl interpreters in other threads (GH-162581) The event hook used the thread state of the thread which created the first Tcl interpreter, and crashed if that thread had exited. It now uses the thread state of the thread which calls it. It is also no longer removed when an interpreter is deleted in other thread while other interpreters still exist, and its state is protected by a lock. (cherry picked from commit 87e8ae1ea5745ac9f578b1e92154b1eb97380b09) Co-authored-by: Serhiy Storchaka Co-Authored-By: Claude Opus 5.5 --- ...-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst | 6 +++ Modules/_tkinter.c | 45 ++++++++++++++----- Tools/c-analyzer/cpython/globals-to-fix.tsv | 3 +- 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst b/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst new file mode 100644 index 00000000000000..7c2a81de7cf8bb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst @@ -0,0 +1,6 @@ +Fix a crash in :mod:`tkinter` at the interactive prompt if the first Tcl +interpreter was created in a thread which has since exited. The event hook +which runs Tcl events while waiting for input is also no longer removed when +an interpreter is deleted in other thread while other interpreters still +exist, and its state no longer has data races in the :term:`free-threaded +build`. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 8e2ab6d24f5e95..7312430e685d52 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -382,6 +382,7 @@ typedef struct { int threaded; /* True if tcl_platform[threaded] */ Tcl_ThreadId thread_id; int dispatching; + int event_hook; /* True if counted by EnableEventHook() */ PyObject *trace; /* We cannot include tclInt.h, as this is internal. So we cache interesting types here. */ @@ -655,8 +656,8 @@ Tcl_AppInit(Tcl_Interp *interp) * `tkMain.c'. */ -static void EnableEventHook(void); /* Forward */ -static void DisableEventHook(void); /* Forward */ +static void EnableEventHook(TkappObject *); /* Forward */ +static void DisableEventHook(TkappObject *); /* Forward */ static TkappObject * Tkapp_New(const char *screenName, const char *className, @@ -830,7 +831,7 @@ Tkapp_New(const char *screenName, const char *className, return (TkappObject *)result; } - EnableEventHook(); + EnableEventHook(v); return v; } @@ -3219,10 +3220,10 @@ Tkapp_Dealloc(PyObject *op) Tcl_DeleteInterp(Tkapp_Interp(self)); LEAVE_TCL } + DisableEventHook(self); Py_XDECREF(self->trace); PyObject_Free(self); Py_DECREF(tp); - DisableEventHook(); } @@ -3527,6 +3528,10 @@ static PyMethodDef moduleMethods[] = }; #ifdef WAIT_FOR_STDIN +/* The number of Tcl interpreters which use the event hook. */ +static Py_ssize_t event_hook_users = 0; +static PyMutex event_hook_mutex = {0}; + #ifndef MS_WINDOWS static int stdin_ready = 0; @@ -3544,8 +3549,6 @@ MyFileProc(void *clientData, int mask) } #endif -static PyThreadState *event_tstate = NULL; - static int EventHook(void) { @@ -3554,9 +3557,18 @@ EventHook(void) DWORD type; #else int tfile; +#endif + /* The hook is called with the GIL released, usually by PyOS_Readline() + in the thread which reads the input. Use the thread state of that + thread, not of the thread which created the Tcl interpreter. */ + PyThreadState *tstate = PyGILState_GetThisThreadState(); + if (tstate == NULL) { + return 0; + } +#ifndef MS_WINDOWS stdin_ready = 0; #endif - PyEval_RestoreThread(event_tstate); + PyEval_RestoreThread(tstate); errorInCmd = 0; #ifdef MS_WINDOWS hStdin = GetStdHandle(STD_INPUT_HANDLE); @@ -3588,7 +3600,7 @@ EventHook(void) #endif Py_BEGIN_ALLOW_THREADS if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = event_tstate; + tcl_tstate = tstate; result = Tcl_DoOneEvent(TCL_DONT_WAIT); @@ -3622,23 +3634,32 @@ EventHook(void) #endif static void -EnableEventHook(void) +EnableEventHook(TkappObject *self) { #ifdef WAIT_FOR_STDIN + PyMutex_Lock(&event_hook_mutex); + event_hook_users++; if (PyOS_InputHook == NULL) { - event_tstate = PyThreadState_Get(); PyOS_InputHook = EventHook; } + self->event_hook = 1; + PyMutex_Unlock(&event_hook_mutex); #endif } static void -DisableEventHook(void) +DisableEventHook(TkappObject *self) { #ifdef WAIT_FOR_STDIN - if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { + if (!self->event_hook) { + return; + } + PyMutex_Lock(&event_hook_mutex); + self->event_hook = 0; + if (--event_hook_users == 0 && PyOS_InputHook == EventHook) { PyOS_InputHook = NULL; } + PyMutex_Unlock(&event_hook_mutex); #endif } diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index db575d870be5c5..2172d30f32bb0f 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -442,7 +442,8 @@ Modules/_tkinter.c - var_mutex - Modules/_tkinter.c - command_mutex - Modules/_tkinter.c - HeadFHCD - Modules/_tkinter.c - stdin_ready - -Modules/_tkinter.c - event_tstate - +Modules/_tkinter.c - event_hook_users - +Modules/_tkinter.c - event_hook_mutex - Modules/readline.c - completer_word_break_characters - Modules/readline.c - _history_length - Modules/readline.c - should_auto_add_history -