gh-140067: Fix memory leak in sub-interpreter creation by ashm-dev · Pull Request #140111 · python/cpython · GitHub
Skip to content

gh-140067: Fix memory leak in sub-interpreter creation - #140111

Merged
kumaraditya303 merged 6 commits into
python:mainfrom
ashm-dev:gh-140067
Oct 14, 2025
Merged

gh-140067: Fix memory leak in sub-interpreter creation #140111
kumaraditya303 merged 6 commits into
python:mainfrom
ashm-dev:gh-140067

Conversation

@ashm-dev

@ashm-dev ashm-dev commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

This patch fixes a memory leak detected by AddressSanitizer during subinterpreter creation.
The original allocation pointer is now stored before the aligned memory block, removing
the need for the _malloced field in PyInterpreterState.

Thanks to @kumaraditya303 for highlighting the pointer overwrite issue.

Fixes #140067

@StanFromIreland StanFromIreland changed the title bpo-140067: Fix memory leak in subinterpreter creation (remove redundant gh-140067: Fix memory leak in subinterpreter creation (remove redundant Oct 14, 2025
@kumaraditya303 kumaraditya303 changed the title gh-140067: Fix memory leak in subinterpreter creation (remove redundant gh-140067: Fix memory leak in sub-interpreter creation Oct 14, 2025
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-17-07-37.gh-issue-140067.ID2gOm.rst Outdated
Comment thread Python/pystate.c Outdated
Comment thread Python/pystate.c Outdated
ashm-dev and others added 3 commits October 14, 2025 17:16
…e-140067.ID2gOm.rst

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@kumaraditya303

Copy link
Copy Markdown
Contributor

Comment thread Include/internal/pycore_interp_structs.h
@kumaraditya303 kumaraditya303 self-assigned this Oct 14, 2025
@kumaraditya303 kumaraditya303 added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-subinterpreters labels Oct 14, 2025
@kumaraditya303
kumaraditya303 enabled auto-merge (squash) October 14, 2025 14:38
@kumaraditya303
kumaraditya303 merged commit 59547a2 into python:main Oct 14, 2025
53 checks passed
@ashm-dev
ashm-dev deleted the gh-140067 branch October 14, 2025 14:43
@bedevere-app

bedevere-app Bot commented Oct 14, 2025

Copy link
Copy Markdown

GH-140118 is a backport of this pull request to the 3.14 branch.

@bedevere-app

bedevere-app Bot commented Oct 14, 2025

Copy link
Copy Markdown

GH-140118 is a backport of this pull request to the 3.14 branch.

kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Oct 14, 2025
…ythonGH-140111)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally.
(cherry picked from commit 59547a2)

Co-authored-by: Shamil <ashm.tech@proton.me>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this pull request Oct 14, 2025
@ZeroIntensity

Copy link
Copy Markdown
Member

kumaraditya303 pushed a commit that referenced this pull request Oct 15, 2025
@ashm-dev
ashm-dev restored the gh-140067 branch October 15, 2025 07:13
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Oct 17, 2025
…140111)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally. 

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull request Oct 18, 2025
…0261)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally. 

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull request Oct 18, 2025
) (#140118)

* [3.14] gh-140067: Fix memory leak in sub-interpreter creation  (GH-140111)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally.
(cherry picked from commit 59547a2)

Co-authored-by: Shamil <ashm.tech@proton.me>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…140111)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally. 

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
…140111)  (python#140261)

Fix memory leak in sub-interpreter creation caused by overwriting of the previously used `_malloced` field. Now the pointer is stored in the first word of the memory block to avoid it being overwritten accidentally. 

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-subinterpreters

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection)

3 participants