From 77f83efbf6c185e4ac01b54c9f06300d2b157070 Mon Sep 17 00:00:00 2001 From: Timur Vural Date: Fri, 30 Oct 2026 22:30:00 +0000 Subject: [PATCH 1/2] gh-160986: Sample popped frames only after leaf() is running test_popping_python_frame_is_not_native attached as soon as the child process could be read, then took a fixed 10,000 samples. On a slow startup those samples all landed before leaf(), so the test observed zero frames. --- Lib/test/test_external_inspection.py | 29 +++++++++++++++---- ...-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst | 3 ++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index df0af14d9f75622..c930bb81f86146f 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2033,14 +2033,31 @@ def leaf(depth): leaf(300) """ with _managed_subprocess([sys.executable, "-c", script]) as process: - for _ in busy_retry(SHORT_TIMEOUT): + # RemoteUnwinder can attach during interpreter startup. Wait + # until leaf() is on the stack so the sample window observes it. + unwinder = None + for _ in busy_retry( + SHORT_TIMEOUT, + "child did not reach leaf() before sampling", + ): + if unwinder is None: + try: + unwinder = RemoteUnwinder( + process.pid, + native=True, + gc=False, + cache_frames=False, + ) + except RuntimeError: + continue try: - unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, - ) - except RuntimeError: + threads = unwinder.get_stack_trace()[0].threads + except TRANSIENT_ERRORS: continue - break + if threads and any( + frame.funcname == "leaf" for frame in threads[0].frame_info + ): + break samples = 0 for _ in range(10_000): try: diff --git a/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst b/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst new file mode 100644 index 000000000000000..5c4fcaf0fc46be3 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst @@ -0,0 +1,3 @@ +Fix a race in ``test_external_inspection`` where +``test_popping_python_frame_is_not_native`` could spend its whole sample +window during interpreter startup, before the child reached ``leaf()``. From c15c9ba4e96d43c910b7c8b9701ac79ffd24dd0a Mon Sep 17 00:00:00 2001 From: Timur Vural Date: Fri, 30 Oct 2026 17:27:28 -0700 Subject: [PATCH 2/2] Remove NEWS entry per review feedback. --- .../next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst diff --git a/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst b/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst deleted file mode 100644 index 5c4fcaf0fc46be3..000000000000000 --- a/Misc/NEWS.d/next/Tests/2026-10-30-22-21-00.gh-issue-160986.k4Nw8p.rst +++ /dev/null @@ -1,3 +0,0 @@ -Fix a race in ``test_external_inspection`` where -``test_popping_python_frame_is_not_native`` could spend its whole sample -window during interpreter startup, before the child reached ``leaf()``.