Skip to content

gh-153020: Fix race condition on event_tstate in _tkinter#153640

Open
tangyuan0821 wants to merge 2 commits into
python:mainfrom
tangyuan0821:153020
Open

gh-153020: Fix race condition on event_tstate in _tkinter#153640
tangyuan0821 wants to merge 2 commits into
python:mainfrom
tangyuan0821:153020

Conversation

@tangyuan0821

@tangyuan0821 tangyuan0821 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

event_tstate in Modules/_tkinter.c is read by the EventHook thread and
written by the main thread, so they race. I load and store it with atomic
ops, and clear it to NULL in DisableEventHook before the PyThreadState is
released.

@ByteFlowing1337 ByteFlowing1337 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the race is only possible in FT, why not use macros like FT_ATOMIC_LOAD_PTR or FT_ATOMIC_STORE_PTR to avoid the potential performance overhead in the default build?

@tangyuan0821

Copy link
Copy Markdown
Contributor Author

If the race is only possible in FT, why not use macros like FT_ATOMIC_LOAD_PTR or FT_ATOMIC_STORE_PTR to avoid the potential performance overhead in the default build?

Good catch, switched to FT_ATOMIC_* since the race is only under free-threading. The default build just reads and writes it directly.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants