From b45a4538e9b7a527af8ed3e7ddfbf71f0b82dac1 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Nov 2026 20:57:16 +0200 Subject: [PATCH 1/2] gh-153020: Fix the tkinter event hook with Tcl interpreters in other threads 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. Co-Authored-By: Claude Opus 5.5 --- ...-11-24-19-00-00.gh-issue-153020.Hk2vTs.rst | 6 +++ Modules/_tkinter.c | 45 ++++++++++++++----- 2 files changed, 39 insertions(+), 12 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 d0a03953a80d92..43dbbbb93c0ae8 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -362,6 +362,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. */ @@ -644,8 +645,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, @@ -831,7 +832,7 @@ Tkapp_New(const char *screenName, const char *className, return (TkappObject *)result; } - EnableEventHook(); + EnableEventHook(v); return v; } @@ -3390,10 +3391,10 @@ Tkapp_Dealloc(PyObject *op) Tcl_DeleteInterp(Tkapp_Interp(self)); LEAVE_TCL } + DisableEventHook(self); (void)Tkapp_Clear(op); tp->tp_free(op); Py_DECREF(tp); - DisableEventHook(); } @@ -3704,6 +3705,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; @@ -3721,8 +3726,6 @@ MyFileProc(void *clientData, int mask) } #endif -static PyThreadState *event_tstate = NULL; - static int EventHook(void) { @@ -3731,9 +3734,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); @@ -3765,7 +3777,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); @@ -3799,23 +3811,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 } From 8af46cd9aff5b8360bb8e93d39ec334debfff8f7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Nov 2026 22:49:45 +0200 Subject: [PATCH 2/2] Update globals-to-fix.tsv --- Tools/c-analyzer/cpython/globals-to-fix.tsv | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index 0d0eb929aa116a..cd1939df856c16 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -437,7 +437,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 -