Skip to content

Commit

Permalink
fix memory leak in make_interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
813gan committed Aug 19, 2024
1 parent fb3c142 commit 3e72f27
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions subinterpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ struct interpr *get_interpreter(char *name) {
}

void make_interpreter(char *interpreter_name) {
PyGILState_STATE gil = PyGILState_Ensure();
PyThreadState *tstate = PyThreadState_Get();
PyThreadState *orig_tstate = PyThreadState_Get();

struct interpr *maybe_existing_interpreter = get_interpreter(interpreter_name);
if (NULL != maybe_existing_interpreter) {
PyGILState_Release(gil);
return;
}

unsigned int name_len = strnlen(interpreter_name, MAX_INTERPRETER_NAME_LEN) + 1;
char *name = malloc(name_len);
assert(name);
Expand All @@ -74,17 +84,6 @@ void make_interpreter(char *interpreter_name) {
.gil = PyInterpreterConfig_SHARED_GIL,
};

PyGILState_STATE gil = PyGILState_Ensure();

struct interpr *maybe_existing_interpreter = get_interpreter(name);
if (NULL != maybe_existing_interpreter) {
PyGILState_Release(gil);
return;
}

PyThreadState *tstate = PyThreadState_Get();

PyThreadState *orig_tstate = PyThreadState_Get();
PyThreadState_Swap(NULL);

PyStatus status = Py_NewInterpreterFromConfig(&tstate, &config);
Expand Down

0 comments on commit 3e72f27

Please sign in to comment.