......@@ -726,7 +726,10 @@ def run_diffoscope(parsed_args):
def sigterm_handler(signo, stack_frame):
logger.warning("Received TERM signal; cleaning up temp files...")
clean_all_temp_files()
ProfileManager().finish()
logger.warning("Exiting...")
os._exit(2)
......@@ -781,6 +784,9 @@ def main(args=None):
pdb.post_mortem()
sys.exit(2)
finally:
# Note that this is block is not called if the sigterm_handler method
# is a) called, and b) executes successfully.
# Helps our tests run more predictably - some of them call main()
# which sets Config() values.
Config().reset()
......
......@@ -60,16 +60,25 @@ class ProfileManager:
self.data[namespace][key]["time"] += time.time() - start
self.data[namespace][key]["count"] += 1
def finish(self, parsed_args):
def finish(self, parsed_args=None):
from .presenters.utils import make_printer
# We are being called in the TERM handler so we don't have access to
# parsed_args. Print the profiling output to stderr if we have been
# collecting it.
if parsed_args is None:
if _ENABLED:
self.output(lambda x: print(x, file=sys.stderr))
return
# Include profiling in --debug output if --profile is not set.
if parsed_args.profile_output is None:
with setup_logging(parsed_args.debug, None) as logger:
self.output(lambda x: logger.debug(x.strip("\n")))
else:
with make_printer(parsed_args.profile_output) as fn:
self.output(fn)
return
with make_printer(parsed_args.profile_output) as fn:
self.output(fn)
def output(self, print_fn):
title = "# Profiling output for: {}".format(" ".join(sys.argv))
......