Skip to content

[3.13] gh-128714: Fix function object races in __annotate__, __annotations__ and __type_params__ in free-threading build (GH-129016) #129729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Lib/test/test_free_threading/test_func_annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import concurrent.futures
import unittest
import inspect
from threading import Thread, Barrier
from unittest import TestCase

from test.support import threading_helper, Py_GIL_DISABLED

threading_helper.requires_working_threading(module=True)


def get_func_annotation(f, b):
b.wait()
return inspect.get_annotations(f)


def get_func_annotation_dunder(f, b):
b.wait()
return f.__annotations__


def set_func_annotation(f, b):
b.wait()
f.__annotations__ = {'x': int, 'y': int, 'return': int}
return f.__annotations__


@unittest.skipUnless(Py_GIL_DISABLED, "Enable only in FT build")
class TestFTFuncAnnotations(TestCase):
NUM_THREADS = 8

def test_concurrent_read(self):
def f(x: int) -> int:
return x + 1

for _ in range(100):
with concurrent.futures.ThreadPoolExecutor(max_workers=self.NUM_THREADS) as executor:
b = Barrier(self.NUM_THREADS)
futures = {executor.submit(get_func_annotation, f, b): i for i in range(self.NUM_THREADS)}
for fut in concurrent.futures.as_completed(futures):
annotate = fut.result()
self.assertIsNotNone(annotate)
self.assertEqual(annotate, {'x': int, 'return': int})

with concurrent.futures.ThreadPoolExecutor(max_workers=self.NUM_THREADS) as executor:
b = Barrier(self.NUM_THREADS)
futures = {executor.submit(get_func_annotation_dunder, f, b): i for i in range(self.NUM_THREADS)}
for fut in concurrent.futures.as_completed(futures):
annotate = fut.result()
self.assertIsNotNone(annotate)
self.assertEqual(annotate, {'x': int, 'return': int})

def test_concurrent_write(self):
def bar(x: int, y: float) -> float:
return y ** x

for _ in range(100):
with concurrent.futures.ThreadPoolExecutor(max_workers=self.NUM_THREADS) as executor:
b = Barrier(self.NUM_THREADS)
futures = {executor.submit(set_func_annotation, bar, b): i for i in range(self.NUM_THREADS)}
for fut in concurrent.futures.as_completed(futures):
annotate = fut.result()
self.assertIsNotNone(annotate)
self.assertEqual(annotate, {'x': int, 'y': int, 'return': int})

# func_get_annotations returns in-place dict, so bar.__annotations__ should be modified as well
self.assertEqual(bar.__annotations__, {'x': int, 'y': int, 'return': int})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the potential races in get/set dunder methods ``__annotations__``, ``__annotate__`` and ``__type_params__`` for function object, and add related tests.
117 changes: 116 additions & 1 deletion Objects/clinic/funcobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading