Skip to content

Commit 0ea9854

Browse files
[3.15] gh-84256: Fix a tkinter crash when Tk calls an event handler outside of Tcl (GH-162547) (GH-162586)
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. (cherry picked from commit d3d0fdd) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d8af327 commit 0ea9854

3 files changed

Lines changed: 85 additions & 22 deletions

File tree

‎Lib/test/test_tkinter/test_misc.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
from tkinter import TclError
1212
import enum
1313
from test import support
14+
from test.support import import_helper
1415
from test.support import os_helper
1516
from test.support.isolation import runInSubprocess
1617
from test.support.script_helper import assert_python_ok
1718
from test.test_tkinter.support import setUpModule # noqa: F401
1819
from test.test_tkinter.support import (AbstractTkTest, AbstractDefaultRootTest,
1920
requires_tk, get_tk_patchlevel,
20-
tcl_version)
21+
tcl_version, wait_until_mapped)
2122

2223
support.requires('gui')
2324

@@ -555,6 +556,47 @@ def test_tk_bisque(self):
555556
self.assertEqual(root['background'], '#ffe4c4')
556557
self.assertRaises(TypeError, root.tk_bisque, 'x')
557558

559+
@unittest.skipUnless(sys.platform == 'win32', 'requires Windows')
560+
def test_callback_from_foreign_event_loop(self):
561+
# gh-84256: Tk can call an event handler when Tcl was not entered
562+
# by tkinter, for example when a window message is dispatched
563+
# from a foreign event loop while the GIL is released.
564+
ctypes = import_helper.import_module('ctypes')
565+
from ctypes import wintypes
566+
user32 = ctypes.WinDLL('user32')
567+
user32.SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT,
568+
wintypes.WPARAM, wintypes.LPARAM]
569+
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND,
570+
ctypes.c_int, ctypes.c_int,
571+
ctypes.c_int, ctypes.c_int,
572+
wintypes.UINT]
573+
WM_ENTERSIZEMOVE = 0x0231
574+
WM_EXITSIZEMOVE = 0x0232
575+
SWP_NOMOVE = 0x0002
576+
SWP_NOZORDER = 0x0004
577+
SWP_NOACTIVATE = 0x0010
578+
579+
top = tkinter.Toplevel(self.root)
580+
top.geometry('200x150')
581+
if not wait_until_mapped(top):
582+
self.skipTest('the window is not mapped')
583+
sizes = []
584+
def callback(event):
585+
# Call Tcl from the handler.
586+
sizes.append((event.width, event.height, top.winfo_width()))
587+
top.bind('<Configure>', callback)
588+
hwnd = int(top.wm_frame(), 16)
589+
# Emulate resizing the window with the mouse: Tk handles the
590+
# resulting window messages in its window procedure, outside
591+
# of the Tcl event loop run by tkinter.
592+
user32.SendMessageW(hwnd, WM_ENTERSIZEMOVE, 0, 0)
593+
try:
594+
user32.SetWindowPos(hwnd, None, 0, 0, 250, 180,
595+
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE)
596+
finally:
597+
user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0)
598+
self.assertTrue(sizes)
599+
558600
def test_wait_variable(self):
559601
var = tkinter.StringVar(self.root)
560602
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 *)
@@ -2451,15 +2471,6 @@ typedef struct {
24512471
PyObject *func;
24522472
} PythonCmd_ClientData;
24532473

2454-
static int
2455-
PythonCmd_Error(Tcl_Interp *interp)
2456-
{
2457-
errorInCmd = 1;
2458-
excInCmd = PyErr_GetRaisedException();
2459-
LEAVE_PYTHON
2460-
return TCL_ERROR;
2461-
}
2462-
24632474
/* This is the Tcl command that acts as a wrapper for Python
24642475
* function or method.
24652476
*/
@@ -2472,19 +2483,20 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
24722483
int i;
24732484
Tcl_Obj *obj_res;
24742485
int objargs = data->self->wantobjects >= 2;
2486+
int result = TCL_ERROR;
24752487

24762488
ENTER_PYTHON
24772489

24782490
/* Create argument tuple (objv1, ..., objvN) */
24792491
if (!(args = PyTuple_New(objc - 1)))
2480-
return PythonCmd_Error(interp);
2492+
goto error;
24812493

24822494
for (i = 0; i < (objc - 1); i++) {
24832495
PyObject *s = objargs ? FromObj(data->self, objv[i + 1])
24842496
: unicodeFromTclObj(data->self, objv[i + 1]);
24852497
if (!s) {
24862498
Py_DECREF(args);
2487-
return PythonCmd_Error(interp);
2499+
goto error;
24882500
}
24892501
PyTuple_SET_ITEM(args, i, s);
24902502
}
@@ -2493,19 +2505,25 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp,
24932505
Py_DECREF(args);
24942506

24952507
if (res == NULL)
2496-
return PythonCmd_Error(interp);
2508+
goto error;
24972509

24982510
obj_res = AsObj(res);
24992511
if (obj_res == NULL) {
25002512
Py_DECREF(res);
2501-
return PythonCmd_Error(interp);
2513+
goto error;
25022514
}
25032515
Tcl_SetObjResult(interp, obj_res);
25042516
Py_DECREF(res);
2517+
result = TCL_OK;
2518+
goto done;
25052519

2520+
error:
2521+
errorInCmd = 1;
2522+
excInCmd = PyErr_GetRaisedException();
2523+
done:
25062524
LEAVE_PYTHON
25072525

2508-
return TCL_OK;
2526+
return result;
25092527
}
25102528

25112529

0 commit comments

Comments
 (0)