Skip to content

gh-128100: Atomic dict load in _PyObject_GenericGetAttrWithDict #128297

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 4 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_free_threading/test_races.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,25 @@ def mutate():

do_race(set_value, mutate)

def test_generic_getattr(self):
"""Test generic attribute load"""
import concurrent.futures

num_threads = 100


def closure(b, o):
b.wait()
getattr(o, "foo", None)
o.foo = 42

with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
for _ in range(100):
b = threading.Barrier(num_threads)
o = functools.partial(lambda x: x, 42)
for _ in range(num_threads):
executor.submit(functools.partial(closure, b, o))


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.

#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.

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.

#else
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.)

}
Py_END_CRITICAL_SECTION();
}
}
if (dict != NULL) {
Expand Down
Loading