-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
base: main
Are you sure you want to change the base?
Conversation
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.
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 |
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.
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)); |
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.
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); |
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.
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; |
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.
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
.)
This is my very first attempt in free-threading in cpython. So that I would be happy to get some criticism