Skip to content

gh-84256: Fix _tkinter resuming null thread state - #22453

Closed
E-Paine wants to merge 3 commits into
python:mainfrom
E-Paine:tkinter-tstate-get
Closed

E-Paine wants to merge 3 commits into
python:mainfrom
E-Paine:tkinter-tstate-get

Conversation

@E-Paine

@E-Paine E-Paine commented Sep 29, 2020 •

Copy link
Copy Markdown
Contributor

@E-Paine

E-Paine commented Sep 29, 2020

Copy link
Copy Markdown
Contributor Author

I'll now try to write a test based on @speleo3's simple_tk_demo, but this may take some time (I'm still open to review during this time, but ask it is not merged yet)

@E-Paine

E-Paine commented Sep 30, 2020

Copy link
Copy Markdown
Contributor Author

I did say "try"! In short, it cannot be reasonably done. It is difficult enough to mimic a user action (e.g. moving the window) so as to trigger the Tk event, let alone doing so from a thread that Python has never seen before. It would require a lot of ctypes calls, likely require some of the test code to be written in C and would be a pain to maintain in and of itself (hence it is not worth pursuing).

@serhiy-storchaka (or anyone else?!) would you mind please reviewing the PR?

@E-Paine E-Paine changed the title bpo-40075: Acquire GIL before getting thread state bpo-40075: Fix _tkinter resuming null thread state Sep 30, 2020
Comment thread Modules/_tkinter.c Resolved
@speleo3

speleo3 commented Dec 15, 2020

Copy link
Copy Markdown

I would give this PR more thumbs up if I could. Is there anything I can do to help this getting approved?

@E-Paine

E-Paine commented Dec 15, 2020

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka please could you review this PR (its a small, 4 line addition to ENTER_PYTHON)

@taleinat

Copy link
Copy Markdown
Contributor

@serhiy-storchaka, it would be really great to get your 👀 on this.

@pbk20191

Copy link
Copy Markdown

Any updates? It is really hard to integrate python with other eventloop framework without this.

@python-cla-bot

python-cla-bot Bot commented Apr 18, 2025 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@mdehoon

mdehoon commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

If I understand correctly, this bug appears on Windows only. If so, it would be good to better understand why the bug occurs. The fact that the bug occurs in ENTER_PYTHON does not necessarily mean that ENTER_PYTHON is the ultimate cause of the bug. I would suggest to try and find out why tstate can end up being NULL on Windows but not on other platforms.

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 8, 2026
@mdehoon

mdehoon commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

This is the call stack leading up to the crash (last command shown first):

0: PythonCmd - 0x7FFCBE425120
1: TclNRRunCallbacks - 0x7FFCA3AEABA0
2: Tcl_EvalEx - 0x7FFCA3AEBAA0
3: Tcl_EvalEx - 0x7FFCA3AEBAA0
4: Tk_BindEvent - 0x7FFCA268CB30
5: TkBindEventProc - 0x7FFCA26B5650
6: Tk_HandleEvent - 0x7FFCA26C2970
7: TkQueueEventForAllChildren - 0x7FFCA26C32D0
8: Tcl_ServiceEvent - 0x7FFCA3BB3770
9: Tcl_ServiceAll - 0x7FFCA3BB3D50
10: TkWinGetSystemPalette - 0x7FFCA2686100
11: CallWindowProcW - 0x7FFCC5FFBCF0
12: SendMessageW - 0x7FFCC5FFB150
13: GetWindowDpiAwarenessContext - 0x7FFCC6031E60
14: KiUserCallbackDispatcher - 0x7FFCC65A5B00
15: NtUserMessageCall - 0x7FFCC2F01340
16: GetSystemMetrics - 0x7FFCC5FF8AD0
17: GetSystemMetrics - 0x7FFCC5FF8AD0
18: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
19: Ordinal140 - 0x7FFCC05CC060
20: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
21: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
22: IsWindowUnicode - 0x7FFCC5FFA420
23: TkWinGetSystemPalette - 0x7FFCA2686100
24: CallWindowProcW - 0x7FFCC5FFBCF0
25: SendMessageW - 0x7FFCC5FFB150
26: GetWindowDpiAwarenessContext - 0x7FFCC6031E60
27: KiUserCallbackDispatcher - 0x7FFCC65A5B00
28: NtUserMessageCall - 0x7FFCC2F01340
29: GetSystemMetrics - 0x7FFCC5FF8AD0
30: GetSystemMetrics - 0x7FFCC5FF8AD0
31: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
32: SetWindowThemeAttribute - 0x7FFCC05C7000
33: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
34: GetImmersiveUserColorSetPreference - 0x7FFCC05871A0
35: IsWindowUnicode - 0x7FFCC5FFA420
36: TkWinGetSystemPalette - 0x7FFCA2686100
37: CallWindowProcW - 0x7FFCC5FFBCF0
38: IsWindowUnicode - 0x7FFCC5FFA420
39: IsDialogMessage - 0x7FFCC6002FE0
40: Ordinal2635 - 0x7FFCC6021400
41: SoftModalMessageBox - 0x7FFCC60229B0
42: MessageBoxIndirectA - 0x7FFCC604EE90
43: MessageBoxTimeoutW - 0x7FFCC604ECC0
44: MessageBoxW - 0x7FFCC604EC60
45: [0x7FFCAF264771]
46: [0x7FFCAF264493]
47: [0x7FFCAF2642C2]
48: [0x7FFCAC43E33A]
49: [0x7FFCAC43ED66]
50: [0x7FFCAC4387D7]
51: _PyObject_MakeTpCall - 0x7FFC4EF0E760
52: _Py_VectorCallInstrumentation_StackRefSteal - 0x7FFC4F05F460
53: _PyEval_EvalFrameDefault - 0x7FFC4F060190
54: _PyEval_Vector - 0x7FFC4F06EB20
55: PyEval_EvalCode - 0x7FFC4F05F050
56: run_eval_code_obj - 0x7FFC4F125AD0
57: pyrun_file - 0x7FFC4F125620
58: _PyRun_SimpleFileObject - 0x7FFC4F1234F0
59: _PyRun_AnyFileObject - 0x7FFC4F1223D0
60: pymain_run_file_obj - 0x7FFC4EE75000
61: pymain_run_python - 0x7FFC4EE75AD0
62: Py_RunMain - 0x7FFC4EE763B0
63: __scrt_common_main_seh - 0x7FF7AC2C1124
64: BaseThreadInitThunk - 0x7FFCC5F4E8C0
65: RtlUserThreadStart - 0x7FFCC64CC3D0

The final command, PythonCmd, uses the ENTER_PYTHON macro, which calls PyEval_RestoreThread with a NULL argument.

The ENTER_PYTHON macro should be used within an ENTER_TCL / LEAVE_TCL block, as in

ENTER_TCL
...use Tcl
ENTER_PYTHON
...use Python
LEAVE_PYTHON
...use Tcl
LEAVE_TCL

Here, ENTER_TCL will store the PyThreadState that gets restored in ENTER_PYTHON.

In the call stack, you will see that we are switching from Python (in line 51) to Tcl/Tk (in line 36) outside of an ENTER_TCL/LEAVE_TCL block. ENTER_PYTHON will then crash because the PyThreadState is not stored.

The reason this happens is that the message box runs its own event loop, which handles events both for the message box and for the tkinter window. But the message box is not aware of tkinter and its intricacies, and processes events for the tkinter window by simply calling into Tcl/Tk directly (in line 36).

Then from line 36 to line 2, we are in Tcl/Tk, which then tries to call into Python (PythonCmd on line 0). PythonCmd expects to be inside a ENTER_TCL / LEAVE_TCL block, so it uses a ENTER_PYTHON / LEAVE_PYTHON block when it needs to call into Python. Hence the crash.

A better solution to this bug would be to use an external event loop, Tcl/Tk supports external event loops, but unfortunately tkinter does not expose this part of the Tcl/Tk API.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label May 10, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 11, 2026
@serhiy-storchaka serhiy-storchaka changed the title bpo-40075: Fix _tkinter resuming null thread state gh-84256: Fix _tkinter resuming null thread state Aug 12, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 14, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Nov 13, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

Thank you for your PR, @E-Paine. It was superseded by #162547: releasing the GIL state immediately after PyGILState_Ensure() could delete the newly created thread state before it was used, so the fix now keeps it until the end of the event handler.

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

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants