Skip to content

gh-84256: Fix a tkinter crash when Tk calls an event handler outside of Tcl - #162547

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-84256-null-tstate
Nov 24, 2026
Merged

serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:gh-84256-null-tstate

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

On Windows, Tk can call an event handler from its window procedure when Tcl was not entered by tkinter, for example when a window message is dispatched from a foreign event loop while the GIL is released. A typical case is resizing a Tk window while a native dialog is shown or while another GUI toolkit runs the main loop. Then there is no saved thread state, and ENTER_PYTHON crashes in PyEval_RestoreThread().

ENTER_PYTHON and LEAVE_PYTHON now form a block, like ENTER_TCL and LEAVE_TCL. If no thread state was saved, ENTER_PYTHON acquires the GIL with PyGILState_Ensure() and LEAVE_PYTHON releases it with PyGILState_Release() at the end of the handler. The Tcl lock is not touched in that case, since it was not acquired.

The added test emulates resizing the window with the mouse by sending WM_ENTERSIZEMOVE and calling SetWindowPos() via ctypes. It crashes without the fix.

This supersedes #22453 by @E-Paine, which added a NULL check in ENTER_PYTHON but released the GIL state immediately, so the thread state created by PyGILState_Ensure() could be deleted before use. The analysis by @mdehoon in the issue, that the handler is entered outside of an ENTER_TCL/LEAVE_TCL block, pointed to the root cause.

🤖 Generated with Claude Code

…tside of Tcl

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>
@serhiy-storchaka serhiy-storchaka added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 24, 2026
@serhiy-storchaka
serhiy-storchaka merged commit d3d0fdd into python:main Nov 24, 2026
65 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-84256-null-tstate branch November 24, 2026 20:20
@miss-islington-app

Copy link
Copy Markdown

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.15 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker d3d0fddc01ce5076e0f33008cbef6faf2ffccd14 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker d3d0fddc01ce5076e0f33008cbef6faf2ffccd14 3.14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant