gh-117657: Fix race data race in `_Py_IsOwnedByCurrentThread()` by mpage · Pull Request #118258 · python/cpython · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

gh-117657: Fix race data race in _Py_IsOwnedByCurrentThread() #118258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2024

Conversation

mpage
Copy link
Contributor

@mpage mpage commented Apr 24, 2024

We need to load ob_tid atomically; it may be set to zero in another thread without any intervening operations that establish a happens-before relationship.

Using a relaxed load should be sufficient; we do not depend on the store publishing any other information.

Examples:

The load in _Py_IsOwnedByCurrentThread():

return ob->ob_tid == _Py_ThreadId();

races with the store in _Py_ExplicitMergeRefcount() (example):

_Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0);

and the store in _Py_MergeZeroLocalRefcount() (example):

_Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0);

We need to load `ob_tid` atomically; it may be set to zero in another
thread without any intervening operations that establish a happens-before
relationship.

Using a relaxed load should be sufficient; we do not depend on the store
publishing any other information.

Examples:

The load in `_Py_IsOwnedByCurrentThread()`:

https://github.com/python/cpython/blob/4b10e209c76f9f36f8ae2e4d713b3a01591c1856/Include/object.h#L306

races with the store in `_Py_ExplicitMergeRefcount()` ([example](https://gist.github.com/mpage/0342ebce4b5b446689c41cdd72f16e61)):

https://github.com/python/cpython/blob/4b10e209c76f9f36f8ae2e4d713b3a01591c1856/Objects/object.c#L421

and the store in `_Py_MergeZeroLocalRefcount()` ([example](https://gist.github.com/mpage/eaec11eda6e2d988ed0dd2bcfd6d606b)):

https://github.com/python/cpython/blob/4b10e209c76f9f36f8ae2e4d713b3a01591c1856/Objects/object.c#L378
@mpage mpage requested a review from colesbury April 25, 2024 21:52
@colesbury colesbury merged commit a5eeb83 into python:main Apr 26, 2024
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

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.