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 737dbeae11)
This commit is contained in:
Harald Jensås 2018-04-20 22:53:03 +02:00
parent bd16596658
commit 99c88629ad
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
@ -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: