Skip to content

fix: release apps after installing the route table #735

Closed

Description

@uniqueg

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 referenced

Proposed 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

  1. added this to the v0.22.0 milestone on Nov 7, 2026
  2. added
    priority: low
    status: in progress
    type: bug
    workload: hours
    on Nov 7, 2026
  3. removed
    status: in progress
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: lowtype: bugworkload: hours

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions