Skip to content

[3.13] GH-116738: document thread-safety of bisect (GH-136555) #137222

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 2 commits into from
Jul 30, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
add back run_concurrently
  • Loading branch information
kumaraditya303 committed Jul 30, 2025
commit 10d6017504e91556717f1c844a3fc53055776d59
25 changes: 24 additions & 1 deletion Lib/test/test_free_threading/test_bisect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from test.support import import_helper, threading_helper
import random
from threading import Thread, Barrier

py_bisect = import_helper.import_fresh_module('bisect', blocked=['_bisect'])
c_bisect = import_helper.import_fresh_module('bisect', fresh=['_bisect'])
Expand All @@ -18,7 +19,7 @@ def insert(data):
insert_method(data, x)

data = list(range(OBJECT_COUNT))
threading_helper.run_concurrently(
self.run_concurrently(
worker_func=insert, args=(data,), nthreads=NTHREADS
)
if False:
Expand All @@ -41,6 +42,28 @@ def is_sorted_ascending(lst):
"""
return all(lst[i - 1] <= lst[i] for i in range(1, len(lst)))

def run_concurrently(self, worker_func, args, nthreads):
"""
Run the worker function concurrently in multiple threads.
"""
barrier = Barrier(nthreads)

def wrapper_func(*args):
# Wait for all threads to reach this point before proceeding.
barrier.wait()
worker_func(*args)

with threading_helper.catch_threading_exception() as cm:
workers = (
Thread(target=wrapper_func, args=args)
for _ in range(nthreads)
)
with threading_helper.start_threads(workers):
pass

# Worker threads should not raise any exceptions
self.assertIsNone(cm.exc_value)


@threading_helper.requires_working_threading()
class TestPyBisect(unittest.TestCase, TestBase):
Expand Down
Loading