From b04003c63e21722c25e04ce5b7c53d8e8baf1281 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 7 Jan 2026 15:17:28 -0500 Subject: [PATCH 1/2] gh-129824: Fix data race on runtime->gilstate.check_enabled The new_interpreter() function can be called concurrently. --- Python/pylifecycle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 16fb43ea191439..88dbdb6d139c5f 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -2435,7 +2435,7 @@ new_interpreter(PyThreadState **tstate_p, /* Issue #10915, #15751: The GIL API doesn't work with multiple interpreters: disable PyGILState_Check(). */ - runtime->gilstate.check_enabled = 0; + _Py_atomic_store_int_relaxed(&runtime->gilstate.check_enabled, 0); // XXX Might new_interpreter() have been called without the GIL held? PyThreadState *save_tstate = _PyThreadState_GET(); From bb7c984067fab858d576a950ce069f732e623f9e Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 7 Jan 2026 16:18:24 -0500 Subject: [PATCH 2/2] Use atomic for read as well --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index f605527598a86d..23853f69792450 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2832,7 +2832,7 @@ int PyGILState_Check(void) { _PyRuntimeState *runtime = &_PyRuntime; - if (!runtime->gilstate.check_enabled) { + if (!_Py_atomic_load_int_relaxed(&runtime->gilstate.check_enabled)) { return 1; }