https://github.com/python/cpython/commit/14af19e6c0d9dd05b525596fdd53373f726253d8
commit: 14af19e6c0d9dd05b525596fdd53373f726253d8
branch: main
author: sobolevn <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-05-18T18:39:54+03:00
summary:

gh-149816: Fix a RC in `_random.Random.__init__` method (#149824)

files:
A Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst
M Modules/_randommodule.c
M Modules/clinic/_randommodule.c.h

diff --git 
a/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst 
b/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst
new file mode 100644
index 00000000000000..3ea70071ec3c75
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst
@@ -0,0 +1,2 @@
+Fix a race condition in ``_random.Random.__init__`` method in free-threading
+mode.
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 0fb73481651748..a06966be23be1e 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -123,9 +123,9 @@ typedef struct {
 
 /*[clinic input]
 module _random
-class _random.Random "RandomObject *" "_randomstate_type(type)->Random_Type"
+class _random.Random "RandomObject *" "(PyTypeObject 
*)_randomstate_type(Py_TYPE(self))->Random_Type"
 [clinic start generated code]*/
-/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70a2c99619474983]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f04bcbfba61a322e]*/
 
 /* Random methods */
 
@@ -549,27 +549,20 @@ _random_Random_getrandbits_impl(RandomObject *self, 
uint64_t k)
     return result;
 }
 
-static int
-random_init(PyObject *self, PyObject *args, PyObject *kwds)
-{
-    PyObject *arg = NULL;
-    _randomstate *state = _randomstate_type(Py_TYPE(self));
-
-    if ((Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) ||
-         Py_TYPE(self)->tp_init == 
((PyTypeObject*)state->Random_Type)->tp_init) &&
-        !_PyArg_NoKeywords("Random", kwds)) {
-        return -1;
-    }
-
-    if (PyTuple_GET_SIZE(args) > 1) {
-        PyErr_SetString(PyExc_TypeError, "Random() requires 0 or 1 argument");
-        return -1;
-    }
+/*[clinic input]
+@critical_section
+@text_signature "($self, [seed])"
+_random.Random.__init__ as random_init
 
-    if (PyTuple_GET_SIZE(args) == 1)
-        arg = PyTuple_GET_ITEM(args, 0);
+    seed: object = NULL
+    /
+[clinic start generated code]*/
 
-    return random_seed(RandomObject_CAST(self), arg);
+static int
+random_init_impl(RandomObject *self, PyObject *seed)
+/*[clinic end generated code: output=260734a3739c394f input=e516bf32e8a05e28]*/
+{
+    return random_seed(self, seed);
 }
 
 
diff --git a/Modules/clinic/_randommodule.c.h b/Modules/clinic/_randommodule.c.h
index 2563a16aea0b6f..ca9cad7a572dad 100644
--- a/Modules/clinic/_randommodule.c.h
+++ b/Modules/clinic/_randommodule.c.h
@@ -143,4 +143,35 @@ _random_Random_getrandbits(PyObject *self, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=7ce97b2194eecaf7 input=a9049054013a1b77]*/
+
+static int
+random_init_impl(RandomObject *self, PyObject *seed);
+
+static int
+random_init(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    int return_value = -1;
+    PyTypeObject *base_tp = (PyTypeObject 
*)_randomstate_type(Py_TYPE(self))->Random_Type;
+    PyObject *seed = NULL;
+
+    if ((Py_IS_TYPE(self, base_tp) ||
+         Py_TYPE(self)->tp_new == base_tp->tp_new) &&
+        !_PyArg_NoKeywords("Random", kwargs)) {
+        goto exit;
+    }
+    if (!_PyArg_CheckPositional("Random", PyTuple_GET_SIZE(args), 0, 1)) {
+        goto exit;
+    }
+    if (PyTuple_GET_SIZE(args) < 1) {
+        goto skip_optional;
+    }
+    seed = PyTuple_GET_ITEM(args, 0);
+skip_optional:
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = random_init_impl((RandomObject *)self, seed);
+    Py_END_CRITICAL_SECTION();
+
+exit:
+    return return_value;
+}
+/*[clinic end generated code: output=ec95f7df0c3f3c19 input=a9049054013a1b77]*/

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to