Skip to content

Commit 87e8ae1

Browse files
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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d3d0fdd commit 87e8ae1

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fix a crash in :mod:`tkinter` at the interactive prompt if the first Tcl
2+
interpreter was created in a thread which has since exited. The event hook
3+
which runs Tcl events while waiting for input is also no longer removed when
4+
an interpreter is deleted in other thread while other interpreters still
5+
exist, and its state no longer has data races in the :term:`free-threaded
6+
build`.

‎Modules/_tkinter.c‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ typedef struct {
382382
int threaded; /* True if tcl_platform[threaded] */
383383
Tcl_ThreadId thread_id;
384384
int dispatching;
385+
int event_hook; /* True if counted by EnableEventHook() */
385386
PyObject *trace;
386387
/* We cannot include tclInt.h, as this is internal.
387388
So we cache interesting types here. */
@@ -664,8 +665,8 @@ Tcl_AppInit(Tcl_Interp *interp)
664665
* `tkMain.c'.
665666
*/
666667

667-
static void EnableEventHook(void); /* Forward */
668-
static void DisableEventHook(void); /* Forward */
668+
static void EnableEventHook(TkappObject *); /* Forward */
669+
static void DisableEventHook(TkappObject *); /* Forward */
669670

670671
static TkappObject *
671672
Tkapp_New(const char *screenName, const char *className,
@@ -851,7 +852,7 @@ Tkapp_New(const char *screenName, const char *className,
851852
return (TkappObject *)result;
852853
}
853854

854-
EnableEventHook();
855+
EnableEventHook(v);
855856

856857
return v;
857858
}
@@ -3408,10 +3409,10 @@ Tkapp_Dealloc(PyObject *op)
34083409
Tcl_DeleteInterp(Tkapp_Interp(self));
34093410
LEAVE_TCL
34103411
}
3412+
DisableEventHook(self);
34113413
(void)Tkapp_Clear(op);
34123414
tp->tp_free(op);
34133415
Py_DECREF(tp);
3414-
DisableEventHook();
34153416
}
34163417

34173418

@@ -3722,6 +3723,10 @@ static PyMethodDef moduleMethods[] =
37223723
};
37233724

37243725
#ifdef WAIT_FOR_STDIN
3726+
/* The number of Tcl interpreters which use the event hook. */
3727+
static Py_ssize_t event_hook_users = 0;
3728+
static PyMutex event_hook_mutex = {0};
3729+
37253730
#ifndef MS_WINDOWS
37263731

37273732
static int stdin_ready = 0;
@@ -3739,8 +3744,6 @@ MyFileProc(void *clientData, int mask)
37393744
}
37403745
#endif
37413746

3742-
static PyThreadState *event_tstate = NULL;
3743-
37443747
static int
37453748
EventHook(void)
37463749
{
@@ -3749,9 +3752,18 @@ EventHook(void)
37493752
DWORD type;
37503753
#else
37513754
int tfile;
3755+
#endif
3756+
/* The hook is called with the GIL released, usually by PyOS_Readline()
3757+
in the thread which reads the input. Use the thread state of that
3758+
thread, not of the thread which created the Tcl interpreter. */
3759+
PyThreadState *tstate = PyGILState_GetThisThreadState();
3760+
if (tstate == NULL) {
3761+
return 0;
3762+
}
3763+
#ifndef MS_WINDOWS
37523764
stdin_ready = 0;
37533765
#endif
3754-
PyEval_RestoreThread(event_tstate);
3766+
PyEval_RestoreThread(tstate);
37553767
errorInCmd = 0;
37563768
#ifdef MS_WINDOWS
37573769
hStdin = GetStdHandle(STD_INPUT_HANDLE);
@@ -3783,7 +3795,7 @@ EventHook(void)
37833795
#endif
37843796
Py_BEGIN_ALLOW_THREADS
37853797
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3786-
tcl_tstate = event_tstate;
3798+
tcl_tstate = tstate;
37873799

37883800
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
37893801

@@ -3817,23 +3829,32 @@ EventHook(void)
38173829
#endif
38183830

38193831
static void
3820-
EnableEventHook(void)
3832+
EnableEventHook(TkappObject *self)
38213833
{
38223834
#ifdef WAIT_FOR_STDIN
3835+
PyMutex_Lock(&event_hook_mutex);
3836+
event_hook_users++;
38233837
if (PyOS_InputHook == NULL) {
3824-
event_tstate = PyThreadState_Get();
38253838
PyOS_InputHook = EventHook;
38263839
}
3840+
self->event_hook = 1;
3841+
PyMutex_Unlock(&event_hook_mutex);
38273842
#endif
38283843
}
38293844

38303845
static void
3831-
DisableEventHook(void)
3846+
DisableEventHook(TkappObject *self)
38323847
{
38333848
#ifdef WAIT_FOR_STDIN
3834-
if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
3849+
if (!self->event_hook) {
3850+
return;
3851+
}
3852+
PyMutex_Lock(&event_hook_mutex);
3853+
self->event_hook = 0;
3854+
if (--event_hook_users == 0 && PyOS_InputHook == EventHook) {
38353855
PyOS_InputHook = NULL;
38363856
}
3857+
PyMutex_Unlock(&event_hook_mutex);
38373858
#endif
38383859
}
38393860

‎Tools/c-analyzer/cpython/globals-to-fix.tsv‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ Modules/_tkinter.c - var_mutex -
437437
Modules/_tkinter.c - command_mutex -
438438
Modules/_tkinter.c - HeadFHCD -
439439
Modules/_tkinter.c - stdin_ready -
440-
Modules/_tkinter.c - event_tstate -
440+
Modules/_tkinter.c - event_hook_users -
441+
Modules/_tkinter.c - event_hook_mutex -
441442
Modules/readline.c - completer_word_break_characters -
442443
Modules/readline.c - _history_length -
443444
Modules/readline.c - should_auto_add_history -

0 commit comments

Comments
 (0)