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-128100: Atomic dict load in _PyObject_GenericGetAttrWithDict #128297

Merged
merged 4 commits into from
Dec 30, 2024

Conversation

wrongnull
Copy link
Contributor

@wrongnull wrongnull commented Dec 27, 2024

This is my very first attempt in free-threading in cpython. So that I would be happy to get some criticism

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a shot at this. I haven't dove too deep into the issue so take my review with a grain of salt.

Objects/object.c Outdated
@@ -1715,10 +1715,16 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
dict = (PyObject *)_PyObject_GetManagedDict(obj);
}
else {
Py_BEGIN_CRITICAL_SECTION(obj);
#ifdef DISABLE_GIL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Py_GIL_DISABLED.

Objects/object.c Outdated
@@ -1715,10 +1715,16 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
dict = (PyObject *)_PyObject_GetManagedDict(obj);
}
else {
Py_BEGIN_CRITICAL_SECTION(obj);
#ifdef DISABLE_GIL
PyObject **dictptr = _Py_atomic_load_ptr(_PyObject_ComputedDictPointer(obj));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong thing to load atomically--dereferencing it should be the atomic operation. In fact, this doesn't do what you think it does: _Py_atomic_load* takes a pointer, and the thing that it points to gets atomically loaded and returned.

Objects/object.c Outdated
@@ -1715,10 +1715,16 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
dict = (PyObject *)_PyObject_GetManagedDict(obj);
}
else {
Py_BEGIN_CRITICAL_SECTION(obj);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we don't need to lock something if we're going to use one of the _Py_atomic APIs, and vice versa.

PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
#endif
if (dictptr) {
dict = *dictptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where the atomic load should be. (And, as Sam said in the issue, we want _Py_atomic_load_ptr_acquire instead of plain old _Py_atomic_load_ptr.)

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.

The change looks good. Let's drop the test for now as it's not catching any TSAN data races even with repeated runs before this change. We can write a better test when we address the second half of the issue.

It wasn't reporting any TSAN failures before the PR.
@colesbury colesbury enabled auto-merge (squash) December 30, 2024 20:15
@colesbury colesbury merged commit 47d2cb8 into python:main Dec 30, 2024
39 checks passed
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