Skip to content

Commit ba9c198

Browse files
authored
pythongh-136186: Fix race condition in test_external_inspection.test_only_active_thread (python#136347)
1 parent 0152df5 commit ba9c198

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Lib/test/test_external_inspection.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import socket
77
import threading
8+
import time
89
from asyncio import staggered, taskgroups, base_events, tasks
910
from unittest.mock import ANY
1011
from test.support import os_helper, SHORT_TIMEOUT, busy_retry, requires_gil_enabled
@@ -930,9 +931,6 @@ def main_work():
930931
# Signal threads to start waiting
931932
ready_event.set()
932933
933-
# Give threads time to start sleeping
934-
time.sleep(0.1)
935-
936934
# Now do busy work to hold the GIL
937935
main_work()
938936
"""
@@ -967,7 +965,23 @@ def main_work():
967965

968966
# Get stack trace with all threads
969967
unwinder_all = RemoteUnwinder(p.pid, all_threads=True)
970-
all_traces = unwinder_all.get_stack_trace()
968+
for _ in range(10):
969+
# Wait for the main thread to start its busy work
970+
all_traces = unwinder_all.get_stack_trace()
971+
found = False
972+
for thread_id, stack in all_traces:
973+
if not stack:
974+
continue
975+
current_frame = stack[0]
976+
if current_frame.funcname == "main_work" and current_frame.lineno >15:
977+
found = True
978+
979+
if found:
980+
break
981+
# Give a bit of time to take the next sample
982+
time.sleep(0.1)
983+
else:
984+
self.fail("Main thread did not start its busy work on time")
971985

972986
# Get stack trace with only GIL holder
973987
unwinder_gil = RemoteUnwinder(p.pid, only_active_thread=True)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy