Skip to content
Merged
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
20 changes: 14 additions & 6 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

# Maximum number of retry attempts for operations that may fail transiently
MAX_TRIES = 10
RETRY_DELAY = 0.1

# Exceptions that can occur transiently when reading from a live process
TRANSIENT_ERRORS = (OSError, RuntimeError, UnicodeDecodeError)

try:
from concurrent import interpreters
Expand Down Expand Up @@ -1713,7 +1717,7 @@ def main_work():
)
if found:
break
time.sleep(0.1)
time.sleep(RETRY_DELAY)
else:
self.fail(
"Main thread did not start its busy work on time"
Expand Down Expand Up @@ -2505,7 +2509,11 @@ def _check_exception_status(self, p, thread_tid, expect_exception):
# Collect multiple samples for reliability
results = []
for _ in range(MAX_TRIES):
traces = unwinder.get_stack_trace()
try:
traces = unwinder.get_stack_trace()
except TRANSIENT_ERRORS:
time.sleep(RETRY_DELAY)
continue
statuses = self._get_thread_statuses(traces)

if thread_tid in statuses:
Expand All @@ -2515,7 +2523,7 @@ def _check_exception_status(self, p, thread_tid, expect_exception):
if len(results) >= 3:
break

time.sleep(0.2)
time.sleep(RETRY_DELAY)

# Check majority of samples match expected
if not results:
Expand Down Expand Up @@ -2648,14 +2656,14 @@ def make_unwinder(cache_frames=True):
def _get_frames_with_retry(self, unwinder, required_funcs):
"""Get frames containing required_funcs, with retry for transient errors."""
for _ in range(MAX_TRIES):
with contextlib.suppress(OSError, RuntimeError):
with contextlib.suppress(*TRANSIENT_ERRORS):
traces = unwinder.get_stack_trace()
for interp in traces:
for thread in interp.threads:
funcs = {f.funcname for f in thread.frame_info}
if required_funcs.issubset(funcs):
return thread.frame_info
time.sleep(0.1)
time.sleep(RETRY_DELAY)
return None

def _sample_frames(
Expand All @@ -2674,7 +2682,7 @@ def _sample_frames(
frames = self._get_frames_with_retry(unwinder, required_funcs)
if frames and len(frames) >= expected_frames:
break
time.sleep(0.1)
time.sleep(RETRY_DELAY)
client_socket.sendall(send_ack)
return frames

Expand Down
Loading