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
Show file tree
Hide file tree
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
8000 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
8000
Next Next commit
wip: update type slots, stop-the-world
  • Loading branch information
nascheme committed Mar 13, 2025
commit 0e995ad361ec91755581bb06693fba9d0bf6c49e
1 change: 1 addition & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct _stoptheworld_state {
bool requested; // Set when a pause is requested.
bool world_stopped; // Set when the world is stopped.
bool is_global; // Set when contained in PyRuntime struct.
Py_ssize_t world_stops; // Number of times the world was stopped.

PyEvent stop_event; // Set when thread_countdown reaches zero.
Py_ssize_t thread_countdown; // Number of threads that must pause.
Expand Down
53 changes: 53 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3490,6 +3490,9 @@ static int update_slot(PyTypeObject *, PyObject *);
static void fixup_slot_dispatchers(PyTypeObject *);
static int type_new_set_names(PyTypeObject *);
static int type_new_init_subclass(PyTypeObject *, PyObject *);
#ifdef Py_GIL_DISABLED
static bool has_slotdef(PyObject *);
#endif

/*
* Helpers for __dict__ descriptor. We don't want to expose the dicts
Expand Down Expand Up @@ -5943,6 +5946,23 @@ _Py_type_getattro(PyObject *tp, PyObject *name)
return _Py_type_getattro_impl(type, name, NULL);
}

#ifdef Py_GIL_DISABLED
static int
update_slot_world_stopped(PyTypeObject *type, PyObject *name)
{
// Modification of type slots is protected by the global type
// lock. However, type slots are read non-atomically without holding the
// type lock. So, we need to stop-the-world while modifying slots, in
// order to avoid data races. This is unfortunately quite expensive.
int ret;
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
ret = update_slot(type, name);
_PyEval_StartTheWorld(interp);
return ret;
}
#endif

static int
type_update_dict(PyTypeObject *type, PyDictObject *dict, PyObject *name,
PyObject *value, PyObject **old_value)
Expand Down Expand Up @@ -5972,9 +5992,15 @@ type_update_dict(PyTypeObject *type, PyDictObject *dict, PyObject *name,
return -1;
}

#if Py_GIL_DISABLED
if (is_dunder_name(name) && has_slotdef(name)) {
return update_slot_world_stopped(type, name);
}
#else
if (is_dunder_name(name)) {
return update_slot(type, name);
}
#endif

return 0;
}
Expand Down Expand Up @@ -11002,6 +11028,21 @@ resolve_slotdups(PyTypeObject *type, PyObject *name)
#undef ptrs
}

#ifdef Py_GIL_DISABLED
// Return true if "name" corresponds to at least one slot definition. This is
// used to avoid calling update_slot() if is_dunder_name() is true but it's
// not actually a slot.
static bool
has_slotdef(PyObject *name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I built a temp dictionary on main interp init that can be used for this. In final version I didn't store it in interp state but maybe it is worth to store it (I have intermediate commits for this - bac95a5, b0ad875, 79a165d)
JFYI.

{
for (pytype_slotdef *p = slotdefs; p->name_strobj; p++) {
if (p->name_strobj == name) {
return true;
}
}
return false;
}
#endif

/* Common code for update_slots_callback() and fixup_slot_dispatchers().
*
Expand Down Expand Up @@ -11241,20 +11282,32 @@ fixup_slot_dispatchers(PyTypeObject *type)
END_TYPE_LOCK();
}

// Called when __bases__ is re-assigned.
static void
update_all_slots(PyTypeObject* type)
{
pytype_slotdef *p;

ASSERT_TYPE_LOCK_HELD();

// Similar to update_slot_world_stopped(), this is required to
// avoid races. We do it once here rather than once per-slot.
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PyEval_StopTheWorld() is always defined and is a no-op in the default build.

#endif

/* Clear the VALID_VERSION flag of 'type' and all its subclasses. */
type_modified_unlocked(type);

for (p = slotdefs; p->name; p++) {
/* update_slot returns int but can't actually fail */
update_slot(type, p->name_strobj);
}

#ifdef Py_GIL_DISABLED
_PyEval_StartTheWorld(interp);
#endif
}


Expand Down
20 changes: 19 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,7 @@ start_the_world(struct _stoptheworld_state *stw)
assert(PyMutex_IsLocked(&stw->mutex));

HEAD_LOCK(runtime);
stw->world_stops++;
stw->requested = 0;
stw->world_stopped = 0;
// Switch threads back to the detached state.
Expand Down Expand Up @@ -2730,6 +2731,7 @@ _PyGILState_Fini(PyInterpreterState *interp)
return;
}
interp->runtime->gilstate.autoInterpreterState = NULL;
//fprintf(stderr, "world stops %zd\n", interp->stoptheworld.world_stops);
}


Expand Down
22 changes: 22 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,27 @@
#endif
}

/*[clinic input]
sys._get_world_stops

Return the number of times the "stop-the-world" condition was true.
[clinic start generated code]*/

static PyObject *
sys__get_world_stops_impl(PyObject *module)
/*[clinic end generated code: output=7886d32b71a94e72 input=44a9bde7e07b30e3]*/
{
Py_ssize_t stops;
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
stops = interp->stoptheworld.world_stops;
#else
stops = 0;
#endif
return PyLong_FromLong(stops);

Check warning on line 2547 in Python/sysmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2547 in Python/sysmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2547 in Python/sysmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2547 in Python/sysmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'function': conversion from 'Py_ssize_t' to 'long', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
}



static PerfMapState perf_map_state;

Expand Down Expand Up @@ -2703,6 +2724,7 @@
#endif
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
SYS__IS_GIL_ENABLED_METHODDEF
SYS__GET_WORLD_STOPS_METHODDEF
SYS__DUMP_TRACELETS_METHODDEF
{NULL, NULL} // sentinel
};
Expand Down
6 changes: 3 additions & 3 deletions Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

race_top:assign_version_tag
race_top:_multiprocessing_SemLock_acquire_impl
race_top:_Py_slot_tp_getattr_hook
#race_top:_Py_slot_tp_getattr_hook
race_top:dump_traceback
race_top:fatal_error
race_top:_multiprocessing_SemLock_release_impl
race_top:_PyFrame_GetCode
race_top:_PyFrame_Initialize
race_top:_PyObject_TryGetInstanceAttribute
race_top:PyUnstable_InterpreterFrame_GetLine
race_top:type_modified_unlocked
#race_top:type_modified_unlocked
race_top:write_thread_id

# gh-129068: race on shared range iterators (test_free_threading.test_zip.ZipThreading.test_threading)
Expand All @@ -32,7 +32,7 @@ race_top:rangeiter_next
race_top:mi_block_set_nextx

# gh-127266: type slot updates are not thread-safe (test_opcache.test_load_attr_method_lazy_dict)
race_top:update_one_slot
#race_top:update_one_slot

# https://gist.github.com/mpage/6962e8870606cfc960e159b407a0cb40
thread:pthread_create
Expand Down
Loading
0