cpython icon indicating copy to clipboard operation
cpython copied to clipboard

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

Open kemingy opened this issue 2 weeks ago • 4 comments

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.

  • Issue: gh-142654

kemingy avatar Dec 13 '25 02:12 kemingy

@kemingy can you approach this similarly as https://github.com/python/cpython/pull/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

pablogsal avatar Dec 14 '25 04:12 pablogsal

@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.

kemingy avatar Dec 15 '25 15:12 kemingy

We also need a test for this

pablogsal avatar Dec 15 '25 18:12 pablogsal

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.

kemingy avatar Dec 16 '25 15:12 kemingy

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

pablogsal avatar Dec 17 '25 14:12 pablogsal