Skip to content

cProfile/profile do not output results if profiling terminates because of SIGTERM #104676

Open
@SnoopJ

Description

@SnoopJ

Bug report

cProfile and profile do not produce any results if the process being profiled is terminated with SIGTERM, although the documentation for cProfile and profile and the behavior of the profiler in the case of an exception (KeyboardInterrupt in particular) do make it seem like the profiler will always write output "at the end" of profiling, and the actual behavior on this edge case is somewhat surprising as a result.

I discovered this behavior while trying to profile a worker subprocess in an application that sends SIGTERM to that subprocess during shutdown. I was able to write my own profiling helper that registers a signal handler, but thought it made sense to open an issue and ask if this is a case of slightly murky documentation, or something that can be improved in profile. Considering the complexity of signal handling, I'm guessing this is more likely to be considered a missing stair in the documentation, but I'm happy to send a PR for either case if others agree that this behavior isn't entirely obvious.

The closest issue I can find in the CPython issue history is #50228 which doesn't involve signals.

Reproduction

$ cat repro.py
import time

x = 0

while True:
    x += 1
    print(x)
    time.sleep(0.1)
$ python3.9 -m cProfile --outfile out.prof repro.py & PID=$!; sleep 0.5; kill -s SIGTERM ${PID}
[2] 1113607
1
2
3
4
[2]-  Terminated              python3 -m cProfile --outfile out.prof repro.py
$ ls out.prof
ls: cannot access 'out.prof': No such file or directory

Environment

  • CPython versions tested on: 3.9, 3.8
  • Operating system and architecture: Ubuntu 20.04

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions