Repository navigation
Expand file tree
/
Copy pathtimeout_with_stack.py
More file actions
342 lines (280 loc) · 9.76 KB
/
Copy pathtimeout_with_stack.py
File metadata and controls
342 lines (280 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
"""
Module for running commands with timeout and capturing stack traces.
This module provides functionality to run commands with a timeout, capture stack traces
of processes that exceed the timeout, and properly terminate process trees.
See Also
--------
subprocess.Popen : For running subprocesses without timeout.
psutil.Process : For process management and information.
Examples
--------
>>> from timeout_with_stack import run_with_timeout
>>> exit_code = run_with_timeout(["sleep", "10"], timeout=5)
>>> print(f"Process exited with code: {exit_code}")
"""
from __future__ import annotations
import argparse
import os
import shutil
import signal
import subprocess
import sys
import time
from contextlib import suppress
from enum import IntEnum
from typing import TYPE_CHECKING
import psutil
if TYPE_CHECKING:
from collections.abc import Sequence
class StackType(IntEnum):
"""Enum representing the type of stack trace to capture."""
C = 0
Python = 1
def get_child_pids(pid: int) -> list[int]:
"""
Get all child PIDs of a given process.
This function retrieves all child process IDs (PIDs) of a given process,
including recursively nested child processes.
Parameters
----------
pid
The process ID of the parent process.
Returns
-------
A list of child process IDs. Returns an empty list if the parent process
does not exist.
See Also
--------
psutil.Process.children : For getting child processes.
Examples
--------
>>> from timeout_with_stack import get_child_pids
>>> child_pids = get_child_pids(1234)
>>> print(f"Child PIDs: {child_pids}")
"""
try:
parent = psutil.Process(pid)
children = parent.children(recursive=True)
return [p.pid for p in children]
except psutil.NoSuchProcess:
return []
def capture_stack_trace(pid: int, stack_type=StackType.C) -> None:
"""
Capture stack trace for a given process.
This function captures the stack trace of a process using GDB. It prints the
stack trace to stdout, which can be useful for debugging hanging or long-running
processes.
Parameters
----------
pid
The process ID of the process to capture stack trace for.
stack_type
The stack type to extract, either C or Python.
See Also
--------
capture_all_stacks : For capturing stack traces of a process and its children.
Examples
--------
>>> from timeout_with_stack import capture_stack_trace
>>> capture_stack_trace(1234)
"""
if stack_type is StackType.C:
bt_command = "thread apply all bt"
print(f"\nCapturing C stack trace for process {pid}:")
else:
bt_command = "thread apply all py-bt"
print(f"\nCapturing Python stack trace for process {pid}:")
gdb = shutil.which("gdb")
if gdb is None:
print(f"Skipping stack trace for process {pid}: gdb not found")
return
gdb_args = [gdb, "--quiet"]
try:
executable = psutil.Process(pid).exe()
except (psutil.AccessDenied, psutil.NoSuchProcess):
pass
else:
auto_load_script = f"{executable}-gdb.py"
if os.path.isfile(auto_load_script):
# GDB attaches while processing --pid, before regular -ex commands
# run. Trust only this executable's helper early enough for it to
# register Python commands such as py-bt during the attach.
gdb_args.extend(
[
"-iex",
f"add-auto-load-safe-path {auto_load_script}",
]
)
gdb_args.extend(
[
"--pid",
str(pid),
"-ex",
"set pagination off",
"-ex",
"set confirm off",
"-ex",
bt_command,
"-ex",
"quit",
]
)
proc = subprocess.run(
gdb_args,
capture_output=True,
text=True,
check=False,
)
print(proc.stdout)
if proc.stderr:
print(proc.stderr, file=sys.stderr)
def capture_all_stacks(pid: int, *, enable_python: bool = False) -> None:
"""
Capture stack traces for parent and all child processes.
This function captures stack traces for both the parent process and all its
child processes. It first captures the parent's stack trace, then recursively
captures stack traces for all child processes.
Parameters
----------
pid
The process ID of the parent process.
enable_python
Whether to capture Python stack traces.
See Also
--------
capture_stack_trace : For capturing stack trace of a single process.
get_child_pids : For getting child process IDs.
Examples
--------
>>> from timeout_with_stack import capture_all_stacks
>>> capture_all_stacks(1234, enable_python=True)
"""
# Capture parent process stack
if enable_python:
capture_stack_trace(pid, stack_type=StackType.Python)
capture_stack_trace(pid, stack_type=StackType.C)
# Get and capture all child processes
child_pids = get_child_pids(pid)
for child_pid in child_pids:
if enable_python:
capture_stack_trace(child_pid, stack_type=StackType.Python)
capture_stack_trace(child_pid, stack_type=StackType.C)
def terminate_process_tree(pid: int) -> None:
"""
Terminate a process and all its children.
This function terminates a process and all its child processes. It first
attempts to gracefully terminate all processes, then forcefully kills any
remaining processes after a timeout.
Parameters
----------
pid
The process ID of the parent process to terminate.
See Also
--------
psutil.Process.terminate : For gracefully terminating a process.
psutil.Process.kill : For forcefully killing a process.
Examples
--------
>>> from timeout_with_stack import terminate_process_tree
>>> terminate_process_tree(1234)
"""
try:
parent = psutil.Process(pid)
children = parent.children(recursive=True)
# Terminate children first
for child in children:
with suppress(psutil.NoSuchProcess):
child.terminate()
# Create a copy of children list
terminated_children = list(children)
# Wait for all children to terminate
for child in terminated_children:
with suppress(psutil.TimeoutExpired):
child.wait(timeout=3)
# Kill any remaining children
for child in terminated_children:
with suppress(psutil.NoSuchProcess):
child.kill()
# Terminate parent
parent.terminate()
try:
parent.wait(timeout=3)
except psutil.TimeoutExpired:
parent.kill()
except psutil.NoSuchProcess:
pass
def run_with_timeout(
cmd: Sequence[str], timeout: float, *, enable_python: bool = False
) -> int:
"""
Run a command with a timeout and capture stack traces if it exceeds the timeout.
This function runs a command with a specified timeout. If the command exceeds
the timeout, it captures stack traces of the process and its children before
terminating them. It handles keyboard interrupts gracefully.
Parameters
----------
cmd
The command and its arguments to run.
timeout
Maximum time in seconds to allow the command to run.
enable_python
Whether to capture Python stack traces.
Returns
-------
Return code of the command, or 124 if timeout occurred, or signal.SIGINT
if interrupted by keyboard.
See Also
--------
subprocess.Popen : For running subprocesses without timeout.
capture_all_stacks : For capturing stack traces of processes.
Examples
--------
>>> from timeout_with_stack import run_with_timeout
>>> exit_code = run_with_timeout(["sleep", "10"], timeout=5, enable_python=True)
>>> print(f"Process exited with code: {exit_code}")
"""
# Start the process with a new process group
# Note: preexec_fn is used here as we need to create a new process group
# for proper termination of child processes
process = subprocess.Popen(
cmd,
preexec_fn=os.setsid, # noqa: PLW1509
)
start_time = time.time()
try:
while time.time() - start_time < timeout:
if process.poll() is not None:
return process.returncode
time.sleep(0.1)
print(f"\nProcess timed out after {timeout} seconds")
print("Capturing stack traces for all processes...")
# Capture stacks for parent and all children
capture_all_stacks(process.pid, enable_python=enable_python)
# Terminate the entire process tree
print("\nTerminating process tree...")
terminate_process_tree(process.pid)
except KeyboardInterrupt:
print("\nReceived keyboard interrupt")
terminate_process_tree(process.pid)
return signal.SIGINT
else:
return 124
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run a command with timeout and capture stack traces"
)
parser.add_argument("timeout", type=float, help="Timeout in seconds")
parser.add_argument(
"--enable-python", action="store_true", help="Enable Python stack trace capture"
)
parser.add_argument("command", nargs=argparse.REMAINDER, help="Command to run")
args = parser.parse_args()
if not args.command:
parser.error("No command specified")
exit_code = run_with_timeout(
args.command, args.timeout, enable_python=args.enable_python
)
sys.exit(exit_code)