Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-103092: Add a mutex to make the random state of rotatingtree concurrent-safe #115301

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Isolate :mod:`_lsprof` (apply :pep:`687`).
4 changes: 1 addition & 3 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};

Expand Down
9 changes: 9 additions & 0 deletions Modules/rotatingtree.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved

#include "Python.h"
#include "pycore_lock.h"
#include "rotatingtree.h"

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

static unsigned int random_value = 1;
static unsigned int random_stream = 0;
static PyMutex random_mutex = {0};
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved

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;
}

Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Loading