Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-142654: show the clear error message when sampling on an unknown PID#142655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7024bd1b1a5082cfea35bab9c0c6a1c66e50a1f140dcad05a4d554015b5ad2f7b4bd69File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """Custom exceptions for the sampling profiler.""" | ||
| class SamplingProfilerError(Exception): | ||
| """Base exception for sampling profiler errors.""" | ||
| class SamplingUnknownProcessError(SamplingProfilerError): | ||
| def __init__(self, pid): | ||
| self.pid = pid | ||
| super().__init__(f"Process with PID '{pid}' does not exist.") | ||
| class SamplingScriptNotFoundError(SamplingProfilerError): | ||
| def __init__(self, script_path): | ||
| self.script_path = script_path | ||
| super().__init__(f"Script '{script_path}' not found.") | ||
| class SamplingModuleNotFoundError(SamplingProfilerError): | ||
| def __init__(self, module_name): | ||
| self.module_name = module_name | ||
| super().__init__(f"Module '{module_name}' not found.") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -34,23 +34,29 @@ def __init__(self, pid, sample_interval_usec, all_threads, *, mode=PROFILING_MOD | ||
| self.all_threads = all_threads | ||
| self.mode = mode # Store mode for later use | ||
| self.collect_stats = collect_stats | ||
| try: | ||
| self.unwinder = self._new_unwinder(native, gc, opcodes, skip_non_matching_threads) | ||
| except RuntimeError as err: | ||
| raise SystemExit(err) from err | ||
| # Track sample intervals and total sample count | ||
| self.sample_intervals = deque(maxlen=100) | ||
| self.total_samples = 0 | ||
| self.realtime_stats = False | ||
| def _new_unwinder(self, native, gc, opcodes, skip_non_matching_threads): | ||
| if _FREE_THREADED_BUILD: | ||
| self.unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, all_threads=self.all_threads, mode=mode, native=native, gc=gc, | ||
| unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, all_threads=self.all_threads, mode=self.mode, native=native, gc=gc, | ||
| opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads, | ||
| cache_frames=True, stats=collect_stats | ||
| cache_frames=True, stats=self.collect_stats | ||
| ) | ||
| else: | ||
| only_active_threads = bool(self.all_threads) | ||
| self.unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, only_active_thread=only_active_threads, mode=mode, native=native, gc=gc, | ||
| unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, only_active_thread=bool(self.all_threads), mode=self.mode, native=native, gc=gc, | ||
| opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads, | ||
| cache_frames=True, stats=collect_stats | ||
| cache_frames=True, stats=self.collect_stats | ||
| ) | ||
| # Track sample intervals and total sample count | ||
| self.sample_intervals = deque(maxlen=100) | ||
| self.total_samples = 0 | ||
| self.realtime_stats = False | ||
| return unwinder | ||
| def sample(self, collector, duration_sec=10, *, async_aware=False): | ||
| sample_interval_sec = self.sample_interval_usec / 1_000_000 | ||
| @@ -86,7 +92,7 @@ def sample(self, collector, duration_sec=10, *, async_aware=False): | ||
| collector.collect_failed_sample() | ||
| errors += 1 | ||
| except Exception as e: | ||
| if not self._is_process_running(): | ||
| if not _is_process_running(self.pid): | ||
| break | ||
| raise e from None | ||
| @@ -148,22 +154,6 @@ def sample(self, collector, duration_sec=10, *, async_aware=False): | ||
| f"({(expected_samples - num_samples) / expected_samples * 100:.2f}%)" | ||
| ) | ||
| def _is_process_running(self): | ||
| if sys.platform == "linux" or sys.platform == "darwin": | ||
| try: | ||
| os.kill(self.pid, 0) | ||
| return True | ||
| except ProcessLookupError: | ||
| return False | ||
| elif sys.platform == "win32": | ||
| try: | ||
| _remote_debugging.RemoteUnwinder(self.pid) | ||
| except Exception: | ||
| return False | ||
| return True | ||
| else: | ||
| raise ValueError(f"Unsupported platform: {sys.platform}") | ||
| def _print_realtime_stats(self): | ||
| """Print real-time sampling statistics.""" | ||
| if len(self.sample_intervals) < 2: | ||
| @@ -279,6 +269,28 @@ def _print_unwinder_stats(self): | ||
| print(f" {ANSIColors.YELLOW}Stale cache invalidations: {stale_invalidations}{ANSIColors.RESET}") | ||
| def _is_process_running(pid): | ||
pablogsal marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if pid <= 0: | ||
| return False | ||
| if os.name == "posix": | ||
| try: | ||
| os.kill(pid, 0) | ||
| return True | ||
| except ProcessLookupError: | ||
| return False | ||
| except PermissionError: | ||
| # EPERM means process exists but we can't signal it | ||
| return True | ||
| elif sys.platform == "win32": | ||
| try: | ||
| _remote_debugging.RemoteUnwinder(pid) | ||
| except Exception: | ||
| return False | ||
| return True | ||
| else: | ||
| raise ValueError(f"Unsupported platform: {sys.platform}") | ||
| def sample( | ||
| pid, | ||
| collector, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Show the clearer error message when using ``profiling.sampling`` on an | ||
kemingy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| unknown PID. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.