Correctly exit with proper unlock on SIGHUP, fixes #1593

If the connections hangs up, the borg server needs to clean
up, especially unlock the repository, so a later try will
work again.

This is especially problematic with systemd systems that have
KillUserProcesses enabled (which is the default): Logind sends
a SIGHUP message to the session scope when the session ends.
This commit is contained in:
Julian Andres Klode 2016-09-13 21:28:16 +02:00
parent 3714be0d79
commit c8f4e9e34c

View file

@ -1666,6 +1666,14 @@ def sig_term_handler(signum, stack):
raise SIGTERMReceived
class SIGHUPReceived(BaseException):
pass
def sig_hup_handler(signum, stack):
raise SIGHUPReceived
def setup_signal_handlers(): # pragma: no cover
sigs = []
if hasattr(signal, 'SIGUSR1'):
@ -1674,7 +1682,12 @@ def setup_signal_handlers(): # pragma: no cover
sigs.append(signal.SIGINFO) # kill -INFO pid (or ctrl-t)
for sig in sigs:
signal.signal(sig, sig_info_handler)
# If we received SIGTERM or SIGHUP, catch them and raise a proper exception
# that can be handled for an orderly exit. SIGHUP is important especially
# for systemd systems, where logind sends it when a session exits, in
# addition to any traditional use.
signal.signal(signal.SIGTERM, sig_term_handler)
signal.signal(signal.SIGHUP, sig_hup_handler)
def main(): # pragma: no cover
@ -1713,6 +1726,9 @@ def main(): # pragma: no cover
except SIGTERMReceived:
msg = 'Received SIGTERM.'
exit_code = EXIT_ERROR
except SIGHUPReceived:
msg = 'Received SIGHUP.'
exit_code = EXIT_ERROR
if msg:
logger.error(msg)
if args.show_rc: