From c8f4e9e34ca20ff3b0688f843913bf930e1fc9d7 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 13 Sep 2016 21:28:16 +0200 Subject: [PATCH] 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. --- borg/archiver.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/borg/archiver.py b/borg/archiver.py index 785a7b8d1..bb8e33f77 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -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: