Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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`.
45 changes: 33 additions & 12 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -831,7 +832,7 @@ Tkapp_New(const char *screenName, const char *className,
return (TkappObject *)result;
}

EnableEventHook();
EnableEventHook(v);

return v;
}
Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -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;
Expand All @@ -3721,8 +3726,6 @@ MyFileProc(void *clientData, int mask)
}
#endif

static PyThreadState *event_tstate = NULL;

static int
EventHook(void)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
Loading