From 080ee5a88406fb68aaab741145cd5d2a7c5f2ad6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 8 Dec 2019 21:55:58 +0100 Subject: [PATCH] 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. --- Python/pylifecycle.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8bd71a350de..823d96e86a4 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -705,24 +705,29 @@ static PyStatus pycore_interp_init(PyThreadState *tstate) { PyStatus status; + PyObject *sysmod = NULL; status = pycore_init_types(tstate); if (_PyStatus_EXCEPTION(status)) { - return status; + goto done; } - PyObject *sysmod; status = _PySys_Create(tstate, &sysmod); if (_PyStatus_EXCEPTION(status)) { - return status; + goto done; } status = pycore_init_builtins(tstate); if (_PyStatus_EXCEPTION(status)) { - return status; + goto done; } - return pycore_init_import_warnings(tstate, sysmod); + status = pycore_init_import_warnings(tstate, sysmod); + +done: + /* sys.modules['sys'] contains a strong reference to the module */ + Py_XDECREF(sysmod); + return status; }