8000 gh-127266: avoid data races when updating type slots by nascheme · Pull Request #131174 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127266: avoid data races when updating type slots #131174

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 54 commits into from
Apr 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
0e995ad
wip: update type slots, stop-the-world
nascheme Mar 12, 2025
b173f17
Remove unneeded atomics for tp_flags.
nascheme Mar 13, 2025
d4ce112
Use stop-the-world for tp_flag changes too.
nascheme Mar 13, 2025
44eb332
Remove 'world_stops' and 'sys._get_world_stops'.
nascheme Mar 14, 2025
d132fab
Improve code comments.
nascheme Mar 14, 2025
658bcd5
Remove TSAN suppressions that seem unneeded.
nascheme Mar 14, 2025
ef2f07b
Add NEWS file.
nascheme Mar 14, 2025
ce8536d
Use mutex rather than critical sections.
nascheme Mar 22, 2025
b97a4b4
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Mar 26, 2025
ca00e74
Fix non-debug build.
nascheme Mar 26, 2025
6db4542
Improve comments.
nascheme Mar 26, 2025
398ac14
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Mar 27, 2025
75d6b71
Avoid unused function warning.
nascheme Mar 27, 2025
895a86a
Remove unwanted suppression (bad merge).
nascheme Mar 31, 2025
65e40f4
Fixes based on review feedback.
nascheme Mar 31, 2025
b68f1a1
Remove spurious assert().
nascheme Mar 31, 2025
9976b32
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 1, 2025
1b84486
Omit mutex_tid member from default build.
nascheme Apr 1, 2025
2af9e49
Remove Py_TPFLAGS_EXPOSED flag and related logic.
nascheme Apr 1, 2025
f65e87c
Improve comment for TYPE_LOCK.
nascheme Apr 1, 2025
395a6d3
Re-add the ASSERT_NEW_OR_STOPPED() asserts.
nascheme Apr 1, 2025
e4f87e5
Further cleanups of the locking in typeobject.
nascheme Apr 3, 2025
2a66555
Fix data race in resolve_slotdups().
nascheme Apr 3, 2025
2efac26
Fix comment.
nascheme Apr 3, 2025
f7d2d36
Make the init of tp_dict thread-safe.
nascheme Apr 9, 2025
f3fd35a
Do some addtional locking simplification.
nascheme Apr 9, 2025
57c2a44
Avoid acquiring the types mutex if version is set.
nascheme Apr 10, 2025
7c0ccf5
Add check_invalid_reentrancy() call.
nascheme Apr 17, 2025
4fa77bb
Fix additional re-entrancy issues found.
nascheme Apr 17, 2025
caf6554
Add comment explaining class_name() code.
nascheme Apr 17, 2025
803d703
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 17, 2025
90ea541
Use atomic load to avoid thread safety issue.
nascheme Apr 17, 2025
0dc0faf
Fix default debug build.
nascheme Apr 17, 2025
956e5d1
Move declaration to avoid syntax error.
nascheme Apr 17, 2025
2bb710c
Remove _PyType_GetVersionForCurrentState, unused.
nascheme Apr 21, 2025
3a9bc96
Fix for possible re-entrancy in has_custom_mro().
nascheme Apr 21, 2025
d742a53
Use correct FT_ATOMIC_ macro.
nascheme Apr 21, 2025
e7480c3
Remove TSAN suppression for 'assign_version_tag'.
nascheme Apr 21, 2025
e2ea281
Small efficiency fix for types_mutex_set_owned().
nascheme Apr 21, 2025
935bfca
Revert to using critical section with TYPE_LOCK.
nascheme Apr 21, 2025
1cff448
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 21, 2025
a81e9e3
Invalidate type cache before calling watchers.
nascheme Apr 21, 2025
f5df0c3
Fixes for type_modified_unlocked().
nascheme Apr 22, 2025
7db281c
Major re-work, TYPE_LOCK protects more things.
nascheme Apr 22, 2025
da2a0ad
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 23, 2025
986f23a
Fix non-debug build.
nascheme Apr 23, 2025
c404ed4
Revert unneeded code changes.
nascheme Apr 23, 2025
55af4ba
Merge branch 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 23, 2025
0eb77da
Restore comment
nascheme Apr 24, 2025
16f15b2
Revert more changes.
nascheme Apr 24, 2025
0c328cc
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 25, 2025
64547e9
Reduce item list size for a few tests.
nascheme Apr 25, 2025
fff1bd2
Merge 'origin/main' into gh-127266-type-slots-ts
nascheme Apr 28, 2025
5672352
Minor code tidy.
nascheme Apr 28, 2025
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
Revert more changes.
These are not required as part of this PR and can be reviewed
separately.
  • Loading branch information
nascheme committed Apr 24, 2025
commit 16f15b2e3b62c8c95694796293927bfbbbacf183
128 changes: 52 additions & 76 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1156,14 +1156,6 @@ type_modified_unlocked(PyTypeObject *type)
}
}

set_version_unlocked(type, 0); /* 0 is not a valid version tag */
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
FT_ATOMIC_STORE_PTR_RELAXED(
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
}

// Notify registered type watchers, if any
if (type->tp_watched) {
PyInterpreterState *interp = _PyInterpreterState_GET();
Expand All @@ -1185,6 +1177,14 @@ type_modified_unlocked(PyTypeObject *type)
bits >>= 1;
}
}

set_version_unlocked(type, 0); /* 0 is not a valid version tag */
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
FT_ATOMIC_STORE_PTR_RELAXED(
((PyHeapTypeObject *)type)->_spec_cache.getitem, NULL);
}
}

void
Expand Down Expand Up @@ -1238,21 +1238,14 @@ type_mro_modified(PyTypeObject *type, PyObject *bases)
Called from mro_internal, which will subsequently be called on
each subclass when their mro is recursively updated.
*/
Py_ssize_t i, n;

ASSERT_TYPE_LOCK_HELD();
if (!Py_IS_TYPE(type, &PyType_Type)) {
unsigned int meta_version = Py_TYPE(type)->tp_version_tag;
// This can be re-entrant.
bool is_custom = has_custom_mro(type);
if (meta_version != Py_TYPE(type)->tp_version_tag) {
// metaclass changed during call of has_custom_mro()
goto clear;
}
if (is_custom) {
goto clear;
}
if (!Py_IS_TYPE(type, &PyType_Type) && has_custom_mro(type)) {
goto clear;
}
Py_ssize_t n = PyTuple_GET_SIZE(bases);
for (Py_ssize_t i = 0; i < n; i++) {
n = PyTuple_GET_SIZE(bases);
for (i = 0; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i);
PyTypeObject *cls = _PyType_CAST(b);

Expand Down Expand Up @@ -5771,22 +5764,12 @@ update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int versio
return old_name;
}

#if Py_GIL_DISABLED

static void
maybe_update_cache(PyTypeObject *type, struct type_cache_entry *entry,
PyObject *name, unsigned int version_tag, PyObject *value)
update_cache_gil_disabled(struct type_cache_entry *entry, PyObject *name,
unsigned int version_tag, PyObject *value)
{
if (version_tag == 0) {
return; // 0 is not a valid version
}
// Calling find_name_in_mro() might cause the type version to change.
// For example, if a __hash__ or __eq__ method mutates the types.
// This case is expected to be rare but we check for 8000 it here and avoid
// replacing a valid cache entry with a known to be stale one.
if (FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag) != version_tag) {
return; // version changed during lookup
}
PyObject *old_value;
#ifdef Py_GIL_DISABLED
_PySeqLock_LockWrite(&entry->sequence);

// update the entry
Expand All @@ -5798,16 +5781,16 @@ maybe_update_cache(PyTypeObject *type, struct type_cache_entry *entry,
return;
}

old_value = update_cache(entry, name, version_tag, value);
PyObject *old_value = update_cache(entry, name, version_tag, value);

// Then update sequence to the next valid value
_PySeqLock_UnlockWrite(&entry->sequence);
#else
old_value = update_cache(entry, name, version_tag, value);
#endif

Py_DECREF(old_value);
}

#endif

void
_PyTypes_AfterFork(void)
{
Expand All @@ -5825,22 +5808,23 @@ _PyTypes_AfterFork(void)
#endif
}

// Try to assign a new type version tag, return it if successful. Return 0
// if no version was assigned.
static unsigned int
type_assign_version(PyTypeObject *type)
/* Internal API to look for a name through the MRO.
This returns a strong reference, and doesn't set an exception!
If nonzero, version is set to the value of type->tp_version at the time of
the lookup.
*/
PyObject *
_PyType_LookupRefAndVersion(PyTypeObject *type, PyObject *name, unsigned int *version)
{
unsigned int version = type->tp_version_tag;
if (version == 0) {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (assign_version_tag(interp, type)) {
version = type->tp_version_tag;
}
else {
version = 0;
}
_PyStackRef out;
unsigned int ver = _PyType_LookupStackRefAndVersion(type, name, &out);
if (version) {
*version = ver;
}
return version;
if (PyStackRef_IsNull(out)) {
return NULL;
}
return PyStackRef_AsPyObjectSteal(out);
}

unsigned int
Expand Down Expand Up @@ -5896,12 +5880,15 @@ _PyType_LookupStackRefAndVersion(PyTypeObject *type, PyObject *name, _PyStackRef

PyObject *res;
int error;
unsigned int assigned_version = 0; // 0 is not a valid version
PyInterpreterState *interp = _PyInterpreterState_GET();
int has_version = 0;
unsigned int assigned_version = 0;
BEGIN_TYPE_LOCK();
res = find_name_in_mro(type, name, &error);
if (MCACHE_CACHEABLE_NAME(name)) {
assigned_version = type_assign_version(type);
has_version = assign_version_tag(interp, type);
assigned_version = type->tp_version_tag;
}
res = find_name_in_mro(type, name, &error);
END_TYPE_LOCK();

/* Only put NULL results into cache if there was no error. */
Expand All @@ -5920,28 +5907,17 @@ _PyType_LookupStackRefAndVersion(PyTypeObject *type, PyObject *name, _PyStackRef
*out = PyStackRef_NULL;
return 0;
}
maybe_update_cache(type, entry, name, assigned_version, res);
*out = res ? PyStackRef_FromPyObjectSteal(res) : PyStackRef_NULL;
return assigned_version;
}

/* Internal API to look for a name through the MRO.
This returns a strong reference, and doesn't set an exception!
If nonzero, version is set to the value of type->tp_version at the time of
the lookup.
*/
PyObject *
_PyType_LookupRefAndVersion(PyTypeObject *type, PyObject *name, unsigned int *version)
{
_PyStackRef out;
unsigned int ver = _PyType_LookupStackRefAndVersion(type, name, &out);
if (version) {
*version = ver;
}
if (PyStackRef_IsNull(out)) {
return NULL;
if (has_version) {
#if Py_GIL_DISABLED
update_cache_gil_disabled(entry, name, assigned_version, res);
#else
PyObject *old_value = update_cache(entry, name, assigned_version, res);
Py_DECREF(old_value);
#endif
}
return PyStackRef_AsPyObjectSteal(out);
*out = res ? PyStackRef_FromPyObjectSteal(res) : PyStackRef_NULL;
return has_version ? assigned_version : 0;
}

/* Internal API to look for a name through the MRO.
Expand Down
Loading
0