Skip to content

gh-144446: Fix thread-safety of FrameLocalsProxy on executing frames - #156973

Merged
kumaraditya303 merged 8 commits into
python:mainfrom
kumaraditya303:gh-144446-frame-locals
Nov 25, 2026
Merged

kumaraditya303 merged 8 commits into
python:mainfrom
kumaraditya303:gh-144446-frame-locals

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Synchronize FrameLocalsProxy accesses with the frame's owning thread: fast locals are accessed under the frame object's critical section, and with the world stopped when the frame may be executing on another thread.

Changes:

  • Only the fast locals read and write run in the synchronized region. Key lookup (__hash__ / __eq__) runs outside it, so no Python code is executed while the world is stopped.
  • The extra locals dict is never touched by the running frame, so it does not need stop-the-world. It is fetched under the frame's critical section and used with the dict's own locking.
  • The value replaced in a cell is decref'd after the region, since its destructor can run arbitrary code.
  • keys(), values(), items() and len() snapshot the fast locals in the region and hold the extra locals dict's critical section while iterating it.
  • PyFrame_GetVar is synchronized the same way.

Co-authored-by: Sam Gross colesbury@gmail.com

…rames

Synchronize FrameLocalsProxy accesses with the frame's owning thread:
accessors run under the frame object's critical section and stop the
world when the frame is executing on another thread.

Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app bedevere-app Bot mentioned this pull request Sep 5, 2026
3 tasks done
@kumaraditya303
kumaraditya303 marked this pull request as ready for review September 9, 2026 16:14
@kumaraditya303
kumaraditya303 requested a review from mpage September 9, 2026 16:15
Comment thread Objects/frameobject.c Resolved

@mpage mpage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Putting this back on your queue

ambv
ambv previously requested changes Oct 21, 2026

@ambv ambv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think Matt meant to request changes.

@bedevere-app

bedevere-app Bot commented Oct 21, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@kumaraditya303 kumaraditya303 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 9, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit e94a203 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F156973%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 9, 2026
@kumaraditya303

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

@bedevere-app

bedevere-app Bot commented Nov 9, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@ambv, @mpage: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested review from ambv and mpage November 9, 2026 12:43
…cleared frames

Allow a NULL free variable slot in framelocalsproxy_setfast_lock_held:
the frame may have been cleared.
@kumaraditya303
kumaraditya303 dismissed ambv’s stale review November 25, 2026 12:01

review addressed

@kumaraditya303

Copy link
Copy Markdown
Contributor Author

I've addressed the review: key lookup (__hash__ / __eq__) now runs outside the stop-the-world region, so no Python code executes while the world is stopped. I added tests with custom keys defining __hash__ and __eq__ to cover this.

@kumaraditya303
kumaraditya303 merged commit 7a065be into python:main Nov 25, 2026
60 of 61 checks passed
@kumaraditya303
kumaraditya303 deleted the gh-144446-frame-locals branch November 25, 2026 12:43
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.

4 participants