Skip to content

Commit 7024bd1

Browse files
committed
gh-142654: show the clear error message when sampling on an unknown PID
Signed-off-by: Keming <kemingy94@gmail.com>
1 parent 519bee4 commit 7024bd1

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

Lib/profiling/sampling/sample.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ def __init__(self, pid, sample_interval_usec, all_threads, *, mode=PROFILING_MOD
3434
self.all_threads = all_threads
3535
self.mode = mode # Store mode for later use
3636
self.collect_stats = collect_stats
37-
if _FREE_THREADED_BUILD:
38-
self.unwinder = _remote_debugging.RemoteUnwinder(
39-
self.pid, all_threads=self.all_threads, mode=mode, native=native, gc=gc,
40-
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
41-
cache_frames=True, stats=collect_stats
42-
)
43-
else:
44-
only_active_threads = bool(self.all_threads)
45-
self.unwinder = _remote_debugging.RemoteUnwinder(
46-
self.pid, only_active_thread=only_active_threads, mode=mode, native=native, gc=gc,
47-
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
48-
cache_frames=True, stats=collect_stats
49-
)
37+
try:
38+
if _FREE_THREADED_BUILD:
39+
self.unwinder = _remote_debugging.RemoteUnwinder(
40+
self.pid, all_threads=self.all_threads, mode=mode, native=native, gc=gc,
41+
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
42+
cache_frames=True, stats=collect_stats
43+
)
44+
else:
45+
only_active_threads = bool(self.all_threads)
46+
self.unwinder = _remote_debugging.RemoteUnwinder(
47+
self.pid, only_active_thread=only_active_threads, mode=mode, native=native, gc=gc,
48+
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
49+
cache_frames=True, stats=collect_stats
50+
)
51+
except Exception as err:
52+
raise SystemExit(err)
5053
# Track sample intervals and total sample count
5154
self.sample_intervals = deque(maxlen=100)
5255
self.total_samples = 0

Lib/test/test_profiling/test_sampling_profiler/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def test_sample_target_module(self):
635635
)
636636
class TestSampleProfilerErrorHandling(unittest.TestCase):
637637
def test_invalid_pid(self):
638-
with self.assertRaises((OSError, RuntimeError)):
638+
with self.assertRaises(SystemExit):
639639
collector = PstatsCollector(sample_interval_usec=100, skip_idle=False)
640640
profiling.sampling.sample.sample(-1, collector, duration_sec=1)
641641

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Show the clearer error message when using ``profiling.sampling`` on an
2+
unknown PID.

0 commit comments

Comments
 (0)