Skip to content

Conversation

@kemingy
Copy link

@kemingy kemingy commented Dec 13, 2025

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>
Copy link

Copilot AI left a comment

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.

Signed-off-by: Keming <kemingy94@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant