GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-140067: Fix memory leak in sub-interpreter creation by ashm-dev · Pull Request #140111 · python/cpython

Conversation

@ashm-dev

@ashm-dev ashm-dev commented Oct 14, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

  • Issue: Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection) #140067

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ashm-dev
bpo-140067: Fix memory leak in subinterpreter creation (remove redundant
595adea
malloced pointer)
@ashm-dev
ashm-dev requested review from ZeroIntensity and ericsnowcurrently as code owners October 14, 2025 14:03
@bedevere-app bedevere-app Bot added the awaiting review label Oct 14, 2025
@ashm-dev
add blurb
0fd2715
@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
@bedevere-app bedevere-app Bot mentioned this pull request Oct 14, 2025
Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection) #140067
Closed
@ashm-dev
Merge branch 'main' into pythongh-140067
b96102b
@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
kumaraditya303
kumaraditya303 reviewed Oct 14, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-17-07-37.gh-issue-140067.ID2gOm.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 reviewed Oct 14, 2025
View reviewed changes
Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 reviewed Oct 14, 2025
View reviewed changes
Comment thread Python/pystate.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

ashm-dev and others added 3 commits October 14, 2025 17:16
@ashm-dev @kumaraditya303
Update Misc/NEWS.d/next/Core_and_Builtins/2025-10-14-17-07-37.gh-issu…
e9c6735
…e-140067.ID2gOm.rst

Co-authored-by: Kumar Aditya <[email protected]>
@ashm-dev @kumaraditya303
Update Python/pystate.c
0afd99e
Co-authored-by: Kumar Aditya <[email protected]>
@ashm-dev @kumaraditya303
Update Python/pystate.c
47fef02
Co-authored-by: Kumar Aditya <[email protected]>
@kumaraditya303

kumaraditya303 commented Oct 14, 2025

Copy link
Copy Markdown
Contributor

On main without fix:

Using random seed: 1178707616
0:00:00 load avg: 0.00 Run 1 test sequentially in a single process
0:00:00 load avg: 0.00 [1/1] test_sys
0:00:15 load avg: 0.30 [1/1] test_sys passed

== Tests result: SUCCESS ==

1 test OK.

Total duration: 15.3 sec
Total tests: run=92 skipped=7
Total test files: run=1/1
Result: SUCCESS

=================================================================
==72608==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4290637 byte(s) in 19 object(s) allocated from:
    #0 0x55ccf021119d in calloc (/home/realkumaraditya/cpython/python+0x32f19d) (BuildId: 8c0c0e48d6c92676ff9a5253a45afb26c8d18926)
    #1 0x55ccf0549d3e in _PyMem_DebugRawAlloc /home/realkumaraditya/cpython/Objects/obmalloc.c:2884:24
    #2 0x55ccf0549d3e in _PyMem_DebugRawCalloc /home/realkumaraditya/cpython/Objects/obmalloc.c:2929:12
    #3 0x55ccf091d6d5 in alloc_interpreter /home/realkumaraditya/cpython/Python/pystate.c:462:17
    #4 0x55ccf091d6d5 in _PyInterpreterState_New /home/realkumaraditya/cpython/Python/pystate.c:660:18
    #5 0x55ccf08d5f30 in new_interpreter /home/realkumaraditya/cpython/Python/pylifecycle.c:2425:14
    #6 0x55ccf08d5cb2 in Py_NewInterpreterFromConfig /home/realkumaraditya/cpython/Python/pylifecycle.c:2521:12
    #7 0x55ccf080f6a8 in _PyXI_NewInterpreter /home/realkumaraditya/cpython/Python/crossinterp.c:3204:23
    #8 0x7f4cbc4dc62f in _interpreters_create_impl /home/realkumaraditya/cpython/./Modules/_interpretersmodule.c:880:13
    #9 0x7f4cbc4dc62f in _interpreters_create /home/realkumaraditya/cpython/./Modules/clinic/_interpretersmodule.c.h:91:20
    #10 0x55ccf03fafc2 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #11 0x55ccf0783a7f in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:3188:35
    #12 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #13 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #14 0x55ccf0406d12 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #15 0x55ccf0404631 in method_vectorcall /home/realkumaraditya/cpython/Objects/classobject.c:95:18
    #16 0x55ccf077790d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:2616:32
    #17 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #18 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #19 0x55ccf03fb440 in _PyObject_VectorcallDictTstate /home/realkumaraditya/cpython/Objects/call.c:135:15
    #20 0x55ccf03fe1b2 in _PyObject_Call_Prepend /home/realkumaraditya/cpython/Objects/call.c:504:24
    #21 0x55ccf05ade31 in call_method /home/realkumaraditya/cpython/Objects/typeobject.c:3076:19
    #22 0x55ccf03fb9f3 in _PyObject_MakeTpCall /home/realkumaraditya/cpython/Objects/call.c:242:18
    #23 0x55ccf078353d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:4021:35
    #24 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #25 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #26 0x55ccf0406d12 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #27 0x55ccf0404631 in method_vectorcall /home/realkumaraditya/cpython/Objects/classobject.c:95:18
    #28 0x55ccf077790d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:2616:32
    #29 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #30 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #31 0x55ccf03fb440 in _PyObject_VectorcallDictTstate /home/realkumaraditya/cpython/Objects/call.c:135:15
    #32 0x55ccf03fe1b2 in _PyObject_Call_Prepend /home/realkumaraditya/cpython/Objects/call.c:504:24
    #33 0x55ccf05ade31 in call_method /home/realkumaraditya/cpython/Objects/typeobject.c:3076:19
    #34 0x55ccf03fb9f3 in _PyObject_MakeTpCall /home/realkumaraditya/cpython/Objects/call.c:242:18
    #35 0x55ccf078353d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:4021:35
    #36 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #37 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12

Direct leak of 225823 byte(s) in 1 object(s) allocated from:
    #0 0x55ccf021119d in calloc (/home/realkumaraditya/cpython/python+0x32f19d) (BuildId: 8c0c0e48d6c92676ff9a5253a45afb26c8d18926)
    #1 0x55ccf0549d3e in _PyMem_DebugRawAlloc /home/realkumaraditya/cpython/Objects/obmalloc.c:2884:24
    #2 0x55ccf0549d3e in _PyMem_DebugRawCalloc /home/realkumaraditya/cpython/Objects/obmalloc.c:2929:12
    #3 0x55ccf091d6d5 in alloc_interpreter /home/realkumaraditya/cpython/Python/pystate.c:462:17
    #4 0x55ccf091d6d5 in _PyInterpreterState_New /home/realkumaraditya/cpython/Python/pystate.c:660:18
    #5 0x55ccf08d5f30 in new_interpreter /home/realkumaraditya/cpython/Python/pylifecycle.c:2425:14
    #6 0x55ccf08d5cb2 in Py_NewInterpreterFromConfig /home/realkumaraditya/cpython/Python/pylifecycle.c:2521:12
    #7 0x55ccf080f6a8 in _PyXI_NewInterpreter /home/realkumaraditya/cpython/Python/crossinterp.c:3204:23
    #8 0x7f4cbc4dc62f in _interpreters_create_impl /home/realkumaraditya/cpython/./Modules/_interpretersmodule.c:880:13
    #9 0x7f4cbc4dc62f in _interpreters_create /home/realkumaraditya/cpython/./Modules/clinic/_interpretersmodule.c.h:91:20
    #10 0x55ccf03fafc2 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #11 0x55ccf078af2d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:2920:35
    #12 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #13 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #14 0x55ccf0406d12 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #15 0x55ccf0404631 in method_vectorcall /home/realkumaraditya/cpython/Objects/classobject.c:95:18
    #16 0x55ccf077790d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:2616:32
    #17 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #18 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #19 0x55ccf03fb440 in _PyObject_VectorcallDictTstate /home/realkumaraditya/cpython/Objects/call.c:135:15
    #20 0x55ccf03fe1b2 in _PyObject_Call_Prepend /home/realkumaraditya/cpython/Objects/call.c:504:24
    #21 0x55ccf05ade31 in call_method /home/realkumaraditya/cpython/Objects/typeobject.c:3076:19
    #22 0x55ccf03fb9f3 in _PyObject_MakeTpCall /home/realkumaraditya/cpython/Objects/call.c:242:18
    #23 0x55ccf078353d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:4021:35
    #24 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #25 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #26 0x55ccf0406d12 in _PyObject_VectorcallTstate /home/realkumaraditya/cpython/./Include/internal/pycore_call.h:169:11
    #27 0x55ccf0404631 in method_vectorcall /home/realkumaraditya/cpython/Objects/classobject.c:95:18
    #28 0x55ccf077790d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:2616:32
    #29 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #30 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12
    #31 0x55ccf03fb440 in _PyObject_VectorcallDictTstate /home/realkumaraditya/cpython/Objects/call.c:135:15
    #32 0x55ccf03fe1b2 in _PyObject_Call_Prepend /home/realkumaraditya/cpython/Objects/call.c:504:24
    #33 0x55ccf05ade31 in call_method /home/realkumaraditya/cpython/Objects/typeobject.c:3076:19
    #34 0x55ccf03fb9f3 in _PyObject_MakeTpCall /home/realkumaraditya/cpython/Objects/call.c:242:18
    #35 0x55ccf078353d in _PyEval_EvalFrameDefault /home/realkumaraditya/cpython/Python/generated_cases.c.h:4021:35
    #36 0x55ccf075c6e7 in _PyEval_EvalFrame /home/realkumaraditya/cpython/./Include/internal/pycore_ceval.h:121:16
    #37 0x55ccf075c6e7 in _PyEval_Vector /home/realkumaraditya/cpython/Python/ceval.c:2001:12

SUMMARY: AddressSanitizer: 4516460 byte(s) leaked in 20 allocation(s).

With fix:

Using random seed: 1947560357
0:00:00 load avg: 1.07 Run 1 test sequentially in a single process
0:00:00 load avg: 1.07 [1/1] test_sys
0:00:16 load avg: 1.12 [1/1] test_sys passed

== Tests result: SUCCESS ==

1 test OK.

Total duration: 16.3 sec
Total tests: run=92 skipped=7
Total test files: run=1/1
Result: SUCCESS
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Oct 14, 2025
View reviewed changes
Comment thread Include/internal/pycore_interp_structs.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303
kumaraditya303 approved these changes Oct 14, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Oct 14, 2025
@kumaraditya303
kumaraditya303 requested a review from ZeroIntensity October 14, 2025 14:25
ZeroIntensity
ZeroIntensity approved these changes Oct 14, 2025
View reviewed changes
@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
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 14, 2025
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Oct 14, 2025
@ashm-dev @kumaraditya303
[3.14] pythongh-140067: Fix memory leak in sub-interpreter creation (p…
a25bab8
…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 <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
@nascheme nascheme mentioned this pull request Oct 14, 2025
TSAN failure with free-threaded build, test_daemon_threads_fatal_error #140138
Closed
@ZeroIntensity ZeroIntensity mentioned this pull request Oct 14, 2025
[3.14] gh-140067: Fix memory leak in sub-interpreter creation (GH-140111) #140118
Merged
ZeroIntensity added a commit to ZeroIntensity/cpython that referenced this pull request Oct 14, 2025
@ZeroIntensity
Revert "pythongh-140067: Fix memory leak in sub-interpreter creation (p…
eac1c71
…ython#140111)"

This reverts commit 59547a2.
@ZeroIntensity

ZeroIntensity commented Oct 15, 2025

Copy link
Copy Markdown
Member

It seems this broke the TSan CI, see #140138. I have a revert up at #140140.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303 pushed a commit that referenced this pull request Oct 15, 2025
@ZeroIntensity
Revert "gh-140067: Fix memory leak in sub-interpreter creation (#140111…
0bcb1c2
…)" (#140140)

This reverts commit 59547a2.
@ashm-dev
ashm-dev restored the gh-140067 branch October 15, 2025 07:13
@ashm-dev ashm-dev mentioned this pull request Oct 16, 2025
gh-140138: NOGIL: add atomic owner refcount to PyInterpreterState; finalize on last tstate #140224
Closed
kumaraditya303 added a commit to kumaraditya303/cpython that referenced this pull request Oct 17, 2025
@ashm-dev @kumaraditya303
pythongh-140067: Fix memory leak in sub-interpreter creation (python#…
a775296
…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 <[email protected]>
kumaraditya303 added a commit that referenced this pull request Oct 18, 2025
@kumaraditya303
gh-140067: Fix memory leak in sub-interpreter creation (#140111) (#14…
58c44c2
…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 <[email protected]>
kumaraditya303 added a commit that referenced this pull request Oct 18, 2025
@kumaraditya303 @ashm-dev
[3.14] gh-140067: Fix memory leak in sub-interpreter creation (GH-140111
1d11627
) (#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 <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@ashm-dev @kumaraditya303 @StanFromIreland
pythongh-140067: Fix memory leak in sub-interpreter creation (python#…
247c974
…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 <[email protected]>
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@ZeroIntensity @StanFromIreland
Revert "pythongh-140067: Fix memory leak in sub-interpreter creation (p…
01c0ad6
…ython#140111)" (python#140140)

This reverts commit 59547a2.
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@kumaraditya303 @StanFromIreland
pythongh-140067: Fix memory leak in sub-interpreter creation (python#…
eab8105
…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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@kumaraditya303 kumaraditya303

Labels

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

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Memory leak in test_sys with subinterpreters creation (AddressSanitizer detection)

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ashm-dev @kumaraditya303 @ZeroIntensity
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.