Skip to content

Commit

Permalink
fix: capture data on SIGTERM #1307
Browse files Browse the repository at this point in the history
This covers multiprocessing.Process.terminate(), and maybe other cases also.
  • Loading branch information
nedbat committed Jan 23, 2022
1 parent 2e8c191 commit 6121d88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import os.path
import platform
import signal
import sys
import time
import warnings
Expand Down Expand Up @@ -526,6 +527,7 @@ def _init_for_start(self):
self._should_write_debug = True

atexit.register(self._atexit)
self._old_sigterm = signal.signal(signal.SIGTERM, self._on_sigterm)

def _init_data(self, suffix):
"""Create a data file if we don't have one yet."""
Expand Down Expand Up @@ -583,15 +585,20 @@ def stop(self):
self._collector.stop()
self._started = False

def _atexit(self):
def _atexit(self, event="atexit"):
"""Clean up on process shutdown."""
if self._debug.should("process"):
self._debug.write(f"atexit: pid: {os.getpid()}, instance: {self!r}")
self._debug.write(f"{event}: pid: {os.getpid()}, instance: {self!r}")
if self._started:
self.stop()
if self._auto_save:
self.save()

def _on_sigterm(self, signum, frame):
self._atexit("sigterm")
signal.signal(signal.SIGTERM, self._old_sigterm)
signal.raise_signal(signal.SIGTERM)

def erase(self):
"""Erase previously collected coverage data.
Expand Down
2 changes: 1 addition & 1 deletion coverage/multiproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _bootstrap(self, *args, **kwargs):
"""Wrapper around _bootstrap to start coverage."""
try:
from coverage import Coverage # avoid circular import
cov = Coverage(data_suffix=True)
cov = Coverage(data_suffix=True, auto_data=True)
cov._warn_preimported_source = False
cov.start()
debug = cov._debug
Expand Down

0 comments on commit 6121d88

Please sign in to comment.