Skip to content
Open
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
15 changes: 15 additions & 0 deletions Lib/test/test_external_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<pid>/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):
with open(f"/proc/{pid}/cmdline", "rb") as f:
if cmdline in f.read():
break


@contextmanager
def _managed_subprocess(args, timeout=SHORT_TIMEOUT):
"""
Expand Down Expand Up @@ -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":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could find a portable solution to fix also the issue on other platforms.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think the portable solution is the earlier socket-communication version in this PR. The child sends a b"ready" signal, which can only happen after execve(). It works on Linux/macOS/Windows. I'm happy to switch back to that version if you prefer portability.

_wait_for_process_exec(process.pid, script.encode())
for _ in busy_retry(SHORT_TIMEOUT):
try:
unwinder = RemoteUnwinder(
Expand Down
Loading