From da32c44bce15a83b1d504db3b6e6fb2ed8f4d584 Mon Sep 17 00:00:00 2001 From: Guillermo Arribas Date: Wed, 1 Nov 2017 12:15:00 -0300 Subject: [PATCH 1/2] Terminate containers on SIGHUP (fixes #4909) Signed-off-by: Guillermo Arribas --- compose/cli/main.py | 5 +++-- compose/cli/signals.py | 14 ++++++++++++++ tests/acceptance/cli_test.py | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index c3e30919d..e89a0fe13 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -1178,6 +1178,7 @@ def run_one_off_container(container_options, project, service, options): project.client.remove_container(container.id, force=True, v=True) signals.set_signal_handler_to_shutdown() + signals.set_signal_handler_to_hang_up() try: try: if IS_WINDOWS_PLATFORM: @@ -1195,10 +1196,10 @@ def run_one_off_container(container_options, project, service, options): service.start_container(container) pty.start(sockets) exit_code = container.wait() - except signals.ShutdownException: + except (signals.ShutdownException, signals.HangUpException): project.client.stop(container.id) exit_code = 1 - except signals.ShutdownException: + except (signals.ShutdownException, signals.HangUpException): project.client.kill(container.id) remove_container(force=True) sys.exit(2) diff --git a/compose/cli/signals.py b/compose/cli/signals.py index 9b360c44e..44def2ece 100644 --- a/compose/cli/signals.py +++ b/compose/cli/signals.py @@ -10,6 +10,10 @@ class ShutdownException(Exception): pass +class HangUpException(Exception): + pass + + def shutdown(signal, frame): raise ShutdownException() @@ -23,6 +27,16 @@ def set_signal_handler_to_shutdown(): set_signal_handler(shutdown) +def hang_up(signal, frame): + raise HangUpException() + + +def set_signal_handler_to_hang_up(): + # on Windows a ValueError will be raised if trying to set signal handler for SIGHUP + if not IS_WINDOWS_PLATFORM: + signal.signal(signal.SIGHUP, hang_up) + + def ignore_sigpipe(): # Restore default behavior for SIGPIPE instead of raising # an exception when encountered. diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 6c18a175c..65b96733e 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -1795,6 +1795,19 @@ class CLITestCase(DockerClientTestCase): 'simplecomposefile_simple_run_1', 'exited')) + def test_run_handles_sighup(self): + proc = start_process(self.base_dir, ['run', '-T', 'simple', 'top']) + wait_on_condition(ContainerStateCondition( + self.project.client, + 'simplecomposefile_simple_run_1', + 'running')) + + os.kill(proc.pid, signal.SIGHUP) + wait_on_condition(ContainerStateCondition( + self.project.client, + 'simplecomposefile_simple_run_1', + 'exited')) + @mock.patch.dict(os.environ) def test_run_unicode_env_values_from_system(self): value = 'ą, ć, ę, ł, ń, ó, ś, ź, ż' From 75572f38609b57d637d79c67660a9c0899527f34 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 7 Feb 2018 14:03:31 -0800 Subject: [PATCH 2/2] Immediately kill / force-rm one-off container when receiving SIGHUP Signed-off-by: Joffrey F --- compose/cli/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index 9359ec703..4319d0ada 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -1274,7 +1274,7 @@ def run_one_off_container(container_options, project, service, options, project_ service.start_container(container) pty.start(sockets) exit_code = container.wait() - except (signals.ShutdownException, signals.HangUpException): + except (signals.ShutdownException): project.client.stop(container.id) exit_code = 1 except (signals.ShutdownException, signals.HangUpException):