Coverage data not saved on SIGTERM in 'concurrency = thread' mode · Issue #1599 · nedbat/coveragepy (original) (raw)

Describe the bug

If you have a long-running/daemon process that you're collecting coverage for, e.g. with coverage run long_running.py, then even with the sigterm = true setting the coverage data is not written if the process is killed with SIGTERM.

If the process is killed with SIGINT instead (either with Ctrl+C or kill -2 <pid>) then data is successfully saved, presumably because this manifests as a python KeyboardInterrupt exception that allows finally blocks to be executed, whereas when SIGTERM is captured by coverage the original SIGTERM handler is invoked without saving data (finally blocks do not get run).

To Reproduce

  1. What version of Python are you using?
    • 3.8, 3.11 (not specific to any version)
  2. What version of coverage.py shows the problem? The output of coverage debug sys is helpful.
    • latest (master branch)
  3. What versions of what packages do you have installed? The output of pip freeze is helpful.
    • n/a
  4. What code shows the problem? Give us a specific commit of a specific repo that we can check out. If you've already worked around the problem, please provide a commit before that fix.
    • I have a testcase reproducing the problem, will link via PR, also shown below
  5. What commands did you run?
$cat handler.py
import os, signal

print("START", flush=True)
print("SIGTERM", flush=True)
os.kill(os.getpid(), signal.SIGTERM)
print("NOT HERE", flush=True)
$cat .coveragerc
[run]
sigterm = true
$coverage run handler.py
START
SIGTERM
Terminated
$ls -Al
-rw-r--r-- 1 legaul legaul   77 Apr  3 00:23 .coveragerc
-rw-r--r-- 1 legaul legaul  142 Apr  3 00:49 handler.py

Expected behavior
Expected coverage data file to be created.

Additional context
n/a