From 060c525fe2bede192189a5e8a00a98ea333d14a7 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 19 Nov 2026 18:43:45 +0800 Subject: [PATCH 01/10] fix: create unwinder after execve --- Lib/test/test_external_inspection.py | 81 ++++++++++++++++------------ 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 5aebe1fb6b18e9..cfb726681aad39 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2068,46 +2068,61 @@ def test_empty_native_thread_stack(self): "Test only runs on Linux with process_vm_readv support", ) def test_popping_python_frame_is_not_native(self): - script = """\ + port = find_unused_port() + script = f"""\ +import socket + def leaf(depth): if depth: leaf(depth - 1) +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +sock.connect(('localhost', {port})) +sock.sendall(b"ready") while True: leaf(300) """ - with _managed_subprocess([sys.executable, "-c", script]) as process: - for _ in busy_retry(SHORT_TIMEOUT): - try: - unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, - ) - except RuntimeError: - continue - break - # The unwinder can attach before the main loop starts. - _get_stack_trace_with_retry( - unwinder, - condition=lambda trace: trace[0].threads and any( - frame.funcname == "leaf" - for frame in trace[0].threads[0].frame_info - ), - ) - samples = 0 - for _ in range(10_000): - try: - threads = unwinder.get_stack_trace()[0].threads - except TRANSIENT_ERRORS: - continue - if not threads: - continue - frames = threads[0].frame_info - names = [frame.funcname for frame in frames] - if "leaf" not in names: - continue - samples += 1 - self.assertNotIn(("leaf", ""), zip(names, names[1:])) - self.assertGreater(samples, 1000) + server_socket = _create_server_socket(port) + client_socket = None + try: + with _managed_subprocess([sys.executable, "-c", script]) as process: + client_socket, _ = server_socket.accept() + server_socket.close() + _wait_for_signal(client_socket, b"ready") + + for _ in busy_retry(SHORT_TIMEOUT): + try: + unwinder = RemoteUnwinder( + process.pid, native=True, gc=False, cache_frames=False, + ) + except RuntimeError: + continue + break + # The unwinder can attach before the main loop starts. + _get_stack_trace_with_retry( + unwinder, + condition=lambda trace: trace[0].threads and any( + frame.funcname == "leaf" + for frame in trace[0].threads[0].frame_info + ), + ) + samples = 0 + for _ in range(10_000): + try: + threads = unwinder.get_stack_trace()[0].threads + except TRANSIENT_ERRORS: + continue + if not threads: + continue + frames = threads[0].frame_info + names = [frame.funcname for frame in frames] + if "leaf" not in names: + continue + samples += 1 + self.assertNotIn(("leaf", ""), zip(names, names[1:])) + self.assertGreater(samples, 1000) + finally: + _cleanup_sockets(server_socket, client_socket) @skip_if_not_supported @unittest.skipIf( From c4afe6799f52a4f03d9b8e82614411ea53064de5 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Thu, 19 Nov 2026 20:16:39 +0800 Subject: [PATCH 02/10] style: break lone lines --- Lib/test/test_external_inspection.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index cfb726681aad39..caccd17fa5b73e 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2085,7 +2085,9 @@ def leaf(depth): server_socket = _create_server_socket(port) client_socket = None try: - with _managed_subprocess([sys.executable, "-c", script]) as process: + with _managed_subprocess( + [sys.executable, "-c", script] + ) as process: client_socket, _ = server_socket.accept() server_socket.close() _wait_for_signal(client_socket, b"ready") @@ -2093,7 +2095,8 @@ def leaf(depth): for _ in busy_retry(SHORT_TIMEOUT): try: unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, + process.pid, native=True, gc=False, + cache_frames=False, ) except RuntimeError: continue @@ -2119,7 +2122,8 @@ def leaf(depth): if "leaf" not in names: continue samples += 1 - self.assertNotIn(("leaf", ""), zip(names, names[1:])) + self.assertNotIn(("leaf", ""), + zip(names, names[1:])) self.assertGreater(samples, 1000) finally: _cleanup_sockets(server_socket, client_socket) From ad9d76c3e16db41c656b617c39a8e381b5963eb2 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 01:34:21 +0800 Subject: [PATCH 03/10] Revert "style: break lone lines" This reverts commit c4afe6799f52a4f03d9b8e82614411ea53064de5. --- Lib/test/test_external_inspection.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index caccd17fa5b73e..cfb726681aad39 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2085,9 +2085,7 @@ def leaf(depth): server_socket = _create_server_socket(port) client_socket = None try: - with _managed_subprocess( - [sys.executable, "-c", script] - ) as process: + with _managed_subprocess([sys.executable, "-c", script]) as process: client_socket, _ = server_socket.accept() server_socket.close() _wait_for_signal(client_socket, b"ready") @@ -2095,8 +2093,7 @@ def leaf(depth): for _ in busy_retry(SHORT_TIMEOUT): try: unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, - cache_frames=False, + process.pid, native=True, gc=False, cache_frames=False, ) except RuntimeError: continue @@ -2122,8 +2119,7 @@ def leaf(depth): if "leaf" not in names: continue samples += 1 - self.assertNotIn(("leaf", ""), - zip(names, names[1:])) + self.assertNotIn(("leaf", ""), zip(names, names[1:])) self.assertGreater(samples, 1000) finally: _cleanup_sockets(server_socket, client_socket) From c1d8c39269e956eebe62d08ca8f0bece1ecde154 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 01:34:35 +0800 Subject: [PATCH 04/10] Revert "fix: create unwinder after execve" This reverts commit 060c525fe2bede192189a5e8a00a98ea333d14a7. --- Lib/test/test_external_inspection.py | 81 ++++++++++++---------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index cfb726681aad39..5aebe1fb6b18e9 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2068,61 +2068,46 @@ def test_empty_native_thread_stack(self): "Test only runs on Linux with process_vm_readv support", ) def test_popping_python_frame_is_not_native(self): - port = find_unused_port() - script = f"""\ -import socket - + script = """\ def leaf(depth): if depth: leaf(depth - 1) -sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -sock.connect(('localhost', {port})) -sock.sendall(b"ready") while True: leaf(300) """ - server_socket = _create_server_socket(port) - client_socket = None - try: - with _managed_subprocess([sys.executable, "-c", script]) as process: - client_socket, _ = server_socket.accept() - server_socket.close() - _wait_for_signal(client_socket, b"ready") - - for _ in busy_retry(SHORT_TIMEOUT): - try: - unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, - ) - except RuntimeError: - continue - break - # The unwinder can attach before the main loop starts. - _get_stack_trace_with_retry( - unwinder, - condition=lambda trace: trace[0].threads and any( - frame.funcname == "leaf" - for frame in trace[0].threads[0].frame_info - ), - ) - samples = 0 - for _ in range(10_000): - try: - threads = unwinder.get_stack_trace()[0].threads - except TRANSIENT_ERRORS: - continue - if not threads: - continue - frames = threads[0].frame_info - names = [frame.funcname for frame in frames] - if "leaf" not in names: - continue - samples += 1 - self.assertNotIn(("leaf", ""), zip(names, names[1:])) - self.assertGreater(samples, 1000) - finally: - _cleanup_sockets(server_socket, client_socket) + with _managed_subprocess([sys.executable, "-c", script]) as process: + for _ in busy_retry(SHORT_TIMEOUT): + try: + unwinder = RemoteUnwinder( + process.pid, native=True, gc=False, cache_frames=False, + ) + except RuntimeError: + continue + break + # The unwinder can attach before the main loop starts. + _get_stack_trace_with_retry( + unwinder, + condition=lambda trace: trace[0].threads and any( + frame.funcname == "leaf" + for frame in trace[0].threads[0].frame_info + ), + ) + samples = 0 + for _ in range(10_000): + try: + threads = unwinder.get_stack_trace()[0].threads + except TRANSIENT_ERRORS: + continue + if not threads: + continue + frames = threads[0].frame_info + names = [frame.funcname for frame in frames] + if "leaf" not in names: + continue + samples += 1 + self.assertNotIn(("leaf", ""), zip(names, names[1:])) + self.assertGreater(samples, 1000) @skip_if_not_supported @unittest.skipIf( From a63e693e5fbf2534212ef266058b484a86f5dbae Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 01:36:17 +0800 Subject: [PATCH 05/10] wait for execve() through /proc/pid/cmdline --- Lib/test/test_external_inspection.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 5aebe1fb6b18e9..05a5d379479560 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -18,6 +18,7 @@ busy_retry, requires_gil_enabled, requires_remote_subprocess_debugging, + sleeping_retry, ) from test.support.import_helper import import_module from test.support.script_helper import make_script @@ -165,6 +166,17 @@ def _wait_for_n_signals(sock, signal_pattern, count, timeout=SHORT_TIMEOUT): return buffer +def _wait_for_process_exec(pid, cmdline, timeout=SHORT_TIMEOUT): + # /proc//cmdline is still the parent's command line until + # the forked process calls execve(). Wait until the new command line is + # visible before inspecting the process with RemoteUnwinder. Raise + # AssertionError on timeout. + for _ in sleeping_retry(timeout, max_delay=0.1): + with open(f"/proc/{pid}/cmdline", "rb") as f: + if cmdline in f.read(): + break + + @contextmanager def _managed_subprocess(args, timeout=SHORT_TIMEOUT): """ @@ -2077,6 +2089,9 @@ def leaf(depth): leaf(300) """ with _managed_subprocess([sys.executable, "-c", script]) as process: + # On Linux, wait until execve() replaced the forked process image. + if sys.platform == "linux": + _wait_for_process_exec(process.pid, script.encode()) for _ in busy_retry(SHORT_TIMEOUT): try: unwinder = RemoteUnwinder( From 7b29d569ba9212acb3cc98d9f36ac8decf40589b Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 01:56:32 +0800 Subject: [PATCH 06/10] Update Lib/test/test_external_inspection.py Co-authored-by: Victor Stinner --- Lib/test/test_external_inspection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 05a5d379479560..2921ea2d4dddb3 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -171,7 +171,7 @@ def _wait_for_process_exec(pid, cmdline, timeout=SHORT_TIMEOUT): # the forked process calls execve(). Wait until the new command line is # visible before inspecting the process with RemoteUnwinder. Raise # AssertionError on timeout. - for _ in sleeping_retry(timeout, max_delay=0.1): + for _ in sleeping_retry(timeout): with open(f"/proc/{pid}/cmdline", "rb") as f: if cmdline in f.read(): break From 655df5b97702f62ff6c8a777c2cf607e4e04576a Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 12:19:51 +0800 Subject: [PATCH 07/10] Revert "Update Lib/test/test_external_inspection.py" This reverts commit 7b29d569ba9212acb3cc98d9f36ac8decf40589b. --- Lib/test/test_external_inspection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 2921ea2d4dddb3..05a5d379479560 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -171,7 +171,7 @@ def _wait_for_process_exec(pid, cmdline, timeout=SHORT_TIMEOUT): # the forked process calls execve(). Wait until the new command line is # visible before inspecting the process with RemoteUnwinder. Raise # AssertionError on timeout. - for _ in sleeping_retry(timeout): + for _ in sleeping_retry(timeout, max_delay=0.1): with open(f"/proc/{pid}/cmdline", "rb") as f: if cmdline in f.read(): break From a433131e479e4b409b58aa11a85c99096147fced Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 12:20:07 +0800 Subject: [PATCH 08/10] Revert "wait for execve() through /proc/pid/cmdline" This reverts commit a63e693e5fbf2534212ef266058b484a86f5dbae. --- Lib/test/test_external_inspection.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 05a5d379479560..5aebe1fb6b18e9 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -18,7 +18,6 @@ busy_retry, requires_gil_enabled, requires_remote_subprocess_debugging, - sleeping_retry, ) from test.support.import_helper import import_module from test.support.script_helper import make_script @@ -166,17 +165,6 @@ def _wait_for_n_signals(sock, signal_pattern, count, timeout=SHORT_TIMEOUT): return buffer -def _wait_for_process_exec(pid, cmdline, timeout=SHORT_TIMEOUT): - # /proc//cmdline is still the parent's command line until - # the forked process calls execve(). Wait until the new command line is - # visible before inspecting the process with RemoteUnwinder. Raise - # AssertionError on timeout. - for _ in sleeping_retry(timeout, max_delay=0.1): - with open(f"/proc/{pid}/cmdline", "rb") as f: - if cmdline in f.read(): - break - - @contextmanager def _managed_subprocess(args, timeout=SHORT_TIMEOUT): """ @@ -2089,9 +2077,6 @@ def leaf(depth): leaf(300) """ with _managed_subprocess([sys.executable, "-c", script]) as process: - # On Linux, wait until execve() replaced the forked process image. - if sys.platform == "linux": - _wait_for_process_exec(process.pid, script.encode()) for _ in busy_retry(SHORT_TIMEOUT): try: unwinder = RemoteUnwinder( From 018e1ec0e7519ebac43ce684c4fbf214b4d17154 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 12:22:29 +0800 Subject: [PATCH 09/10] use _target_processing() --- Lib/test/test_external_inspection.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 5aebe1fb6b18e9..c0ac1964d3cb27 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2068,23 +2068,19 @@ def test_empty_native_thread_stack(self): "Test only runs on Linux with process_vm_readv support", ) def test_popping_python_frame_is_not_native(self): - script = """\ -def leaf(depth): - if depth: - leaf(depth - 1) + script_body = """\ + def leaf(depth): + if depth: + leaf(depth - 1) -while True: - leaf(300) -""" - with _managed_subprocess([sys.executable, "-c", script]) as process: - for _ in busy_retry(SHORT_TIMEOUT): - try: - unwinder = RemoteUnwinder( - process.pid, native=True, gc=False, cache_frames=False, - ) - except RuntimeError: - continue - break + while True: + leaf(300) + """ + + with self._target_process(script_body) as (process, _, _): + unwinder = RemoteUnwinder( + process.pid, native=True, gc=False, cache_frames=False, + ) # The unwinder can attach before the main loop starts. _get_stack_trace_with_retry( unwinder, From bd204a4475e69979676121b333ebbf26330e352b Mon Sep 17 00:00:00 2001 From: Tan Long Date: Fri, 20 Nov 2026 12:41:36 +0800 Subject: [PATCH 10/10] revert whitespace changes --- Lib/test/test_external_inspection.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index c0ac1964d3cb27..d9cd7ee72fd2b2 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -2069,13 +2069,13 @@ def test_empty_native_thread_stack(self): ) def test_popping_python_frame_is_not_native(self): script_body = """\ - def leaf(depth): - if depth: - leaf(depth - 1) +def leaf(depth): + if depth: + leaf(depth - 1) - while True: - leaf(300) - """ +while True: + leaf(300) +""" with self._target_process(script_body) as (process, _, _): unwinder = RemoteUnwinder(