From 4d582bb6bc100a36bde65d0d65473e23b066fd17 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 22 Oct 2023 02:29:55 +0200 Subject: [PATCH] Ensure that the application always terminates on Ctrl+C. --- platypush/runner/_app.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/platypush/runner/_app.py b/platypush/runner/_app.py index a0a147764..56c0886c9 100644 --- a/platypush/runner/_app.py +++ b/platypush/runner/_app.py @@ -3,6 +3,7 @@ import os import signal import subprocess import sys +import time from platypush.process import ControllableProcess @@ -28,17 +29,30 @@ class ApplicationProcess(ControllableProcess): def main(self): self.logger.info('Starting application...') + app = None - with subprocess.Popen( - [sys.executable, '-m', 'platypush.app', *self.args], - stdin=sys.stdin, - stdout=sys.stdout, - stderr=sys.stderr, - ) as app: - try: + try: + with subprocess.Popen( + [sys.executable, '-m', 'platypush.app', *self.args], + stdin=sys.stdin, + stdout=sys.stdout, + stderr=sys.stderr, + ) as app: app.wait() - except KeyboardInterrupt: - pass + except KeyboardInterrupt: + pass + + if app and app.poll() is None: + app.terminate() + + wait_start = time.time() + while app and app.poll() is None: + if time.time() - wait_start > 5: + self.logger.warning('Application did not terminate, killing it') + app.kill() + break + + time.sleep(0.1) def on_stop(self): try: