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
23 changes: 19 additions & 4 deletions Lib/test/test_dtrace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dis
import os.path
import re
import signal
import subprocess
import sys
import sysconfig
Expand Down Expand Up @@ -50,6 +51,20 @@ def normalize_trace_output(output):
)


USE_PROCESS_GROUP = (hasattr(os, "setsid") and hasattr(os, "killpg"))

def create_process_group(*args, **kwargs):
if USE_PROCESS_GROUP:
kwargs['start_new_session'] = True
return subprocess.Popen(*args, **kwargs)

def kill_process_group(proc):
if USE_PROCESS_GROUP:
os.killpg(proc.pid, signal.SIGKILL)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

   try:
       os.killpg(proc.pid, signal.SIGKILL)
   except ProcessLookupError:
       pass

else:
proc.kill()


class TraceBackend:
EXTENSION = None
COMMAND = None
Expand Down Expand Up @@ -205,15 +220,15 @@ def run_case(self, name, optimize_python=None):
program = self.PROGRAMS[name].format(python=sys.executable)

try:
proc = subprocess.Popen(
proc = create_process_group(
["bpftrace", "-e", program, "-c", " ".join(subcommand)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
stdout, stderr = proc.communicate(timeout=60)
except subprocess.TimeoutExpired:
proc.kill()
kill_process_group(proc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

proc.communicate() # Clean up

raise AssertionError("bpftrace timed out")
except (FileNotFoundError, PermissionError) as e:
raise unittest.SkipTest(f"bpftrace not available: {e}")
Expand Down Expand Up @@ -243,15 +258,15 @@ def assert_usable(self):
# Check if bpftrace is available and can attach to USDT probes
program = f'usdt:{sys.executable}:python:function__entry {{ printf("probe: success\\n"); exit(); }}'
try:
proc = subprocess.Popen(
proc = create_process_group(
["bpftrace", "-e", program, "-c", f"{sys.executable} -c pass"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
stdout, stderr = proc.communicate(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
kill_process_group(proc)
proc.communicate() # Clean up
raise unittest.SkipTest("bpftrace timed out during usability check")
except OSError as e:
Expand Down
Loading