Skip to content

Commit cd8c633

Browse files
committed
gh-153020: Fix race condition on event_tstate in _tkinter
1 parent ed71655 commit cd8c633

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Modules/_tkinter.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,7 +3655,7 @@ EventHook(void)
36553655
int tfile;
36563656
stdin_ready = 0;
36573657
#endif
3658-
PyEval_RestoreThread(event_tstate);
3658+
PyEval_RestoreThread(_Py_atomic_load_ptr(&event_tstate));
36593659
errorInCmd = 0;
36603660
#ifdef MS_WINDOWS
36613661
hStdin = GetStdHandle(STD_INPUT_HANDLE);
@@ -3687,7 +3687,7 @@ EventHook(void)
36873687
#endif
36883688
Py_BEGIN_ALLOW_THREADS
36893689
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3690-
tcl_tstate = event_tstate;
3690+
tcl_tstate = _Py_atomic_load_ptr(&event_tstate);
36913691

36923692
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
36933693

@@ -3725,7 +3725,7 @@ EnableEventHook(void)
37253725
{
37263726
#ifdef WAIT_FOR_STDIN
37273727
if (PyOS_InputHook == NULL) {
3728-
event_tstate = PyThreadState_Get();
3728+
_Py_atomic_store_ptr(&event_tstate, PyThreadState_Get());
37293729
PyOS_InputHook = EventHook;
37303730
}
37313731
#endif
@@ -3737,6 +3737,7 @@ DisableEventHook(void)
37373737
#ifdef WAIT_FOR_STDIN
37383738
if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
37393739
PyOS_InputHook = NULL;
3740+
_Py_atomic_store_ptr(&event_tstate, NULL);
37403741
}
37413742
#endif
37423743
}

0 commit comments

Comments
 (0)