-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
gh-127020: Make PyCode_GetCode
thread-safe for free threading
#127043
Conversation
Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe.
Hi @colesbury , Lysandros pointed me out to your PR fixing a bug in FT mode. import concurrent.futures
import threading
import inspect
def decorator(f):
# Introspect the callable for optional features.
sig = inspect.signature(f)
for param in sig.parameters.values():
pass
def emit_call_op(*call_args):
pass
wrapped = emit_call_op
return wrapped
def test_dialects_vector_repro_3():
num_workers = 6
num_runs = 10
barrier = threading.Barrier(num_workers)
def closure():
barrier.wait()
for _ in range(num_runs):
@decorator
def print_vector(arg):
return 0
barrier.wait()
with concurrent.futures.ThreadPoolExecutor(
max_workers=num_workers
) as executor:
futures = []
for _ in range(num_workers):
futures.append(executor.submit(closure))
# We should call future.result() to re-raise an exception if test has
# failed
assert len(list(f.result() for f in futures)) == num_workers
if __name__ == "__main__":
test_dialects_vector_repro_3() Outputs (cpython 3.13 built with TSAN):
or
Can you please check and run it in your PR and see if this is also fixed? Thanks a lot! |
@vfdev-5, I verified that it fixed your repro as well. |
Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…pythonGH-127043) Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe. (cherry picked from commit 3926842) Co-authored-by: Sam Gross <colesbury@gmail.com>
GH-127107 is a backport of this pull request to the 3.13 branch. |
…python#127043) Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe.
Some fields in PyCodeObject are lazily initialized. Use atomics and critical sections to make their initializations and accesses thread-safe.
PyCode_GetCode
is not thread-safe and causes assertion fail with Python 3.13td #127020