summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNeil Schemenauer <nas-github@arctrix.com>2025-04-28 20:28:44 (GMT)
committerGitHub <noreply@github.com>2025-04-28 20:28:44 (GMT)
commite414a2d81c3e15553516979e146d9f258fb47b2e (patch)
treea3235722193c596e4188373416afb4412cdd6045 /Python
parent22f0730d40c9534672f745f35cf876ad63d450bb (diff)
downloadcpython-e414a2d81c3e15553516979e146d9f258fb47b2e.zip
cpython-e414a2d81c3e15553516979e146d9f258fb47b2e.tar.gz
cpython-e414a2d81c3e15553516979e146d9f258fb47b2e.tar.bz2
gh-127266: avoid data races when updating type slots (gh-131174)
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index fb72fd4..b7b7f9c 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -138,6 +138,19 @@
#endif
+static void
+check_invalid_reentrancy(void)
+{
+#if defined(Py_DEBUG) && defined(Py_GIL_DISABLED)
+ // In the free-threaded build, the interpreter must not be re-entered if
+ // the world-is-stopped. If so, that's a bug somewhere (quite likely in
+ // the painfully complex typeobject code).
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ assert(!interp->stoptheworld.world_stopped);
+#endif
+}
+
+
#ifdef Py_DEBUG
static void
dump_item(_PyStackRef item)
@@ -995,6 +1008,7 @@ PyObject* _Py_HOT_FUNCTION DONT_SLP_VECTORIZE
_PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)
{
_Py_EnsureTstateNotNULL(tstate);
+ check_invalid_reentrancy();
CALL_STAT_INC(pyeval_calls);
#if USE_COMPUTED_GOTOS && !Py_TAIL_CALL_INTERP