From 99c88629ad879e5c50c113f2fcd62f49a711ce3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20Jens=C3=A5s?= Date: Fri, 20 Apr 2018 22:53:03 +0200 Subject: [PATCH] Raise KeyboardInterrupt on SIGTERM - Workaround Catch SIGTERM signal and call the signal handler method. The signal handler then raises KeyboardInterrupt. The KeyboardInterrupt won't be caught by any 'except Exception' clauses. Without this the service does not stop periodic workers, tear down pxe filters etc as it is supposed to on shutdown. NOTE: Calling shutdown() directly from the signal handler causes the below error. This is why the signal handler raises KeyboardInterrupt. AssertionError: Cannot switch to MAINLOOP from MAINLOOP This patch differs from the commit on master: * It also imports 'signal' module. In master the signal module was already imported. Related-Bug: #1765700 Story: 2001890 Task: 14374 Change-Id: If0e24eae767b7806243fa4ae34fedb30ae9af25a (cherry picked from commit 737dbeae118cc136597565c8c1449fca4945143c) --- ironic_inspector/wsgi_service.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ironic_inspector/wsgi_service.py b/ironic_inspector/wsgi_service.py index 7365c5fe7..b990d47de 100644 --- a/ironic_inspector/wsgi_service.py +++ b/ironic_inspector/wsgi_service.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import signal import ssl import sys @@ -36,6 +37,7 @@ class WSGIService(object): def __init__(self): self.app = app.app self._periodics_worker = None + signal.signal(signal.SIGTERM, self._handle_sigterm) def _init_middleware(self): """Initialize WSGI middleware. @@ -171,6 +173,12 @@ class WSGIService(object): finally: self.shutdown() + def _handle_sigterm(self, *args): + # This is a workaround to ensure that shutdown() is done when recieving + # SIGTERM. Raising KeyboardIntrerrupt which won't be caught by any + # 'except Exception' clauses. + raise KeyboardInterrupt + def periodic_update(): # pragma: no cover try: