From c7bafaa7fb5cb9ad6d2f3e4a8affc7f3a220e2f2 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Sat, 7 Nov 2026 12:28:40 -0700 Subject: [PATCH] Starting the app again adds no signal routes (#949) Sanic's SignalRouter.finalize appends a `sanic.__signal__.__init__` handler before it checks whether the router is already finalized. Every start of the app finalizes it again (the test client, Sanic's startup, and finalize_signals_after_auto_routes), so each test left two more routes. After a few hundred tests in one process the compiled router was too deep for the compiler (RecursionError in sanic_routing, intermittently failing whichever tests a worker happened to run late). api_app's signal router now skips a finalize when it is already finalized, and registers that handler once. Co-authored-by: Claude Opus 5.5 --- wrolpi/api_utils.py | 25 ++++++++++++++++++++++++- wrolpi/test/test_api_utils.py | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/wrolpi/api_utils.py b/wrolpi/api_utils.py index c2c0efd5..857ce045 100644 --- a/wrolpi/api_utils.py +++ b/wrolpi/api_utils.py @@ -10,6 +10,7 @@ from http import HTTPStatus from pathlib import Path from sanic import response, HTTPResponse, Request, Sanic, SanicException +from sanic.signals import SignalRouter from sanic_ext.config import PRIORITY as SANIC_EXT_PRIORITY from wrolpi.common import Base, get_media_directory, logger, LOGGING_CONFIG @@ -38,8 +39,30 @@ def _reset_inherited_signal_handlers(): os.register_at_fork(after_in_child=_reset_inherited_signal_handlers) +# Sanic's internal signal, see `SignalRouterFinalizedOnce`. +SIGNAL_INIT_EVENT = 'sanic.__signal__.__init__' + + +class SignalRouterFinalizedOnce(SignalRouter): + """Sanic's SignalRouter, which finalizing again leaves unchanged. + + `SignalRouter.finalize` appends a `sanic.__signal__.__init__` handler before it checks whether the router is + already finalized; an app that is started again (every test, or `finalize_signals_after_auto_routes`) gained a + route each time, until the compiled router was too deep to compile.""" + + def add(self, handler, event, *args, **kwargs): + if str(event) == SIGNAL_INIT_EVENT and (existing := self.name_index.get(SIGNAL_INIT_EVENT)): + return existing + return super().add(handler, event, *args, **kwargs) + + def finalize(self, *args, **kwargs): + if self.finalized: + return self + return super().finalize(*args, **kwargs) + + # The only Sanic App, this is imported all over. -api_app = Sanic(name='api_app', log_config=LOGGING_CONFIG) +api_app = Sanic(name='api_app', log_config=LOGGING_CONFIG, signal_router=SignalRouterFinalizedOnce()) # sanic-ext adds the automatic HEAD and OPTIONS routes one at a time in a `before_server_start` listener at # SANIC_EXT_PRIORITY. Each route re-finalizes the finalized signal router (`Sanic.amend`), so a worker rebuilt it once diff --git a/wrolpi/test/test_api_utils.py b/wrolpi/test/test_api_utils.py index 4ef71c8e..aaa7c180 100644 --- a/wrolpi/test/test_api_utils.py +++ b/wrolpi/test/test_api_utils.py @@ -79,6 +79,31 @@ async def test_stop_per_worker_tasks_without_start(): await stop_per_worker_tasks(app) +@pytest.mark.asyncio +async def test_finalizing_signals_again_adds_no_routes(test_session, async_client): + """Each start of the app finalizes the signal router again (the test client, Sanic's startup, and + `finalize_signals_after_auto_routes`). Sanic's finalize appends a `sanic.__signal__.__init__` handler before it + checks whether the router is finalized, so every start left routes behind; after a few hundred tests in one + process the compiled router was too deep to compile (RecursionError).""" + await async_client.get('/api/echo') + before = len(api_app.signal_router.routes) + + # An app that is already started. + api_app.signalize() + api_app.signalize() + assert len(api_app.signal_router.routes) == before + + # What each start does (see `unfinalize_signals_for_auto_routes`). + api_app.signal_router.reset() + api_app.signalize() + assert len(api_app.signal_router.routes) == before + assert api_app.signal_router.finalized + + # Signals still dispatch. + request, response = await async_client.get('/api/echo') + assert response.status_code == 200 + + @pytest.mark.asyncio async def test_server_start_finalizes_signals_once(test_session, async_client): """sanic-ext's automatic HEAD and OPTIONS routes must not re-finalize the signal router once per route; each