Skip to content

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

Merged
colesbury merged 6 commits into
python:mainfrom
yoney:watcher
May 11, 2026
Merged

gh-145235: Make dict watcher API thread-safe for free-threaded builds#145233
colesbury merged 6 commits into
python: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
@colesbury colesbury added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 11, 2026
@colesbury colesbury merged commit 8a48959 into python:main May 11, 2026
72 of 73 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @yoney for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

GH-149690 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 11, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 11, 2026

GH-149691 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 11, 2026
colesbury added a commit that referenced this pull request May 11, 2026
… builds (gh-145233) (#149690)

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.

(cherry picked from commit 8a48959)

Co-authored-by: Alper <alperyoney@fb.com>
colesbury added a commit that referenced this pull request May 12, 2026
… builds (gh-145233) (#149691)

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.

(cherry picked from commit 8a48959)

Co-authored-by: Alper <alperyoney@fb.com>
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