forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-127020: Make
PyCode_GetCode
thread-safe for free threading
Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe.
- Loading branch information
Showing
3 changed files
with
85 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
|
||
from threading import Thread | ||
from unittest import TestCase | ||
|
||
from test.support import threading_helper | ||
|
||
@threading_helper.requires_working_threading() | ||
class TestCode(TestCase): | ||
def test_code_attrs(self): | ||
"""Test concurrent accesses to lazily initialized code attributes""" | ||
code_objects = [] | ||
for _ in range(1000): | ||
code_objects.append(compile("a + b", "<string>", "eval")) | ||
|
||
def run_in_thread(): | ||
for code in code_objects: | ||
self.assertIsInstance(code.co_code, bytes) | ||
self.assertIsInstance(code.co_freevars, tuple) | ||
self.assertIsInstance(code.co_varnames, tuple) | ||
|
||
threads = [Thread(target=run_in_thread) for _ in range(2)] | ||
for thread in threads: | ||
thread.start() | ||
for thread in threads: | ||
thread.join() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Core_and_Builtins/2024-11-19-21-49-58.gh-issue-127020.5vvI17.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fix a crash in the free threading build when :c:func:`PyCode_GetCode`, | ||
:c:func:`PyCode_GetVarnames`, :c:func:`PyCode_GetCellvars`, or | ||
:c:func:`PyCode_GetFreevars` were called from multiple threads at the same | ||
time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters