Skip to content

Conversation

@colesbury
Copy link
Contributor

@colesbury colesbury commented Feb 4, 2026

Fix thread-safety issues when accessing frame attributes while another thread is executing the frame:

  • Add critical section to frame_repr() to prevent races when accessing the frame's code object and line number

  • Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti() to allow intentional racy reads of instr_ptr.

  • Fix take_ownership() to not write to the original frame's f_executable

Fix thread-safety issues when accessing frame attributes while another
thread is executing the frame:

- Add critical section to frame_repr() to prevent races when accessing
  the frame's code object and line number

- Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti()
  to allow intentional racy reads of instr_ptr.

- Fix take_ownership() to not write to the original frame's f_executable
@colesbury
Copy link
Contributor Author

There are still some races when accessing variables through frame.f_locals, which will be address separately.

@colesbury colesbury marked this pull request as ready for review February 4, 2026 18:08
@colesbury colesbury requested a review from mpage February 4, 2026 18:37
@colesbury colesbury added the needs backport to 3.14 bugs and security fixes label Feb 4, 2026
Copy link
Contributor

@mpage mpage left a comment

Choose a reason for hiding this comment

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

LGTM

@colesbury colesbury merged commit 5bb3bbb into python:main Feb 6, 2026
51 checks passed
@colesbury colesbury deleted the gh-144446-frame-free-threading branch February 6, 2026 14:43
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 6, 2026
…-144479)

Fix thread-safety issues when accessing frame attributes while another
thread is executing the frame:

- Add critical section to frame_repr() to prevent races when accessing
  the frame's code object and line number

- Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti()
  to allow intentional racy reads of instr_ptr.

- Fix take_ownership() to not write to the original frame's f_executable
(cherry picked from commit 5bb3bbb9c6a7c9043a04d0cc2e82c83747040788)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Feb 6, 2026

GH-144546 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 Feb 6, 2026
colesbury added a commit that referenced this pull request Feb 6, 2026
…) (#144546)

Fix thread-safety issues when accessing frame attributes while another
thread is executing the frame:

- Add critical section to frame_repr() to prevent races when accessing
  the frame's code object and line number

- Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti()
  to allow intentional racy reads of instr_ptr.

- Fix take_ownership() to not write to the original frame's f_executable
(cherry picked from commit 5bb3bbb)

Co-authored-by: Sam Gross <colesbury@gmail.com>
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