Skip to content

PyDict_Watch and PyDict_Unwatch should use atomic operations in the free threading build #148393

@colesbury

Description

@colesbury

Bug report

Currently, there is a data race when PyDict_Watch/PyDict_Unwatch is called on a dict that another thread may concurrently mutate: the read of _ma_watcher_tag in _PyDict_NotifyEvent (on the mutation path) races with the modification by PyDict_Watch/PyDict_Unwatch. Additionally, concurrent PyDict_Watch/PyDict_Unwatch calls on the same dict (e.g. from different watcher IDs) race with each other, since both are non-atomic read-modify-write operations on _ma_watcher_tag.

In the free threading build, we should use atomic operations for the read of _ma_watcher_tag in _PyDict_NotifyEvent and for the updates performed by PyDict_Watch/PyDict_Unwatch.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions