Skip to content

Commit ebbeb54

Browse files
[3.15] gh-153020: Fix the tkinter event hook with Tcl interpreters in other threads (GH-162581) (GH-162613)
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 87e8ae1) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0ea9854 commit ebbeb54

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. */
@@ -655,8 +656,8 @@ Tcl_AppInit(Tcl_Interp *interp)
655656
* `tkMain.c'.
656657
*/
657658

658-
static void EnableEventHook(void); /* Forward */
659-
static void DisableEventHook(void); /* Forward */
659+
static void EnableEventHook(TkappObject *); /* Forward */
660+
static void DisableEventHook(TkappObject *); /* Forward */
660661

661662
static TkappObject *
662663
Tkapp_New(const char *screenName, const char *className,
@@ -830,7 +831,7 @@ Tkapp_New(const char *screenName, const char *className,
830831
return (TkappObject *)result;
831832
}
832833

833-
EnableEventHook();
834+
EnableEventHook(v);
834835

835836
return v;
836837
}
@@ -3219,10 +3220,10 @@ Tkapp_Dealloc(PyObject *op)
32193220
Tcl_DeleteInterp(Tkapp_Interp(self));
32203221
LEAVE_TCL
32213222
}
3223+
DisableEventHook(self);
32223224
Py_XDECREF(self->trace);
32233225
PyObject_Free(self);
32243226
Py_DECREF(tp);
3225-
DisableEventHook();
32263227
}
32273228

32283229

@@ -3527,6 +3528,10 @@ static PyMethodDef moduleMethods[] =
35273528
};
35283529

35293530
#ifdef WAIT_FOR_STDIN
3531+
/* The number of Tcl interpreters which use the event hook. */
3532+
static Py_ssize_t event_hook_users = 0;
3533+
static PyMutex event_hook_mutex = {0};
3534+
35303535
#ifndef MS_WINDOWS
35313536

35323537
static int stdin_ready = 0;
@@ -3544,8 +3549,6 @@ MyFileProc(void *clientData, int mask)
35443549
}
35453550
#endif
35463551

3547-
static PyThreadState *event_tstate = NULL;
3548-
35493552
static int
35503553
EventHook(void)
35513554
{
@@ -3554,9 +3557,18 @@ EventHook(void)
35543557
DWORD type;
35553558
#else
35563559
int tfile;
3560+
#endif
3561+
/* The hook is called with the GIL released, usually by PyOS_Readline()
3562+
in the thread which reads the input. Use the thread state of that
3563+
thread, not of the thread which created the Tcl interpreter. */
3564+
PyThreadState *tstate = PyGILState_GetThisThreadState();
3565+
if (tstate == NULL) {
3566+
return 0;
3567+
}
3568+
#ifndef MS_WINDOWS
35573569
stdin_ready = 0;
35583570
#endif
3559-
PyEval_RestoreThread(event_tstate);
3571+
PyEval_RestoreThread(tstate);
35603572
errorInCmd = 0;
35613573
#ifdef MS_WINDOWS
35623574
hStdin = GetStdHandle(STD_INPUT_HANDLE);
@@ -3588,7 +3600,7 @@ EventHook(void)
35883600
#endif
35893601
Py_BEGIN_ALLOW_THREADS
35903602
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3591-
tcl_tstate = event_tstate;
3603+
tcl_tstate = tstate;
35923604

35933605
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
35943606

@@ -3622,23 +3634,32 @@ EventHook(void)
36223634
#endif
36233635

36243636
static void
3625-
EnableEventHook(void)
3637+
EnableEventHook(TkappObject *self)
36263638
{
36273639
#ifdef WAIT_FOR_STDIN
3640+
PyMutex_Lock(&event_hook_mutex);
3641+
event_hook_users++;
36283642
if (PyOS_InputHook == NULL) {
3629-
event_tstate = PyThreadState_Get();
36303643
PyOS_InputHook = EventHook;
36313644
}
3645+
self->event_hook = 1;
3646+
PyMutex_Unlock(&event_hook_mutex);
36323647
#endif
36333648
}
36343649

36353650
static void
3636-
DisableEventHook(void)
3651+
DisableEventHook(TkappObject *self)
36373652
{
36383653
#ifdef WAIT_FOR_STDIN
3639-
if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
3654+
if (!self->event_hook) {
3655+
return;
3656+
}
3657+
PyMutex_Lock(&event_hook_mutex);
3658+
self->event_hook = 0;
3659+
if (--event_hook_users == 0 && PyOS_InputHook == EventHook) {
36403660
PyOS_InputHook = NULL;
36413661
}
3662+
PyMutex_Unlock(&event_hook_mutex);
36423663
#endif
36433664
}
36443665

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ Modules/_tkinter.c - var_mutex -
442442
Modules/_tkinter.c - command_mutex -
443443
Modules/_tkinter.c - HeadFHCD -
444444
Modules/_tkinter.c - stdin_ready -
445-
Modules/_tkinter.c - event_tstate -
445+
Modules/_tkinter.c - event_hook_users -
446+
Modules/_tkinter.c - event_hook_mutex -
446447
Modules/readline.c - completer_word_break_characters -
447448
Modules/readline.c - _history_length -
448449
Modules/readline.c - should_auto_add_history -

0 commit comments

Comments
 (0)