Skip to content

Commit ca56c3a

Browse files
authored
gh-103092: Add a mutex to make the PRNG state of rotatingtree concurrent-safe (#115301)
1 parent 6a95676 commit ca56c3a

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Isolate :mod:`_lsprof` (apply :pep:`687`).

Modules/_lsprof.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,7 @@ _lsprof_exec(PyObject *module)
10051005

10061006
static PyModuleDef_Slot _lsprofslots[] = {
10071007
{Py_mod_exec, _lsprof_exec},
1008-
// XXX gh-103092: fix isolation.
1009-
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
1010-
//{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
1008+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
10111009
{0, NULL}
10121010
};
10131011

Modules/rotatingtree.c

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#ifndef Py_BUILD_CORE_BUILTIN
2+
# define Py_BUILD_CORE_MODULE 1
3+
#endif
4+
5+
#include "Python.h"
6+
#include "pycore_lock.h"
17
#include "rotatingtree.h"
28

39
#define KEY_LOWER_THAN(key1, key2) ((char*)(key1) < (char*)(key2))
@@ -10,17 +16,20 @@
1016

1117
static unsigned int random_value = 1;
1218
static unsigned int random_stream = 0;
19+
static PyMutex random_mutex = {0};
1320

1421
static int
1522
randombits(int bits)
1623
{
1724
int result;
25+
PyMutex_Lock(&random_mutex);
1826
if (random_stream < (1U << bits)) {
1927
random_value *= 1082527;
2028
random_stream = random_value;
2129
}
2230
result = random_stream & ((1<<bits)-1);
2331
random_stream >>= bits;
32+
PyMutex_Unlock(&random_mutex);
2433
return result;
2534
}
2635

Tools/c-analyzer/cpython/globals-to-fix.tsv

+1
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,4 @@ Modules/readline.c - sigwinch_ohandler -
482482
Modules/readline.c - completed_input_string -
483483
Modules/rotatingtree.c - random_stream -
484484
Modules/rotatingtree.c - random_value -
485+
Modules/rotatingtree.c - random_mutex -

0 commit comments

Comments
 (0)