Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import io
import os
import shutil
import signal
import subprocess
import sys
Expand Down Expand Up @@ -85,12 +86,11 @@ def reader():
return result[0]


def _wait_for_process_exec(proc, name, timeout):
def _wait_for_process_exec(proc, executable, timeout):
# Wait until a child process calls exec() to replace the forked process
# executable. Raise AssertionError on timeout.
for _ in sleeping_retry(timeout, max_delay=0.1):
exe = os.readlink(f"/proc/{proc.pid}/exe")
if os.path.basename(exe) == name:
if os.path.samefile(f"/proc/{proc.pid}/exe", executable):
break


Expand Down Expand Up @@ -617,15 +617,18 @@ def test_is_python_process_python_subprocess(self):
@unittest.skipUnless(sys.platform == "linux", "Linux only test")
def test_is_python_process_non_python_subprocess(self):
"""Test that a non-Python subprocess is not detected as Python."""
executable = shutil.which("sleep")
if executable is None:
self.skipTest("sleep command not found")
# Start a non-Python subprocess (sleep command)
proc = subprocess.Popen(
["sleep", "60"],
[executable, "60"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)

try:
_wait_for_process_exec(proc, "sleep", SHORT_TIMEOUT)
_wait_for_process_exec(proc, executable, SHORT_TIMEOUT)

self.assertFalse(
is_python_process(proc.pid),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,24 @@ def test_collapsed_stack_collector_export(self):
self.assertIn(stack1_expected, lines)
self.assertIn(stack2_expected, lines)

def test_collapsed_stack_collector_export_synthetic_frames(self):
collapsed_out = tempfile.NamedTemporaryFile(delete=False)
self.addCleanup(close_and_unlink, collapsed_out)

for name in ("<GC>", "<native>", "<unreadable frame>"):
with self.subTest(name=name):
collector = CollapsedStackCollector(1000)
collector.collect([
MockInterpreterInfo(0, [MockThreadInfo(1, [
_remote_debugging.FrameInfo(("~", None, name, None)),
MockFrameInfo("app.py", 10, "main"),
])])
])
with captured_stdout():
collector.export(collapsed_out.name)
with open(collapsed_out.name, encoding="utf-8") as f:
self.assertEqual(f.read(), f"tid:1;app.py:main:10;{name} 1\n")

def test_collapsed_stack_collector_export_non_ascii_names(self):
# gh-156810: frame names are written verbatim, so the output must be
# opened with an encoding that can represent non-ASCII and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ def test_sampling_with_collapsed_export(self):
if ";" in stack_trace:
stack_parts = stack_trace.split(";")
for part in stack_parts:
# Synthetic frames contain only their name.
if part in {"<GC>", "<native>", "<unreadable frame>"}:
continue
# Each part should be file:function:line
self.assertIn(":", part)

Expand Down
Loading