From cf16ddd4d69eee6ba0bc1b25ca52d275f1c06186 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 9 Dec 2023 13:31:30 -0800 Subject: [PATCH] tornado: Fix autoreload for Tornado 6.4 changes. Signed-off-by: Anders Kaseorg --- tools/run-dev | 15 ++++++++++----- zerver/management/commands/runtornado.py | 14 ++------------ zerver/tornado/application.py | 4 ++-- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/tools/run-dev b/tools/run-dev index 0f4e4112f8..d6231ded6c 100755 --- a/tools/run-dev +++ b/tools/run-dev @@ -92,10 +92,17 @@ if options.test: settings_module = "zproject.test_settings" # Don't auto-reload when running Puppeteer tests runserver_args = ["--noreload"] - tornado_autoreload = [] + runtornado_command = ["./manage.py", "runtornado"] else: settings_module = "zproject.settings" - tornado_autoreload = ["-m", "tornado.autoreload"] + runtornado_command = [ + "-m", + "tornado.autoreload", + "--until-success", + "./manage.py", + "runtornado", + "--autoreload", + ] manage_args = [f"--settings={settings_module}"] os.environ["DJANGO_SETTINGS_MODULE"] = settings_module @@ -148,9 +155,7 @@ def server_processes() -> List[List[str]]: "env", "PYTHONUNBUFFERED=1", "python3", - *tornado_autoreload, - "./manage.py", - "runtornado", + *runtornado_command, *manage_args, f"127.0.0.1:{tornado_port}", ], diff --git a/zerver/management/commands/runtornado.py b/zerver/management/commands/runtornado.py index 707e99bf00..af76c05eb1 100644 --- a/zerver/management/commands/runtornado.py +++ b/zerver/management/commands/runtornado.py @@ -8,12 +8,9 @@ from urllib.parse import SplitResult from asgiref.sync import async_to_sync, sync_to_async from django.conf import settings from django.core.management.base import BaseCommand, CommandError, CommandParser -from tornado import autoreload from tornado.platform.asyncio import AsyncIOMainLoop from typing_extensions import override -import __main__ - settings.RUNNING_INSIDE_TORNADO = True if settings.PRODUCTION: settings.SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") @@ -42,6 +39,7 @@ class Command(BaseCommand): @override def add_arguments(self, parser: CommandParser) -> None: + parser.add_argument("--autoreload", action="store_true", help="Enable Tornado autoreload") parser.add_argument( "addrport", help="[port number or ipaddr:port]", @@ -118,7 +116,7 @@ class Command(BaseCommand): ) # Application is an instance of Django's standard wsgi handler. - application = create_tornado_application() + application = create_tornado_application(autoreload=options["autoreload"]) # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer(application, xheaders=True) @@ -135,14 +133,6 @@ class Command(BaseCommand): if settings.USING_RABBITMQ: setup_tornado_rabbitmq(queue_client) - if hasattr(__main__, "add_reload_hook"): - autoreload.start() - await stop_fut - # Monkey patch tornado.autoreload to prevent it from continuing - # to watch for changes after catching our SystemExit. Otherwise - # the user needs to press Ctrl+C twice. - __main__.wait = lambda: None - async_to_sync(inner_run, force_new_loop=True)() diff --git a/zerver/tornado/application.py b/zerver/tornado/application.py index fc8b0c12b6..9215ed99d2 100644 --- a/zerver/tornado/application.py +++ b/zerver/tornado/application.py @@ -12,7 +12,7 @@ def setup_tornado_rabbitmq(queue_client: TornadoQueueClient) -> None: # nocover autoreload.add_reload_hook(lambda: queue_client.close()) -def create_tornado_application() -> tornado.web.Application: +def create_tornado_application(*, autoreload: bool = False) -> tornado.web.Application: django_handler = BaseHandler() django_handler.load_middleware() @@ -26,7 +26,7 @@ def create_tornado_application() -> tornado.web.Application: return tornado.web.Application( [(url, AsyncDjangoHandler, dict(django_handler=django_handler)) for url in urls], debug=settings.DEBUG, - autoreload=False, + autoreload=autoreload, # Disable Tornado's own request logging, since we have our own log_function=lambda x: None, )