mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
parent
e5987fdd7e
commit
c7bafaa7fb
2 changed files with 49 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue