Skip to content

Commit

Permalink
do not call get_subprocess_pid_recursive on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
TTianshun committed Dec 14, 2023
1 parent 62ac0a8 commit 37541f5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/viztracer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ def wait_children_finish(self) -> None:
while True:
remain_viztmp = list(f for f in os.listdir(self.multiprocess_output_dir) if f.endswith(".viztmp"))
if len(remain_viztmp) > 0:
if sys.platform == "darwin":
time.sleep(0.5)
continue
remain_children_pid = list(int(f[7:-12]) for f in remain_viztmp)
alive_children = get_subprocess_pid_recursive(os.getpid())
for pid in remain_children_pid:
Expand Down
21 changes: 11 additions & 10 deletions src/viztracer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,9 @@ def get_subprocess_pid_linux(pid: int) -> set:
except FileNotFoundError:
pass # Process has already terminated
return set(children)

def get_subprocess_pid(pid: int) -> set:
if sys.platform in ("linux", "linux2"):
return get_subprocess_pid_linux(pid)
elif sys.platform == "win32":
cmdline = f"wmic process where (ParentProcessId={pid}) get ProcessId"
elif sys.platform == "darwin":
cmdline = f"ps -o pid,ppid -ax | awk \'{{ if ( $2 == {pid} ) {{ print $1 }} }}'"
else:
raise NotImplementedError(f"Unsupported platform: {sys.platform}")

def get_subprocess_pid_windows(pid: int) -> set:
cmdline = f"wmic process where (ParentProcessId={pid}) get ProcessId"
result = subprocess.run(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=1)
lines = result.stdout.decode("utf-8").splitlines()
sub_pids = set()
Expand All @@ -135,6 +128,14 @@ def get_subprocess_pid(pid: int) -> set:
pass
return sub_pids

def get_subprocess_pid(pid: int) -> set:
if sys.platform in ("linux", "linux2"):
return get_subprocess_pid_linux(pid)
elif sys.platform == "win32":
return get_subprocess_pid_windows(pid)
else:
raise NotImplementedError(f"Unsupported platform: {sys.platform}")

quried_pids = set()
subprocess_pids = set()
stack = [pid]
Expand Down
1 change: 1 addition & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def test_child_process_exits_normally(self):
expected_output_file="result.json", expected_stdout=r"Wait",
script=issue_364_code_1)

@unittest.skipIf(sys.platform == "darwin", "get_subprocess_pid_recursive does not work on mac")
def test_child_process_exits_abnormally(self):
self.template(["viztracer", "-o", "result.json", "--dump_raw", "cmdline_test.py"],
expected_output_file="result.json", expected_stdout=r"Wait",
Expand Down
1 change: 1 addition & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_pid_exists(self):
with self.assertRaises(ValueError):
pid_exists(0)

@unittest.skipIf(sys.platform == "darwin", "get_subprocess_pid_recursive does not work on mac")
@package_matrix(["~psutil", "psutil"])
def test_get_subprocess_pid_recursive(self):
q = Queue()
Expand Down

0 comments on commit 37541f5

Please sign in to comment.