From 00a5fc7f0bf8255bdaaedb60e1132427876f62a3 Mon Sep 17 00:00:00 2001 From: David Zuelke Date: Wed, 15 Jan 2020 16:48:16 +0100 Subject: [PATCH] call out subprocess termination from signals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when hitting Ctrl+C in a 'docker run bob …', the main script itself actually can't be terminated by Ctrl+C, because it's PID 1 in that case, rather than just shutting down silently (by terminating itself using a SIGINT), we will see the subprocess exit (because Ctrl+C is sent to the whole process group) When this happens, the return code of the process will be negative, to indicate that it didn't exit with that code, but instead got terminated by a signal of that (absolute) number --- bob/models.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bob/models.py b/bob/models.py index c7a00dc..e3d793b 100644 --- a/bob/models.py +++ b/bob/models.py @@ -3,6 +3,7 @@ import os import re import shutil +import signal import sys from tempfile import mkstemp, mkdtemp from subprocess import Popen @@ -138,9 +139,18 @@ class Formula(object): p.wait() - if p.returncode != 0: + if p.returncode > 0: print_stderr('Formula exited with return code {}.'.format(p.returncode), title='ERROR') sys.exit(1) + elif p.returncode < 0: # script was terminated by signal number abs(returncode) + signum = abs(p.returncode) + try: + # Python 3.5+ + signame = signal.Signals(signum).name + except AttributeError: + signame = signum + print_stderr('Formula terminated by signal {}.'.format(signame), title='ERROR') + sys.exit(128+signum) # best we can do, given how we weren't terminated ourselves with the same signal (maybe we're PID 1, maybe another reason) print_stderr('\nBuild complete: {}'.format(self.build_path))