Skip to content
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

gh-111926: Make weakrefs thread-safe in free-threaded builds #117168

Merged
merged 67 commits into from Apr 8, 2024

Conversation

mpage
Copy link
Contributor

@mpage mpage commented Mar 22, 2024

This makes weakrefs thread-safe in free-threaded builds. Please see the comment at the beginning of Objects/weakrefobject.c for an explanation of the approach.

Note that this only affects free-threaded builds. Apart from some minor refactoring, the added code is all either gated by ifdefs or is a no-op (e.g. Py_BEGIN_CRITICAL_SECTION).

The `is_dead` check from the default build is insufficient, since the
refcount may go to zero between when we check and when we incref. More
generally, `Py_NewRef` is unsafe to use if you have a borrowed refernce.
Locking is handled in the implementation
@mpage
Copy link
Contributor Author

mpage commented Mar 29, 2024

@colesbury - Would you take another look at this when you get a chance, please?

@mpage mpage requested a review from colesbury March 29, 2024 21:48
Copy link
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.

This is looking good. Some comments inline.

Additionally, proxy_dealloc should remove the redundant PyObject_GC_UnTrack inside the if statement. It's unnecessary and the plain read of self->wr_callback is possibly a data race.

Modules/_sqlite/blob.c Show resolved Hide resolved
Modules/_sqlite/connection.c Outdated Show resolved Hide resolved
Modules/_weakref.c Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Objects/weakrefobject.c Outdated Show resolved Hide resolved
Include/cpython/weakrefobject.h Show resolved Hide resolved
@colesbury colesbury added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 1, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit fece88d 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 1, 2024
@mpage mpage requested a review from colesbury April 2, 2024 19:44
Copy link
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

Python/pystate.c Outdated Show resolved Hide resolved
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@colesbury colesbury added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 8, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @colesbury for commit 73466bf 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 8, 2024
@colesbury colesbury merged commit df73179 into python:main Apr 8, 2024
36 of 39 checks passed
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
…ython#117168)

Most mutable data is protected by a striped lock that is keyed on the
referenced object's address. The weakref's hash is protected using the
weakref's per-object lock.
 
Note that this only affects free-threaded builds. Apart from some minor
refactoring, the added code is all either gated by `ifdef`s or is a no-op
(e.g. `Py_BEGIN_CRITICAL_SECTION`).
@hugovk
Copy link
Member

hugovk commented Apr 26, 2024

I've started seeing a test failure that bisects to this PR. Please see issue #118331.

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.

None yet

5 participants