Skip to content
Open
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,2 @@
Fix a data race in :mod:`!_tkinter` input event handling in free-threaded
builds.
8 changes: 5 additions & 3 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Copyright (C) 1994 Steen Lumholt.
#endif

#include "pycore_long.h" // _PyLong_IsNegative()
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR, FT_ATOMIC_STORE_PTR
#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8String

#ifdef MS_WINDOWS
Expand Down Expand Up @@ -3655,7 +3656,7 @@ EventHook(void)
int tfile;
stdin_ready = 0;
#endif
PyEval_RestoreThread(event_tstate);
PyEval_RestoreThread(FT_ATOMIC_LOAD_PTR(event_tstate));
errorInCmd = 0;
#ifdef MS_WINDOWS
hStdin = GetStdHandle(STD_INPUT_HANDLE);
Expand Down Expand Up @@ -3687,7 +3688,7 @@ EventHook(void)
#endif
Py_BEGIN_ALLOW_THREADS
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
tcl_tstate = event_tstate;
tcl_tstate = FT_ATOMIC_LOAD_PTR(event_tstate);

result = Tcl_DoOneEvent(TCL_DONT_WAIT);

Expand Down Expand Up @@ -3725,7 +3726,7 @@ EnableEventHook(void)
{
#ifdef WAIT_FOR_STDIN
if (PyOS_InputHook == NULL) {
event_tstate = PyThreadState_Get();
FT_ATOMIC_STORE_PTR(event_tstate, PyThreadState_Get());
PyOS_InputHook = EventHook;
}
#endif
Expand All @@ -3737,6 +3738,7 @@ DisableEventHook(void)
#ifdef WAIT_FOR_STDIN
if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
PyOS_InputHook = NULL;
FT_ATOMIC_STORE_PTR(event_tstate, NULL);
}
#endif
}
Expand Down
Loading