Skip to content

gh-142654: show the clear error message when sampling on an unknown PID - #142655

Merged
pablogsal merged 10 commits into
python:mainfrom
kemingy:gh-142654
Dec 17, 2025
Merged

pablogsal merged 10 commits into
python:mainfrom
kemingy:gh-142654

Conversation

@kemingy

@kemingy kemingy commented Dec 13, 2025 •

Copy link
Copy Markdown
Contributor

Before this change:

OSError: Cannot open process memory map file '/proc/-1/maps' for PID -1 section search: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/keming/GitHub/cpython/Lib/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
                     "__main__", mod_spec)
  File "/home/keming/GitHub/cpython/Lib/runpy.py", line 87, in _run_code
    exec(code, run_globals)
    ~~~~^^^^^^^^^^^^^^^^^^^
  File "/home/keming/GitHub/cpython/Lib/profiling/sampling/__main__.py", line 64, in <module>
    main()
    ~~~~^^
  File "/home/keming/GitHub/cpython/Lib/profiling/sampling/cli.py", line 592, in main
    handler(args)
    ~~~~~~~^^^^^^
  File "/home/keming/GitHub/cpython/Lib/profiling/sampling/cli.py", line 620, in _handle_attach
    collector = sample(
        args.pid,
    ...<8 lines>...
        opcodes=args.opcodes,
    )
  File "/home/keming/GitHub/cpython/Lib/profiling/sampling/sample.py", line 327, in sample
    profiler = SampleProfiler(
        pid,
    ...<7 lines>...
        collect_stats=realtime_stats,
    )
  File "/home/keming/GitHub/cpython/Lib/profiling/sampling/sample.py", line 39, in __init__
    self.unwinder = _remote_debugging.RemoteUnwinder(
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self.pid, all_threads=self.all_threads, mode=mode, native=native, gc=gc,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        cache_frames=True, stats=collect_stats
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
RuntimeError: Failed to find the PyRuntime section in process -1 on Linux platform

After this change:

Failed to find the PyRuntime section in process -1 on Linux platform

This aligns the behavior with attaching to a process that doesn't have the PyRuntime section.

…nown PID

Signed-off-by: Keming <kemingy94@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves error handling when attempting to profile a process with an invalid PID by simplifying the error message. Instead of showing a confusing stack trace with multiple exceptions, it now displays only the final, clear error message about failing to find the PyRuntime section.

Key Changes:

  • Wraps RemoteUnwinder initialization in a try-except block that converts exceptions to SystemExit
  • Updates test expectations from OSError/RuntimeError to SystemExit
  • Adds a NEWS entry documenting the improvement

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Lib/profiling/sampling/sample.py Adds exception handling in SampleProfiler.__init__ to convert errors to SystemExit for cleaner user-facing messages
Lib/test/test_profiling/test_sampling_profiler/test_integration.py Updates test to expect SystemExit instead of OSError/RuntimeError for invalid PIDs
Misc/NEWS.d/next/Library/2025-12-13-10-34-59.gh-issue-142654.fmm974.rst Documents the error message improvement

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Lib/profiling/sampling/sample.py Resolved
Comment thread Lib/profiling/sampling/sample.py Resolved
Signed-off-by: Keming <kemingy94@gmail.com>
@pablogsal

Copy link
Copy Markdown
Member

@kemingy can you approach this similarly as #142592 ? The idea is to not show an ugly stack trace or difficult to understand messages like "Failed to find the PyRuntime section in process -1 on Linux platform" but just checking if the process exists or not or if is valid or not

Signed-off-by: Keming <kemingy94@gmail.com>
Signed-off-by: Keming <kemingy94@gmail.com>
Signed-off-by: Keming <kemingy94@gmail.com>
@kemingy

kemingy commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

@kemingy can you approach this similarly as #142592 ? The idea is to not show an ugly stack trace or difficult to understand messages like "Failed to find the PyRuntime section in process -1 on Linux platform" but just checking if the process exists or not or if is valid or not

Hi @pablogsal I have added the process check. Please take a look.

Comment thread Lib/profiling/sampling/sample.py Resolved
Comment thread Lib/profiling/sampling/sample.py Resolved
Comment thread Lib/profiling/sampling/cli.py Outdated
def _handle_attach(args):
"""Handle the 'attach' command."""
if not _is_process_running(args.pid):
raise sys.exit(f"Process with PID {args.pid} is not running.")

@pablogsal pablogsal Dec 15, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer to raise some custom exception here and catch it in __main__ like the other exceptions. Perhaps we should also do the same for the other usages of sys.exit.

Do you mind creating a errors.py file with custom exceptions for the different case, raise them and properly handle the messages in __main__?

Comment thread Lib/profiling/sampling/cli.py Resolved
@pablogsal

Copy link
Copy Markdown
Member

We also need a test for this

Signed-off-by: Keming <kemingy94@gmail.com>
@kemingy

kemingy commented Dec 16, 2025

Copy link
Copy Markdown
Contributor Author

We also need a test for this

Hi @pablogsal Thanks for your advice. I have added the errors.py file and handle these custom exceptions in the __main__. Also added a test for the attach cli. Please take a look.

@pablogsal
pablogsal merged commit d4095f2 into python:main Dec 17, 2025
@pablogsal

Copy link
Copy Markdown
Member

Made some small fixes but great job @kemingy! Thanks for the PR 🚀

@kemingy
kemingy deleted the gh-142654 branch December 18, 2025 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants