diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst new file mode 100644 index 00000000000000..a5a6908757e458 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-28-16-46-17.gh-issue-145376.lG5u1a.rst @@ -0,0 +1 @@ +Fix reference leaks in various unusual error scenarios. diff --git a/Modules/main.c b/Modules/main.c index 74e48c94732565..95ba541d98c2e8 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -506,6 +506,7 @@ pymain_run_interactive_hook(int *exitcode) } if (PySys_Audit("cpython.run_interactivehook", "O", hook) < 0) { + Py_DECREF(hook); goto error; } diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 6365b995a0d3f7..c8a80e7a986008 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -609,6 +609,7 @@ check_missing___main___attr(PyObject *exc) // Get the error message. PyObject *args = PyException_GetArgs(exc); if (args == NULL || args == Py_None || PyObject_Size(args) < 1) { + Py_XDECREF(args); assert(!PyErr_Occurred()); return 0; } diff --git a/Python/import.c b/Python/import.c index 4c234a4a70437c..d86bb0b6ac6125 100644 --- a/Python/import.c +++ b/Python/import.c @@ -5638,6 +5638,7 @@ _imp__set_lazy_attributes_impl(PyObject *module, PyObject *modobj, module_dict = get_mod_dict(modobj); if (module_dict == NULL || !PyDict_CheckExact(module_dict)) { + Py_DECREF(lazy_submodules); goto done; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ec8c2d12ab27fc..043bdf3433ab57 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1145,6 +1145,7 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) "traceback", "_print_exception_bltin"); if (print_exception_fn == NULL || !PyCallable_Check(print_exception_fn)) { + Py_XDECREF(print_exception_fn); goto fallback; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 28b2108940c853..55b4072213d3c2 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1762,7 +1762,7 @@ sys_getwindowsversion_impl(PyObject *module) PyObject *realVersion = _sys_getwindowsversion_from_kernel32(); if (!realVersion) { if (!PyErr_ExceptionMatches(PyExc_WindowsError)) { - return NULL; + goto error; } PyErr_Clear();