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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,11 @@ struct _Py_interp_cached_objects {

/* object.__reduce__ */
PyObject *objreduce;
#ifndef Py_GIL_DISABLED
/* resolve_slotdups() */
PyObject *type_slots_pname;
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];
#endif

/* TypeVar and related types */
PyTypeObject *generic_type;
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ extern int _PyDict_CheckConsistency(PyObject *mp, int check_content);
// Fast inlined version of PyType_HasFeature()
static inline int
_PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
return ((FT_ATOMIC_LOAD_ULONG_RELAXED(type->tp_flags) & feature) != 0);
return ((type->tp_flags) & feature) != 0;
}

extern void _PyType_InitCache(PyInterpreterState *interp);
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ extern int _PyType_AddMethod(PyTypeObject *, PyMethodDef *);
extern void _PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask,
unsigned long flags);

extern unsigned int _PyType_GetVersionForCurrentState(PyTypeObject *tp);
PyAPI_FUNC(void) _PyType_SetVersion(PyTypeObject *tp, unsigned int version);
PyTypeObject *_PyType_LookupByVersion(unsigned int version);

Expand Down
12 changes: 7 additions & 5 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ given type object has a specified feature.
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)

// Flag values for ob_flags (16 bits available, if SIZEOF_VOID_P > 4).
#define _Py_IMMORTAL_FLAGS (1 << 0)
#define _Py_STATICALLY_ALLOCATED_FLAG (1 << 2)
#if defined(Py_GIL_DISABLED) && defined(Py_DEBUG)
#define _Py_TYPE_REVEALED_FLAG (1 << 3)
#endif

#define Py_CONSTANT_NONE 0
#define Py_CONSTANT_FALSE 1
Expand Down Expand Up @@ -776,11 +782,7 @@ PyType_HasFeature(PyTypeObject *type, unsigned long feature)
// PyTypeObject is opaque in the limited C API
flags = PyType_GetFlags(type);
#else
# ifdef Py_GIL_DISABLED
flags = _Py_atomic_load_ulong_relaxed(&type->tp_flags);
# else
flags = type->tp_flags;
# endif
flags = type->tp_flags;
#endif
return ((flags & feature) != 0);
}
Expand Down
3 changes: 0 additions & 3 deletions Include/refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ immortal. The latter should be the only instances that require
cleanup during runtime finalization.
*/

#define _Py_STATICALLY_ALLOCATED_FLAG 4
#define _Py_IMMORTAL_FLAGS 1

#if SIZEOF_VOID_P > 4
/*
In 64+ bit systems, any object whose 32 bit reference count is >= 2**31
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ class TestRacesDoNotCrash(TestBase):
# Careful with these. Bigger numbers have a higher chance of catching bugs,
# but you can also burn through a *ton* of type/dict/function versions:
ITEMS = 1000
SMALL_ITEMS = 100
LOOPS = 4
WRITERS = 2

Expand Down Expand Up @@ -609,7 +610,7 @@ class C:
__getitem__ = lambda self, item: None

items = []
for _ in range(self.ITEMS):
for _ in range(self.SMALL_ITEMS):
item = C()
items.append(item)
return items
Expand Down Expand Up @@ -780,7 +781,7 @@ class C:
__getattribute__ = lambda self, name: None

items = []
for _ in range(self.ITEMS):
for _ in range(self.SMALL_ITEMS):
item = C()
items.append(item)
return items
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
In the free-threaded build, avoid data races caused by updating type slots
or type flags after the type was initially created. For those (typically
rare) cases, use the stop-the-world mechanism. Remove the use of atomics
when reading or writing type flags. The use of atomics is not sufficient to
avoid races (since flags are sometimes read without a lock and without
atomics) and are no longer required.
Loading
Loading