Skip to content

Commit d3d0fdd

Browse files
gh-84256: Fix a tkinter crash when Tk calls an event handler outside of Tcl (GH-162547)
This happens on Windows when a window message is dispatched from a foreign event loop, for example when the window is resized while a native dialog is shown. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bc63806 commit d3d0fdd

3 files changed

Lines changed: 84 additions & 21 deletions

File tree

‎Lib/test/test_tkinter/test_misc.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tkinter import TclError, ttk
1515
import enum
1616
from test import support
17+
from test.support import import_helper
1718
from test.support import os_helper
1819
from test.support.isolation import runInSubprocess
1920
from test.support.script_helper import assert_python_ok
@@ -749,6 +750,47 @@ def test_accessible(self):
749750
if self.root._windowingsystem != 'aqua':
750751
acc.emit_focus_change()
751752

753+
@unittest.skipUnless(sys.platform == 'win32', 'requires Windows')
754+
def test_callback_from_foreign_event_loop(self):
755+
# gh-84256: Tk can call an event handler when Tcl was not entered
756+
# by tkinter, for example when a window message is dispatched
757+
# from a foreign event loop while the GIL is released.
758+
ctypes = import_helper.import_module('ctypes')
759+
from ctypes import wintypes
760+
user32 = ctypes.WinDLL('user32')
761+
user32.SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT,
762+
wintypes.WPARAM, wintypes.LPARAM]
763+
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND,
764+
ctypes.c_int, ctypes.c_int,
765+
ctypes.c_int, ctypes.c_int,
766+
wintypes.UINT]
767+
WM_ENTERSIZEMOVE = 0x0231
768+
WM_EXITSIZEMOVE = 0x0232
769+
SWP_NOMOVE = 0x0002
770+
SWP_NOZORDER = 0x0004
771+
SWP_NOACTIVATE = 0x0010
772+
773+
top = tkinter.Toplevel(self.root)
774+
top.geometry('200x150')
775+
if not wait_until_mapped(top):
776+
self.skipTest('the window is not mapped')
777+
sizes = []
778+
def callback(event):
779+
# Call Tcl from the handler.
780+
sizes.append((event.width, event.height, top.winfo_width()))
781+
top.bind('<Configure>', callback)
782+
hwnd = int(top.wm_frame(), 16)
783+
# Emulate resizing the window with the mouse: Tk handles the
784+
# resulting window messages in its window procedure, outside
785+
# of the Tcl event loop run by tkinter.
786+
user32.SendMessageW(hwnd, WM_ENTERSIZEMOVE, 0, 0)
787+
try:
788+
user32.SetWindowPos(hwnd, None, 0, 0, 250, 180,
789+
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE)
790+
finally:
791+
user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0)
792+
self.assertTrue(sizes)
793+
752794
def test_wait_variable(self):
753795
var = tkinter.StringVar(self.root)
754796
self.assertEqual(self.root.waitvar, self.root.wait_variable)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :mod:`tkinter` on Windows when a Tk event handler, such as
2+
a ``<Configure>`` binding, is called while another event loop is running,
3+
for example when the window is resized while a native dialog is shown.

‎Modules/_tkinter.c‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ Tkinter_TkInit(Tcl_Interp *interp)
288288
ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between
289289
ENTER_PYTHON and LEAVE_PYTHON.
290290
291+
An event handler can also be called when Tcl is not entered with
292+
ENTER_TCL, for example from a foreign event loop on Windows, which
293+
dispatches a window message to Tk while the Python interpreter lock
294+
is released. Then there is no saved thread state and the lock for
295+
Tcl is not held, so ENTER_PYTHON acquires the Python interpreter
296+
lock with PyGILState_Ensure() and LEAVE_PYTHON releases it with
297+
PyGILState_Release().
298+
291299
These locks expand to several statements and brackets; they should
292300
not be used in branches of if statements and the like.
293301
@@ -337,15 +345,27 @@ static PyThreadState *tcl_tstate = NULL;
337345
tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
338346

339347
#define ENTER_PYTHON \
340-
{ PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
341-
if(tcl_lock) \
342-
PyThread_release_lock(tcl_lock); \
343-
PyEval_RestoreThread((tstate)); }
348+
{ PyThreadState *tstate = tcl_tstate; \
349+
PyGILState_STATE gilstate = PyGILState_UNLOCKED; \
350+
if (tstate == NULL) { \
351+
gilstate = PyGILState_Ensure(); \
352+
} \
353+
else { \
354+
tcl_tstate = NULL; \
355+
if(tcl_lock)PyThread_release_lock(tcl_lock); \
356+
PyEval_RestoreThread(tstate); \
357+
}
344358

345359
#define LEAVE_PYTHON \
346-
{ PyThreadState *tstate = PyEval_SaveThread(); \
347-
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \
348-
tcl_tstate = tstate; }
360+
if (tstate == NULL) { \
361+
PyGILState_Release(gilstate); \
362+
} \
363+
else { \
364+
tstate = PyEval_SaveThread(); \
365+
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \
366+
tcl_tstate = tstate; \
367+
} \
368+
}
349369

350370
#ifndef FREECAST
351371
#define FREECAST (char *)
@@ -2589,15 +2609,6 @@ typedef struct {
25892609
PyObject *func;
25902610
} PythonCmd_ClientData;
25912611

2592-
static int
2593-
PythonCmd_Error(Tcl_Interp *interp)
2594-
{
2595-
errorInCmd = 1;
2596-
excInCmd = PyErr_GetRaisedException();
2597-
LEAVE_PYTHON
2598-
return TCL_ERROR;
2599-
}
2600-
26012612
/* This is the Tcl command that acts as a wrapper for Python
26022613
* function or method.
26032614
*/
@@ -2610,19 +2621,20 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
26102621
int i;
26112622
Tcl_Obj *obj_res;
26122623
int objargs = data->self->wantobjects >= 2;
2624+
int result = TCL_ERROR;
26132625

26142626
ENTER_PYTHON
26152627

26162628
/* Create argument tuple (objv1, ..., objvN) */
26172629
if (!(args = PyTuple_New(objc - 1)))
2618-
return PythonCmd_Error(interp);
2630+
goto error;
26192631

26202632
for (i = 0; i < (objc - 1); i++) {
26212633
PyObject *s = objargs ? FromObj(data->self, objv[i + 1])
26222634
: unicodeFromTclObj(data->self, objv[i + 1]);
26232635
if (!s) {
26242636
Py_DECREF(args);
2625-
return PythonCmd_Error(interp);
2637+
goto error;
26262638
}
26272639
PyTuple_SET_ITEM(args, i, s);
26282640
}
@@ -2631,19 +2643,25 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
26312643
Py_DECREF(args);
26322644

26332645
if (res == NULL)
2634-
return PythonCmd_Error(interp);
2646+
goto error;
26352647

26362648
obj_res = AsObj(res);
26372649
if (obj_res == NULL) {
26382650
Py_DECREF(res);
2639-
return PythonCmd_Error(interp);
2651+
goto error;
26402652
}
26412653
Tcl_SetObjResult(interp, obj_res);
26422654
Py_DECREF(res);
2655+
result = TCL_OK;
2656+
goto done;
26432657

2658+
error:
2659+
errorInCmd = 1;
2660+
excInCmd = PyErr_GetRaisedException();
2661+
done:
26442662
LEAVE_PYTHON
26452663

2646-
return TCL_OK;
2664+
return result;
26472665
}
26482666

26492667

0 commit comments

Comments
 (0)