From 737dbeae118cc136597565c8c1449fca4945143c 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 Related-Bug: #1765700 Story: 2001890 Task: 14374 Change-Id: If0e24eae767b7806243fa4ae34fedb30ae9af25a --- ironic_inspector/wsgi_service.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ironic_inspector/wsgi_service.py b/ironic_inspector/wsgi_service.py index bdac392b3..cdebd7d70 100644 --- a/ironic_inspector/wsgi_service.py +++ b/ironic_inspector/wsgi_service.py @@ -43,6 +43,7 @@ class WSGIService(object): self._periodics_worker = None self._shutting_down = semaphore.Semaphore() signal.signal(signal.SIGHUP, self._handle_sighup) + signal.signal(signal.SIGTERM, self._handle_sigterm) def _init_middleware(self): """Initialize WSGI middleware. @@ -198,6 +199,12 @@ class WSGIService(object): def _handle_sighup(self, *args): eventlet.spawn(self._handle_sighup_bg, *args) + 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_clean_up(): # pragma: no cover try: