Skip to content

Commit 080ee5a

Browse files
authored
bpo-38858: Fix ref leak in pycore_interp_init() (GH-17512)
bpo-38858, bpo-38997: _PySys_Create() returns a strong reference to the sys module: Py_DECREF() is needed when we are done with the module.
1 parent 526606b commit 080ee5a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Python/pylifecycle.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,24 +705,29 @@ static PyStatus
705705
pycore_interp_init(PyThreadState *tstate)
706706
{
707707
PyStatus status;
708+
PyObject *sysmod = NULL;
708709

709710
status = pycore_init_types(tstate);
710711
if (_PyStatus_EXCEPTION(status)) {
711-
return status;
712+
goto done;
712713
}
713714

714-
PyObject *sysmod;
715715
status = _PySys_Create(tstate, &sysmod);
716716
if (_PyStatus_EXCEPTION(status)) {
717-
return status;
717+
goto done;
718718
}
719719

720720
status = pycore_init_builtins(tstate);
721721
if (_PyStatus_EXCEPTION(status)) {
722-
return status;
722+
goto done;
723723
}
724724

725-
return pycore_init_import_warnings(tstate, sysmod);
725+
status = pycore_init_import_warnings(tstate, sysmod);
726+
727+
done:
728+
/* sys.modules['sys'] contains a strong reference to the module */
729+
Py_XDECREF(sysmod);
730+
return status;
726731
}
727732

728733

0 commit comments

Comments
 (0)