Skip to content

gh-147998: Fix possible memory leak in _pop_preserved (crossinterp.c)#147999

Open
maurycy wants to merge 2 commits intopython:mainfrom
maurycy:maurycy/pop_preserved_shadowing
Open

gh-147998: Fix possible memory leak in _pop_preserved (crossinterp.c)#147999
maurycy wants to merge 2 commits intopython:mainfrom
maurycy:maurycy/pop_preserved_shadowing

Conversation

@maurycy
Copy link
Copy Markdown
Contributor

@maurycy maurycy commented Apr 2, 2026

_PyXI_namespace *xidata = _create_sharedns(session->_preserved);

if (xidata != NULL) {

The xidata seems to be shadowed inside the else, and the error: path does not see it, resulting in a (small) memory leak.

It's no longer reproducible:

2026-04-02T13:17:22.653113102+0000 maurycy@weiss /home/maurycy/cpython (maurycy/pop_preserved_shadowing) % ASAN_OPTIONS=detect_leaks=1 ./python.exe -c "
import _interpreters as _interp

interp_id = _interp.create()

# The callable must SUCCEED but return something non-shareable.
# globals() returns a dict with module refs -- not shareable.
for i in range(200):
    try:
        _interp.call(interp_id, globals)
    except Exception:
        pass

_interp.destroy(interp_id)
print('done')
"
done
2026-04-02T13:17:52.741094846+0000 maurycy@weiss /home/maurycy/cpython (maurycy/pop_preserved_shadowing) % 

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label Apr 2, 2026
@serhiy-storchaka serhiy-storchaka changed the title gh-147998: Fix a memory leak in _pop_preserved gh-147998: Fix possible memory leak in _pop_preserved (crossinterp.c) Apr 2, 2026
@serhiy-storchaka
Copy link
Copy Markdown
Member

Could you please add a NEWS entry?

_pop_preserved() is only used in _PyXI_Exit(), which is used in functions exec(), run_string(), run_func(), call(), and set___main___attrs() of the private module _interpreters (and finally used in some public API). The description should use some general roundabout words to avoid touching internal details.

@maurycy
Copy link
Copy Markdown
Contributor Author

maurycy commented Apr 2, 2026

@serhiy-storchaka 6b415f5

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants