call out subprocess termination from signals

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
This commit is contained in:
David Zuelke
2020-01-15 16:48:16 +01:00
parent 73822ec2f9
commit 00a5fc7f0b
+11 -1
View File
@@ -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))