Skip to content

gh-145235: Make dict watcher API thread-safe for free-threaded builds#145233

Open
yoney wants to merge 5 commits intopython:mainfrom
yoney:watcher
Open

gh-145235: Make dict watcher API thread-safe for free-threaded builds#145233
yoney wants to merge 5 commits intopython:mainfrom
yoney:watcher

Conversation

@yoney
Copy link
Copy Markdown
Contributor

@yoney yoney commented Feb 25, 2026

In free-threaded builds, concurrent calls to PyDict_AddWatcher, PyDict_ClearWatcher, PyDict_Watch, and PyDict_Unwatch can race on the shared callback array and the per-dict watcher tags. This change adds a mutex to serialize watcher registration and removal, atomic operations for tag updates, and atomic acquire/release synchronization for callback dispatch in _PyDict_SendEvent.

Note: Before this change, running test.test_free_threading.test_dict_watcher emitted tsan warnings in the free-threaded tsan build.

cc: @colesbury @mpage @DinoV

@yoney yoney changed the title Make dict watcher API thread-safe for free-threaded builds gh-145235: Make dict watcher API thread-safe for free-threaded builds Feb 25, 2026
Comment thread Include/internal/pycore_dict_state.h Outdated
Comment thread Objects/dictobject.c Outdated
Comment thread Objects/dictobject.c Outdated
Comment thread Objects/dictobject.c Outdated
Comment thread Include/internal/pycore_dict_state.h Outdated
Comment thread Objects/dictobject.c
Comment thread Python/optimizer_analysis.c Outdated
@yoney
Copy link
Copy Markdown
Contributor Author

yoney commented Feb 26, 2026

@colesbury Thank you for the review. I’ve addressed the comments.

Copy link
Copy Markdown
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

LGTM

@yoney
Copy link
Copy Markdown
Contributor Author

yoney commented Feb 27, 2026

Could we please add the needs backport to 3.14 label before merging? Thank you.

@colesbury colesbury added the needs backport to 3.14 bugs and security fixes label Feb 27, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

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 May 7, 2026
@colesbury colesbury requested a review from FFY00 as a code owner May 7, 2026 17:02
@colesbury colesbury removed the stale Stale PR or inactive for long period of time. label May 7, 2026
@colesbury colesbury self-assigned this May 7, 2026
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.

3 participants