From 2191c56def779ce28174a0f8edcd93559fddbcbb Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Thu, 19 Feb 2026 10:47:36 +0300 Subject: [PATCH 1/6] gh-144986: Fix memory leak in atexit.register() --- Modules/atexitmodule.c | 4 ++++ Parser/action_helpers.c | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index 1c901d9124d9ca..3ddbbd59a1ef0c 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -185,6 +185,9 @@ atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) return NULL; } PyObject *func_args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); + if (func_args == NULL) { + return NULL; + } PyObject *func_kwargs = kwargs; if (func_kwargs == NULL) @@ -192,6 +195,7 @@ atexit_register(PyObject *module, PyObject *args, PyObject *kwargs) func_kwargs = Py_None; } PyObject *callback = PyTuple_Pack(3, func, func_args, func_kwargs); + Py_DECREF(func_args); if (callback == NULL) { return NULL; diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index 1f5b6220ba1baa..282a76cf6f87bd 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -1683,7 +1683,7 @@ _build_concatenated_bytes(Parser *p, asdl_expr_seq *strings, int lineno, Py_DECREF(res); return NULL; } - return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, p->arena); + return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, arena); } static expr_ty @@ -1724,7 +1724,7 @@ _build_concatenated_unicode(Parser *p, asdl_expr_seq *strings, int lineno, if (final == NULL) { return NULL; } - if (_PyArena_AddPyObject(p->arena, final) < 0) { + if (_PyArena_AddPyObject(arena, final) < 0) { Py_DECREF(final); return NULL; } @@ -1895,7 +1895,7 @@ _build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings, { asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno, col_offset, end_lineno, end_col_offset, arena); - return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena); + return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, arena); } expr_ty From 0e0abc3677f895a35cfa208ce0d35187733da26e Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Thu, 19 Feb 2026 12:19:13 +0300 Subject: [PATCH 2/6] fix --- Parser/action_helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index 282a76cf6f87bd..8105c404472944 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -1683,7 +1683,7 @@ _build_concatenated_bytes(Parser *p, asdl_expr_seq *strings, int lineno, Py_DECREF(res); return NULL; } - return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, arena); + return _PyAST_Constant(res, kind, lineno, col_offset, end_lineno, end_col_offset, p->arena); } static expr_ty From 616a5ca910f8c0a347272e135c8708e086a549d4 Mon Sep 17 00:00:00 2001 From: Shamil Date: Thu, 19 Feb 2026 12:25:30 +0300 Subject: [PATCH 3/6] Revert unrelated Parser/action_helpers.c change --- Parser/action_helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index 8105c404472944..1f5b6220ba1baa 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -1724,7 +1724,7 @@ _build_concatenated_unicode(Parser *p, asdl_expr_seq *strings, int lineno, if (final == NULL) { return NULL; } - if (_PyArena_AddPyObject(arena, final) < 0) { + if (_PyArena_AddPyObject(p->arena, final) < 0) { Py_DECREF(final); return NULL; } @@ -1895,7 +1895,7 @@ _build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings, { asdl_expr_seq *values = _build_concatenated_str(p, strings, lineno, col_offset, end_lineno, end_col_offset, arena); - return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, arena); + return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena); } expr_ty From 28e4533b8c5692ffef68ba0a9b7b04fc93c721b3 Mon Sep 17 00:00:00 2001 From: Shamil Date: Thu, 19 Feb 2026 12:25:48 +0300 Subject: [PATCH 4/6] Add NEWS entry for gh-144986 --- .../Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst diff --git a/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst new file mode 100644 index 00000000000000..84650bf5fdaafe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst @@ -0,0 +1,2 @@ +Fix memory leak in :func:`atexit.register` caused by missing :c:func:`Py_DECREF` +for the arguments tuple. Patch by Shamil Abdulaev. From c0c2ab536dd421ee62fa9515522146b76bea7580 Mon Sep 17 00:00:00 2001 From: Shamil Date: Thu, 19 Feb 2026 12:47:41 +0300 Subject: [PATCH 5/6] Update Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- .../2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst index 84650bf5fdaafe..99c1e9408144c2 100644 --- a/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst +++ b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst @@ -1,2 +1,2 @@ -Fix memory leak in :func:`atexit.register` caused by missing :c:func:`Py_DECREF` -for the arguments tuple. Patch by Shamil Abdulaev. +Fix a memory leak in :func:`atexit.register. +Patch by Shamil Abdulaev. From 73f9048c97ae085b5f2ec32a01dce7514c4b1889 Mon Sep 17 00:00:00 2001 From: Shamil Date: Thu, 19 Feb 2026 15:18:19 +0300 Subject: [PATCH 6/6] Fix missing closing backtick in NEWS entry --- .../Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst index 99c1e9408144c2..841c3758ec4df1 100644 --- a/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst +++ b/Misc/NEWS.d/next/Library/2026-02-19-00-00-00.gh-issue-144986.atexit-leak.rst @@ -1,2 +1,2 @@ -Fix a memory leak in :func:`atexit.register. +Fix a memory leak in :func:`atexit.register`. Patch by Shamil Abdulaev.