Skip to content

gh-158364: Don't report other interpreters' threads in sys._current_f… - #158369

Merged
ZeroIntensity merged 1 commit into
python:mainfrom
Himesh-rupchandani:gh-158364
Sep 30, 2026
Merged

ZeroIntensity merged 1 commit into
python:mainfrom
Himesh-rupchandani:gh-158364

Conversation

@Himesh-rupchandani

@Himesh-rupchandani Himesh-rupchandani commented Sep 28, 2026 •

Copy link
Copy Markdown

…rames()

sys._current_frames() materialized a PyFrameObject for every thread of every interpreter. A frame object created for a thread of another interpreter belongs to that interpreter: it is stored in its _PyInterpreterFrame.frame_obj and deallocated when that interpreter pops the frame, while the calling interpreter holds the reference from the returned dict and drops it at some arbitrary later time. Since PEP 684 gives each interpreter its own obmalloc arenas, the block ends up being freed by a different interpreter than the one that allocated it, which corrupts the heap and typically aborts the process inside free().

sys._current_exceptions() has the same problem: it hands out references to exception objects owned by other interpreters.

Both functions now only report the threads of the calling interpreter. This matches PyUnstable_DumpTracebackThreads() (used by faulthandler), which already only dumps the threads of one interpreter. As a consequence, only the current interpreter's world needs to be stopped.

@read-the-docs-community

read-the-docs-community Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34818209 | 📁 Comparing e05c195 against main (3330712)

  🔍 Preview build  

2 files changed
± library/sys.html
± whatsnew/changelog.html

Comment thread Doc/library/sys.rst Outdated
Comment thread Doc/library/sys.rst Outdated
Comment thread Lib/test/test_sys.py Outdated
Comment thread Lib/test/test_sys.py Outdated
Comment thread Lib/test/test_sys.py Outdated
Comment thread Python/pystate.c Outdated
Comment thread Python/pystate.c Outdated
Comment thread Python/pystate.c Outdated
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-09-28-18-58-27.gh-issue-158364.jBqmTs.rst Outdated
Comment thread Lib/test/test_sys.py Outdated
@Himesh-rupchandani

Copy link
Copy Markdown
Author

Thanks for the review! Force-pushed with all of it addressed:

Both versionchanged blocks are gone — Doc/library/sys.rst is untouched now.
NEWS entry shortened to your wording.
Dropped the _Py_EnsureTstateNotNULL() call and both long comments from
_PyThread_CurrentFrames() / _PyThread_CurrentExceptions(). The loop
comments now just say "the current interpreter's thread states" to match the
new loop.
Deleted test_current_frames_subinterpreter_thread.
Both remaining tests now use threading_helper.start_threads(..., unlock=left.set), wait without timeouts, use entered/left events, and
if True: instead of textwrap.dedent.
The PR is now 3 files, +114/-52.

@Himesh-rupchandani

Copy link
Copy Markdown
Author

A thread only shows up in sys._current_exceptions() with something other
than None while it is actively handling an exception, so the thread has to
be parked inside an except block — otherwise the subinterpreter's call has
nothing of ours to hand out and the test doesn't exercise the broken path.
entered/left just keep it there while the subinterpreter samples. I've
added a one-line comment saying so.

…rent_frames()

sys._current_frames() materialized a PyFrameObject for every thread of
every interpreter.  A frame object created for a thread of another
interpreter belongs to that interpreter: it is stored in its
_PyInterpreterFrame.frame_obj and deallocated when that interpreter pops
the frame, while the calling interpreter holds the reference from the
returned dict and drops it at some arbitrary later time.  Since PEP 684
gives each interpreter its own obmalloc arenas, the block ends up being
freed by a different interpreter than the one that allocated it, which
corrupts the heap and typically aborts the process inside free().

sys._current_exceptions() has the same problem: it hands out references
to exception objects owned by other interpreters.

Both functions now only report the threads of the calling interpreter.
This matches PyUnstable_DumpTracebackThreads() (used by faulthandler),
which already only dumps the threads of one interpreter.  As a
consequence, only the current interpreter's world needs to be stopped.

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, congrats on your first contribution!

@ZeroIntensity ZeroIntensity added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 30, 2026
@ZeroIntensity
ZeroIntensity merged commit c3cb468 into python:main Sep 30, 2026
65 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @Himesh-rupchandani for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Sep 30, 2026

Copy link
Copy Markdown

GH-158471 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 30, 2026
@miss-islington-app

Copy link
Copy Markdown

Sorry, @Himesh-rupchandani and @ZeroIntensity, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c3cb46870bdb18d104557d8b69d451a692c64c8a 3.13

@bedevere-app

bedevere-app Bot commented Sep 30, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 30, 2026
@bedevere-app

bedevere-app Bot commented Sep 30, 2026

Copy link
Copy Markdown

GH-158473 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 30, 2026
ZeroIntensity added a commit that referenced this pull request Sep 30, 2026
…urrent_frames` (GH-158369) (GH-158473)

(cherry picked from commit c3cb468)

Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
hugovk pushed a commit that referenced this pull request Sep 30, 2026
…urrent_frames` (GH-158369) (#158471)

Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
w-hsiung pushed a commit to w-hsiung/cpython that referenced this pull request Sep 30, 2026
…sys._current_frames` (pythonGH-158369) (pythonGH-158472)

(cherry picked from commit c3cb468)

Co-authored-by: Himesh Rupchandani <rupchandanihimesh69@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants