Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.1k
gh-145235: Make dict watcher API thread-safe for free-threaded builds#145233
gh-145235: Make dict watcher API thread-safe for free-threaded builds#145233yoney wants to merge 3 commits intopython:mainfrom
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Python/optimizer_analysis.c Outdated
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| if (interp->dict_state.watchers[GLOBALS_WATCHER_ID] == NULL) { | ||
| interp->dict_state.watchers[GLOBALS_WATCHER_ID] = globals_watcher_callback; | ||
| if (FT_ATOMIC_LOAD_PTR_RELAXED(interp->dict_state.watchers[GLOBALS_WATCHER_ID]) == NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
A_PyOnceFlag (maybe in JitOptContext) might be a better fit here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
After looking into it (with Claude),JitOptContext seems to be per-optimization-run so_PyOnceFlag might not the right place. I put it in_Py_dict_state as I couldn't find a better place. It fits in the struct packing without extra space.
yoney commentedFeb 26, 2026
@colesbury Thank you for the review. I’ve addressed the comments. |
Uh oh!
There was an error while loading.Please reload this page.
In free-threaded builds, concurrent calls to
PyDict_AddWatcher,PyDict_ClearWatcher,PyDict_Watch, andPyDict_Unwatchcan 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_watcheremitted tsan warnings in the free-threaded tsan build.cc:@colesbury@mpage@DinoV