Repository navigation
Description
Problem
Flask apps aren't released after FOCA installs its route table on their Connexion app (foca.api.routes._install_route_table(), called by create_connexion_app() and register_openapi()).
The route table is installed by a per-app subclass of the app's API class, _RecordingApi. Its method _add_operation_internal() refers to the Flask app through its closure. So as long as any API object of the app, or the class itself, lives on, the Flask app lives on too, with everything it holds (configuration, database clients, routes).
Anything may keep an API object: for example, log records that a handler stores. With pytest's log capture, once a test has enabled DEBUG for Connexion's loggers, the records of Connexion's messages while specifications are registered refer to the API objects. So in a test suite, apps survive tests, depending on the order of the tests. This was seen in #734.
This matters for processes that build many apps, e.g., test suites. It is older than #734.
Reproduction
import gc
import weakref
from foca.factories.connexion_app import create_connexion_app
cnx_app = create_connexion_app()
api = cnx_app.add_api(spec) # any specification
app = weakref.ref(cnx_app.app)
del cnx_app
gc.collect()
assert app() is None # fails while `api` is referencedProposed fix
Let _RecordingApi refer to the Flask app weakly, so that it no longer keeps the app alive. No other change of behaviour. Add a regression test and an entry under Fixes in the release notes of 0.22.0.
Other registries checked
- The parameter validators of
foca.api.validation(fixed in feat: list body and parameter errors together #734 with weak references): apps are collected. - Process-wide caches keyed by strings (OIDC and JWKS caches, registry caches,
lru_caches), the configuration of the latest app (foca.config.access) and the plugin runtime keep one entry per key or process, by design, not one per app. - Celery: Celery keeps its latest app as the current app (by design of Celery). Separately, the worker signal handlers that FOCA connects for apps with a database keep each app's database configuration and client alive; this is a different leak, to be tracked separately if wanted.
Activity