Skip to content

profile sampling does not handle KeyboardInterrupt #141999

@yihong0618

Description

@yihong0618

Bug report

Bug description:

➜  cpython git:(hy/handle_ctrl_c_in_sampling) sudo ./python.exe -m profiling.sampling run -d 99999 -m test
Password:
== CPython 3.15.0a2+ free-threading build (heads/main-dirty:bc9e63dd9d, Nov 27 2025, 09:14:30) [Clang 17.0.0 (clang-1700.0.13.5)]
== macOS-26.1-arm64-arm-64bit-Mach-O little-endian
== Python build: free_threading release
== cwd: /Users/hyi/prs/cpython/build/test_python_worker_57706æ
== CPU count: 8
== encodings: locale=UTF-8 FS=utf-8
== resources: all test resources are disabled, use -u option to unskip tests

Using random seed: 911812834
0:00:00 load avg: 3.47 Run 497 tests sequentially in a single process
0:00:00 load avg: 3.47 [  1/497] test.test_asyncio.test_base_events
0:00:01 load avg: 3.47 [  1/497] test.test_asyncio.test_base_events passed
0:00:01 load avg: 3.47 [  2/497] test.test_asyncio.test_buffered_proto
0:00:01 load avg: 3.47 [  2/497] test.test_asyncio.test_buffered_proto passed
0:00:01 load avg: 3.47 [  3/497] test.test_asyncio.test_context
0:00:01 load avg: 3.47 [  3/497] test.test_asyncio.test_context passed
0:00:01 load avg: 3.47 [  4/497] test.test_asyncio.test_eager_task_factory
^CTraceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/hyi/prs/cpython/Lib/profiling/sampling/__main__.py", line 64, in <module>
    main()
    ~~~~^^
  File "/Users/hyi/prs/cpython/Lib/profiling/sampling/cli.py", line 525, in main
    handler(args)
    ~~~~~~~^^^^^^
  File "/Users/hyi/prs/cpython/Lib/profiling/sampling/cli.py", line 601, in _handle_run
    collector = sample(
        process.pid,
    ...<6 lines>...
        gc=args.gc,
    )
  File "/Users/hyi/prs/cpython/Lib/profiling/sampling/sample.py", line 228, in sample
    profiler.sample(collector, duration_sec)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hyi/prs/cpython/Lib/profiling/sampling/sample.py", line 65, in sample
    current_time = time.perf_counter()
KeyboardInterrupt

seems its better just print the result when KeyboardInterrupt(ctrl+c)

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-profilingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions