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 in that 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 737dbeae11)
This commit is contained in:
Harald Jensås 2018-04-20 22:53:03 +02:00
parent 02ece7fc9f
commit 1eb1f6cc7c
1 changed files with 8 additions and 0 deletions

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import signal
import ssl
import sys
import traceback as traceback_mod
@ -41,6 +42,7 @@ class WSGIService(object):
self.app = app.app
self._periodics_worker = None
self._shutting_down = semaphore.Semaphore()
signal.signal(signal.SIGTERM, self._handle_sigterm)
def _init_middleware(self):
"""Initialize WSGI middleware.
@ -189,6 +191,12 @@ class WSGIService(object):
else:
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_clean_up(): # pragma: no cover
try: