8000 gh-112075: Fix race in constructing dict for instance by DinoV · Pull Request #118499 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112075: Fix race in constructing dict for instance #118499

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
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Simplify incref's around new_dict_with_shared_keys, fix stats
Lock obj instead of using atomic, and fix reads/writes
  • Loading branch information
DinoV committed May 6, 2024
commit f3f3445bdbb67c71eba0bf5acdcfeb66296bee85
16 changes: 4 additions & 12 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,9 @@ new_dict_with_shared_keys(PyInterpreterState *interp, PyDictKeysObject *keys)
size_t size = shared_keys_usable_size(keys);
PyDictValues *values = new_values(size);
if (values == NULL) {
dictkeys_decref(interp, keys, false);
return PyErr_NoMemory();
}
dictkeys_incref(keys);
for (size_t i = 0; i < size; i++) {
values->values[i] = NULL;
}
Expand Down Expand Up @@ -6693,8 +6693,6 @@ materialize_managed_dict_lock_held(PyObject *obj)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(obj);

OBJECT_STAT_INC(dict_materialized_on_request);

PyDictValues *values = _PyObject_InlineValues(obj);
PyInterpreterState *interp = _PyInterpreterState_GET();
PyDictKeysObject *keys = CACHED_KEYS(Py_TYPE(obj));
Expand Down Expand Up @@ -7205,8 +7203,6 @@ ensure_managed_dict(PyObject *obj)
goto done;
}
#endif
OBJECT_STAT_INC(dict_materialized_on_request);
dictkeys_incref(CACHED_KEYS(tp));
dict = (PyDictObject *)new_dict_with_shared_keys(_PyInterpreterState_GET(),
CACHED_KEYS(tp));
FT_ATOMIC_STORE_PTR_RELEASE(_PyObject_ManagedDictPointer(obj)->dict,
Expand All @@ -7226,7 +7222,7 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
{
PyDictKeysObject *cached;

PyObject *dict = FT_ATOMIC_LOAD_PTR_RELAXED(*dictptr);
PyObject *dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr);
if (dict == NULL) {
#ifdef Py_GIL_DISABLED
Py_BEGIN_CRITICAL_SECTION(obj);
Expand All @@ -7236,19 +7232,15 @@ ensure_nonmanaged_dict(PyObject *obj, PyObject **dictptr)
}
#endif
PyTypeObject *tp = Py_TYPE(obj);
if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
if (_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
PyInterpreterState *interp = _PyInterpreterState_GET();
assert(!_PyType_HasFeature(tp, Py_TPFLAGS_INLINE_VALUES));
dictkeys_incref(cached);
dict = new_dict_with_shared_keys(interp, cached);
if (dict == NULL) {
dictkeys_decref(interp, cached, false);
}
}
else {
dict = PyDict_New();
}
FT_ATOMIC_STORE_PTR_RELAXED(*dictptr, dict);
FT_ATOMIC_STORE_PTR_RELEASE(*dictptr, dict);
#ifdef Py_GIL_DISABLED
done:
Py_END_CRITICAL_SECTION();
Expand Down
6 changes: 2 additions & 4 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1789,11 +1789,9 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
"not a '%.200s'", Py_TYPE(value)->tp_name);
return -1;
}
#ifdef Py_GIL_DISABLED
Py_XDECREF(_Py_atomic_exchange_ptr(dictptr, Py_NewRef(value)));
#else
Py_BEGIN_CRITICAL_SECTION(obj);
Py_XSETREF(*dictptr, Py_NewRef(value));
#endif
Py_END_CRITICAL_SECTION();
return 0;
}

Expand Down
0