diff --git a/zerver/management/commands/runtornado.py b/zerver/management/commands/runtornado.py index 134b140a01..514b343261 100644 --- a/zerver/management/commands/runtornado.py +++ b/zerver/management/commands/runtornado.py @@ -104,7 +104,7 @@ class Command(BaseCommand): from zerver.tornado.ioloop_logging import logging_data logging_data["port"] = str(port) - setup_event_queue(port) + setup_event_queue(http_server, port) add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py index 20d1a81b92..93697aac22 100644 --- a/zerver/tornado/event_queue.py +++ b/zerver/tornado/event_queue.py @@ -22,6 +22,7 @@ from typing import ( List, Mapping, MutableMapping, + NoReturn, Optional, Sequence, Set, @@ -603,12 +604,24 @@ def send_restart_events(immediate: bool = False) -> None: client.add_event(event) -def setup_event_queue(port: int) -> None: +def handle_sigterm(server: tornado.httpserver.HTTPServer) -> NoReturn: + logging.warning("Got SIGTERM, shutting down...") + server.stop() + tornado.ioloop.IOLoop.instance().stop() + sys.exit(1) + + +def setup_event_queue(server: tornado.httpserver.HTTPServer, port: int) -> None: + ioloop = tornado.ioloop.IOLoop.instance() + if not settings.TEST_SUITE: load_event_queues(port) atexit.register(dump_event_queues, port) # Make sure we dump event queues even if we exit via signal - signal.signal(signal.SIGTERM, lambda signum, stack: sys.exit(1)) + signal.signal( + signal.SIGTERM, + lambda signum, frame: ioloop.add_callback_from_signal(handle_sigterm, server), + ) add_reload_hook(lambda: dump_event_queues(port)) try: @@ -617,7 +630,6 @@ def setup_event_queue(port: int) -> None: pass # Set up event queue garbage collection - ioloop = tornado.ioloop.IOLoop.instance() pc = tornado.ioloop.PeriodicCallback( lambda: gc_event_queues(port), EVENT_QUEUE_GC_FREQ_MSECS, ioloop )