Skip to content

Commit 8dd10cd

Browse files
committed
gh-140067: Fix memory leak in subinterpreter creation
The memcpy() call in _PyInterpreterState_New() was overwriting the _malloced pointer that was set by alloc_interpreter(), causing a memory leak when subinterpreters were destroyed. Fixed by preserving the _malloced pointer across the memcpy().
1 parent 6416e6e commit 8dd10cd

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Python/pystate.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,14 @@ _PyInterpreterState_New(PyThreadState *tstate, PyInterpreterState **pinterp)
662662
status = _PyStatus_NO_MEMORY();
663663
goto error;
664664
}
665+
666+
void *_malloced = interp->_malloced;
667+
665668
// Set to _PyInterpreterState_INIT.
666669
memcpy(interp, &initial._main_interpreter, sizeof(*interp));
667670

671+
interp->_malloced = _malloced;
672+
668673
if (id < 0) {
669674
/* overflow or Py_Initialize() not called yet! */
670675
status = _PyStatus_ERR("failed to get an interpreter ID");

0 commit comments

Comments
 (0)