Skip to content

gh-163172: _PyEvalFramePushAndInit_Ex leaks locals on failure if not using fast locals - #163174

Open
t-0 wants to merge 4 commits into
python:mainfrom
t-0:bugfix/_PyEvalFramePushAndInit_Ex__leak
Open

t-0 wants to merge 4 commits into
python:mainfrom
t-0:bugfix/_PyEvalFramePushAndInit_Ex__leak

Conversation

@t-0

@t-0 t-0 commented Dec 4, 2026

Copy link
Copy Markdown

gh-163172: _PyEvalFramePushAndInit_Ex leaks locals on failure if not using fast locals

Bug report

CALL_FUNCTION_EX passes a new reference to a function globals as locals when not using fast locals. Each failing call will leak a reference, e.g. should _PyStack_UnpackDict() fails because **kwargs is not a string.

diff --git a/Python/ceval.c b/Python/ceval.c
index 446e6a8be4d..e616641ea4d 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2183,6 +2183,7 @@ _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func,
     Py_XDECREF(kwargs);
     return new_frame;
 error:
+    Py_XDECREF(locals);
     Py_DECREF(callargs);
     Py_XDECREF(kwargs);

Reproduce with:

import sys, types
g = {}
f = types.FunctionType(compile("pass", "<s>", "exec"), g)
def opt():
    pass
for name, fn in [("non-optimized", f), ("optimized", opt)]:
    t1 = g if fn is f else opt.__globals__
    t0 = sys.getrefcount(t1)
    for _ in range(10):
        try:
            fn(**{1: 2})
        except TypeError:
            pass
    print(name, "globals refcount delta:", sys.getrefcount(t1) - t0)

CPython versions tested on: 3.16, CPython main branch
Operating systems tested on: macOS

@t-0
t-0 requested a review from markshannon as a code owner December 4, 2026 21:14
@python-cla-bot

python-cla-bot Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@t-0
t-0 force-pushed the bugfix/_PyEvalFramePushAndInit_Ex__leak branch from de5b05d to afabe1f Compare December 4, 2026 21:36
@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Python/ceval.c Resolved
@efanntyo

efanntyo commented Dec 5, 2026

Copy link
Copy Markdown

A regression test could distinguish failure before frame initialization from argument-binding failure after delegation. I ran this matrix on CPython 3.12.16/Linux (not a build of this PR): using types.FunctionType(compile('pass', '<probe>', 'exec'), g), 1,000 calls with **{1: 2} increased sys.getrefcount(g) by 1,000; **{'unexpected': 2}, *(1,), and a successful empty call each had delta 0. All four optimized-function controls had delta 0 as well. Exceptions were caught inside a helper, without retaining traceback objects.

At afabe1f, the invalid-key case reaches the local error path through _PyStack_UnpackDict; the valid-string unexpected-key case delegates to _PyEvalFramePushAndInit first. Keeping both TypeError paths in a regression test would check the leak repair and guard against accidentally releasing locals twice on delegated failure. A patched-build test should require zero delta for every case (and could use the existing refleak test infrastructure). I have not run the patched interpreter or injected the allocation-failure branch.

AI-assisted analysis; the local matrix above was executed and the stated source paths were inspected.

@bedevere-app

bedevere-app Bot commented Dec 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Dec 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Dec 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz

picnixz commented Dec 5, 2026

Copy link
Copy Markdown
Member
  • Please avoid merging main into the branch if there is no conflict.
  • If it's possible, you need to add a NEWS entry.

@picnixz

picnixz commented Dec 5, 2026

Copy link
Copy Markdown
Member

And if possible, add a test.

@picnixz

picnixz commented Dec 5, 2026

Copy link
Copy Markdown
Member

And I think you also need to update the comment as it says that locals references are not stolen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants