From b4d49720b00308de34c62814e042c9456e38331c Mon Sep 17 00:00:00 2001 From: Umair Khan Date: Wed, 19 Jul 2017 10:35:48 +0500 Subject: [PATCH] tornado: Close queue connection on reload. Tornado reloads the app whenever there is a change in code. Due to this, new connection is created to the client which also results in a new channel. To avoid creating two channels for the queue in the RabbitMQ broker we should close the old channel. Otherwise messages sent to the queue will be distributed among these two channels in a round robin scheme and we will end up losing one message since one of the channels doesn't have an active consumer. This commit closes the connection to the queue whenever Tornado reloads the application using add_reload_hook(). Fixes #5824. --- zerver/tornado/application.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zerver/tornado/application.py b/zerver/tornado/application.py index bf08ed39f4..b38281e7b2 100644 --- a/zerver/tornado/application.py +++ b/zerver/tornado/application.py @@ -9,6 +9,7 @@ from zerver.tornado.handlers import AsyncDjangoHandler from zerver.tornado.socket import get_sockjs_router from zerver.lib.queue import get_queue_client +import tornado.autoreload import tornado.web def setup_tornado_rabbitmq(): @@ -17,6 +18,7 @@ def setup_tornado_rabbitmq(): if settings.USING_RABBITMQ: queue_client = get_queue_client() atexit.register(lambda: queue_client.close()) + tornado.autoreload.add_reload_hook(lambda: queue_client.close()) # type: ignore # TODO: Fix missing tornado.autoreload stub def create_tornado_application(): # type: () -> tornado.web.Application