Skip to content

fix: release apps after installing the route table - #737

Merged
uniqueg merged 3 commits into
devfrom
fix/735-release-apps
Nov 7, 2026
Merged

uniqueg merged 3 commits into
devfrom
fix/735-release-apps

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Flask apps are now released after FOCA installs its route table on their Connexion app. The per-app API class _RecordingApi (foca.api.routes._install_route_table()) referred to the Flask app through its closure. So any API object that lived on, or the class itself, kept the app alive. For example, pytest's log capture keeps log records that refer to API objects once a test has enabled DEBUG for Connexion's loggers. The class now holds a weak reference to the app and dereferences it while operations are added. That happens only while the app registers an API, so the app is always alive at that point. Nothing else changes.

  • Regression test: tests/api/test_routes.py::TestInstall::test_apps_collected keeps an API object alive, drops the app, runs gc.collect() and checks that the app is gone. It failed before the fix.
  • Release notes 0.22.0: new entry under Fixes.

Closes #735

Other per-app registries checked

  • The parameter validators of foca.api.validation (weak since feat: list body and parameter errors together #734): apps are collected.
  • Process-wide caches keyed by strings (OIDC/JWKS, registry caches, lru_caches), the configuration of the latest app (foca.config.access) and the plugin runtime hold one entry per key or per process, by design.
  • Celery: Celery keeps its latest app as the current app (by design). Separately, the worker signal handlers that FOCA connects for apps with a database (weak=False, in foca/factories/celery_app.py) keep every such app's MongoConfig and its client alive, but not the Flask app. Not changed here; it can be tracked separately.

Verification

  • pytest tests/api: 266 passed.
  • Full FOCA suite with a local MongoDB 7.0: 4876 passed, 2 skipped, 100% coverage. One local-only failure, test_version: the editable install's version metadata changed during the run because a downstream venv reinstalled the package. It is unrelated to this change.
  • ruff, ruff format, mypy: passed.
  • Downstream, against this branch (editable install), with locked dependencies and Helm chart dependencies built:
    • proTES dev at 92e24a1: 1722 passed, 11 skipped, 100% coverage.
    • cloud-registry dev at 31c7b8f: 2338 passed, 11 skipped, 100% coverage.
  • CI on a531f08 (before the Sourcery fix): all checks passed (CI passed, CodeQL, Read the Docs, Codecov); image push/publish skipped as intended on PRs.
  • Review (full lane): adversarial-reviewer and quality-reviewer. Round 1 had no high or medium findings.
  • Sourcery (medium), also raised as a low in round 1: adding an API after cnx_app.app was replaced and the original collected raised AttributeError. Fixed in 937bb84: the operation isn't recorded, as before, when it went into the table of the unreachable original app; Connexion still adds the Flask route. Regression test TestInstall::test_app_replaced; tests/api passes (267), with foca/api/routes.py 100% covered. Round 2 adversarial review: no findings.

Declined review findings

  • Release note wording (low): kept, as it names the case that matters to apps.

Open questions

None.

🤖 Generated with Claude Code

uniqueg and others added 2 commits November 7, 2026 13:20
The per-app API class that records operations in the route table
referred to its Flask app through its closure, so any API object that
lived on, e.g., in log records kept by pytest's log capture, kept the
app alive. Refer to the app weakly.

Closes #735

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Use a weak reference to the Flask app and skip recording operations if that app has been collected.

  • foca/api/routes.py L21, L142-144, L168-171

Documentation

Document the app-lifecycle fix in the 0.22.0 release notes.

  • docs/releases/0.22.0.md L15-18

Tests

Verify that apps are collected while API objects remain alive and that operations on a replaced app are not recorded.

  • tests/api/test_routes.py L3-4, L312-336

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3831f0e) to head (937bb84).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #737   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           74        74           
  Lines         9373      9377    +4     
=========================================
+ Hits          9373      9377    +4     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 7, 2026 14:36

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="foca/api/routes.py" line_range="169" />
<code_context>
             # Operations that failed to resolve have a function answering so
             resolution = getattr(operation, "_resolution", None)
+            # Operations are added while the app registers an API, so it lives
+            flask_app: Any = flask_app_ref()
             # As apps may have changed them, which Flask's rules follow
             table = flask_app.extensions.setdefault(_ROUTES_EXTENSION, _RouteTable())
</code_context>
<issue_to_address>
**Replacement app not followed**

The issue asks for no other behaviour change. The recording class dereferences a weak reference to the original Flask app, so adding an API after `cnx_app.app` is replaced can fail instead of recording routes in the replacement app.
</issue_to_address>

Linked issues

#735 fix: release apps after installing the route table · 3 of 4 met

# Requirement Status Notes
1 Release Flask apps while API objects remain alive ✅ Met The recording API class captures a weak reference to the Flask app, and the regression test keeps an API alive while checking that the app is collected.
2 Add a regression test ✅ Met tests/api/test_routes.py::TestInstall::test_apps_collected checks collection after deleting the Connexion app while retaining its API.
3 Add a Fixes entry to the 0.22.0 release notes ✅ Met The release notes describe the app-lifetime fix under Fixes.
4 Make no other change of behaviour 🟡 Partly met If a caller replaces cnx_app.app after installation and then adds an API, the recording class still dereferences the weak reference to the original Flask app; the PR acknowledges this case.

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: foca/api/routes.py:169


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread foca/api/routes.py Resolved
If the Flask app of a Connexion app was replaced and collected, adding
operations no longer fails: their routes are not recorded, as before,
when they went into the table of the unreachable app.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 7350253 into dev Nov 7, 2026
59 of 83 checks passed
@uniqueg
uniqueg deleted the fix/735-release-apps branch November 7, 2026 15:00
@uniqueg uniqueg added this to the v0.22.0 milestone Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: release apps after installing the route table

1 participant