https://github.com/python/cpython/commit/71126ab19c76d8227fc3b9c7c59b957ca82af4dd
commit: 71126ab19c76d8227fc3b9c7c59b957ca82af4dd
branch: main
author: Sam Gross <[email protected]>
committer: colesbury <[email protected]>
date: 2025-11-24T11:19:07-05:00
summary:
gh-129441: Fix some flakiness in test_instrumentation (gh-141881)
Most of the `self.assertTrue(self.called)` checks are flaky because
the worker threads may sometimes finish before the main thread calls
`self.during_threads()`.
files:
M Lib/test/test_free_threading/test_monitoring.py
diff --git a/Lib/test/test_free_threading/test_monitoring.py
b/Lib/test/test_free_threading/test_monitoring.py
index 4fbd3f3415cb32..2cd6e7b035ecb4 100644
--- a/Lib/test/test_free_threading/test_monitoring.py
+++ b/Lib/test/test_free_threading/test_monitoring.py
@@ -35,10 +35,10 @@ def work(self, n, funcs):
return n
return self.work(n - 1, funcs) + self.work(n - 2, funcs)
- def start_work(self, n, funcs):
+ def start_work(self, n, funcs, barrier):
# With the GIL builds we need to make sure that the hooks have
# a chance to run as it's possible to run w/o releasing the GIL.
- time.sleep(0.1)
+ barrier.wait()
self.work(n, funcs)
def after_test(self):
@@ -53,14 +53,16 @@ def test_instrumentation(self):
exec("def f(): pass", x)
funcs.append(x["f"])
+ barrier = Barrier(self.thread_count + 1)
threads = []
for i in range(self.thread_count):
# Each thread gets a copy of the func list to avoid contention
- t = Thread(target=self.start_work, args=(self.fib, list(funcs)))
+ t = Thread(target=self.start_work, args=(self.fib, list(funcs),
barrier))
t.start()
threads.append(t)
self.after_threads()
+ barrier.wait()
while True:
any_alive = False
@@ -120,7 +122,6 @@ class MonitoringMultiThreaded(
def setUp(self):
super().setUp()
self.set = False
- self.called = False
monitoring.register_callback(
self.tool_id, monitoring.events.LINE, self.callback
)
@@ -130,10 +131,7 @@ def tearDown(self):
super().tearDown()
def callback(self, *args):
- self.called = True
-
- def after_test(self):
- self.assertTrue(self.called)
+ pass
def during_threads(self):
if self.set:
@@ -151,16 +149,11 @@ class
SetTraceMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
def setUp(self):
self.set = False
- self.called = False
-
- def after_test(self):
- self.assertTrue(self.called)
def tearDown(self):
sys.settrace(None)
def trace_func(self, frame, event, arg):
- self.called = True
return self.trace_func
def during_threads(self):
@@ -177,16 +170,11 @@ class
SetProfileMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
def setUp(self):
self.set = False
- self.called = False
-
- def after_test(self):
- self.assertTrue(self.called)
def tearDown(self):
sys.setprofile(None)
def trace_func(self, frame, event, arg):
- self.called = True
return self.trace_func
def during_threads(self):
@@ -203,16 +191,11 @@ class
SetProfileAllThreadsMultiThreaded(InstrumentationMultiThreadedMixin, TestC
def setUp(self):
self.set = False
- self.called = False
-
- def after_test(self):
- self.assertTrue(self.called)
def tearDown(self):
threading.setprofile_all_threads(None)
def trace_func(self, frame, event, arg):
- self.called = True
return self.trace_func
def during_threads(self):
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]