]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103092: Add a mutex to make the PRNG state of rotatingtree concurrent-safe (#115301)
authorAN Long <aisk@users.noreply.github.com>
Thu, 29 Feb 2024 23:04:16 +0000 (07:04 +0800)
committerGitHub <noreply@github.com>
Thu, 29 Feb 2024 23:04:16 +0000 (00:04 +0100)
Misc/NEWS.d/next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst [new file with mode: 0644]
Modules/_lsprof.c
Modules/rotatingtree.c
Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Misc/NEWS.d/next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst b/Misc/NEWS.d/next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst
new file mode 100644 (file)
index 0000000..4770139
--- /dev/null
@@ -0,0 +1 @@
+Isolate :mod:`_lsprof` (apply :pep:`687`).
index f1cee7cb6f66bf8b20c6a16b2f0879ce08e83811..a76c3dea55578390ab1196de1eccab28a45837d3 100644 (file)
@@ -1005,9 +1005,7 @@ _lsprof_exec(PyObject *module)
 
 static PyModuleDef_Slot _lsprofslots[] = {
     {Py_mod_exec, _lsprof_exec},
-    // XXX gh-103092: fix isolation.
-    {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
-    //{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
+    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
     {0, NULL}
 };
 
index 07e08bc3167c0a408352b8ee7a1bbf5ed66006ad..217e495b3d2a9d95b34637177ba91f64b4ceca16 100644 (file)
@@ -1,3 +1,9 @@
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
+#include "Python.h"
+#include "pycore_lock.h"
 #include "rotatingtree.h"
 
 #define KEY_LOWER_THAN(key1, key2)  ((char*)(key1) < (char*)(key2))
 
 static unsigned int random_value = 1;
 static unsigned int random_stream = 0;
+static PyMutex random_mutex = {0};
 
 static int
 randombits(int bits)
 {
     int result;
+    PyMutex_Lock(&random_mutex);
     if (random_stream < (1U << bits)) {
         random_value *= 1082527;
         random_stream = random_value;
     }
     result = random_stream & ((1<<bits)-1);
     random_stream >>= bits;
+    PyMutex_Unlock(&random_mutex);
     return result;
 }
 
index 45119664af4362a23c10b6d6671528f8b1bbe7ce..686a3d3160cc906c35249964cab56a59e03adc06 100644 (file)
@@ -482,3 +482,4 @@ Modules/readline.c  -       sigwinch_ohandler       -
 Modules/readline.c     -       completed_input_string  -
 Modules/rotatingtree.c -       random_stream   -
 Modules/rotatingtree.c -       random_value    -
+Modules/rotatingtree.c -       random_mutex    -