Skip to content

gh-157519: Don't defer deallocation on an unknown stack - #157520

Open
JamesWrigley wants to merge 1 commit into
python:mainfrom
JamesWrigley:userspace-stacks
Open

JamesWrigley wants to merge 1 commit into
python:mainfrom
JamesWrigley:userspace-stacks

Conversation

@JamesWrigley

@JamesWrigley JamesWrigley commented Sep 14, 2026

Copy link
Copy Markdown

_Py_RecursionLimit_GetMargin() now reports plenty of margin when the stack pointer is far outside the stack limits, the same rule _Py_CheckRecursiveCall() uses to detect stack switching. Previously _Py_Dealloc() deferred every GC object to the trash queue on such a stack and the objects were never freed.

Fixes #157519. Output of the MWE on my machine:

real stack limits            __del__ calls:  1000/1000   traced memory:     0.0 MiB
stack limits far away        __del__ calls:  1000/1000   traced memory:     0.0 MiB
limits restored              __del__ calls:  1000/1000   traced memory:     0.0 MiB

Written with help from Claude 🤖 Though I reviewed and tested all the changes myself. I think this should be backported to 3.14?

`_Py_RecursionLimit_GetMargin()` now reports plenty of margin when the stack
pointer is far outside the stack limits, the same rule
`_Py_CheckRecursiveCall()` uses to detect stack switching. Previously
`_Py_Dealloc()` deferred every GC object to the trash queue on such a stack and
the chain was never destroyed.
@python-cla-bot

python-cla-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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.

_Py_Dealloc() being unaware of separate stacks can cause memory leaks

1 participant