From be0427fc6551fb861b9a52bf726eeb539fbb277c Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 11 Feb 2024 20:19:29 +0800 Subject: [PATCH 1/5] Lock rotatingtree's random state --- Modules/_lsprof.c | 4 +--- Modules/rotatingtree.c | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 8f09204097529f..c07ad8b1806bb6 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -1004,9 +1004,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} }; diff --git a/Modules/rotatingtree.c b/Modules/rotatingtree.c index 07e08bc3167c0a..13fbed26bc5576 100644 --- a/Modules/rotatingtree.c +++ b/Modules/rotatingtree.c @@ -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)) @@ -10,17 +16,20 @@ static unsigned int random_value = 1; static unsigned int random_stream = 0; +static PyMutex random_mutex = (PyMutex){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; + PyMutex_Unlock(&random_mutex); return result; } From e5d4017dfb9a0cc21bed6bfe25670f535df5e492 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 11 Feb 2024 23:09:27 +0800 Subject: [PATCH 2/5] try to fix Windows error --- Modules/rotatingtree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/rotatingtree.c b/Modules/rotatingtree.c index 13fbed26bc5576..50cceaea65f8d4 100644 --- a/Modules/rotatingtree.c +++ b/Modules/rotatingtree.c @@ -16,7 +16,9 @@ static unsigned int random_value = 1; static unsigned int random_stream = 0; -static PyMutex random_mutex = (PyMutex){0}; +#define _zero 0 +static PyMutex random_mutex = {0}; +#undef _zero static int randombits(int bits) From 03ccd8d49c10e2b953dbf5f4dcfc73a22ae9162d Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 11 Feb 2024 23:09:55 +0800 Subject: [PATCH 3/5] remove debug codes --- Modules/rotatingtree.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/rotatingtree.c b/Modules/rotatingtree.c index 50cceaea65f8d4..217e495b3d2a9d 100644 --- a/Modules/rotatingtree.c +++ b/Modules/rotatingtree.c @@ -16,9 +16,7 @@ static unsigned int random_value = 1; static unsigned int random_stream = 0; -#define _zero 0 static PyMutex random_mutex = {0}; -#undef _zero static int randombits(int bits) From b30f119f2ba792c0cfd7950a7f9c3f68384ffbe1 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 11 Feb 2024 23:40:21 +0800 Subject: [PATCH 4/5] add random_mutex to globals-to-fix.tsv --- Tools/c-analyzer/cpython/globals-to-fix.tsv | 1 + 1 file changed, 1 insertion(+) diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv index 1d9576d083d8dc..00d6e51d937868 100644 --- a/Tools/c-analyzer/cpython/globals-to-fix.tsv +++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv @@ -480,3 +480,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 - From 081c7ca526eac9a4b742724e53f83dd90915811d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:42:53 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst 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 index 00000000000000..47701396c81737 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-12-11-42-48.gh-issue-103092.sGMKr0.rst @@ -0,0 +1 @@ +Isolate :mod:`_lsprof` (apply :pep:`687`).