Repository navigation
fix: release apps after installing the route table - #737
Conversation
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideImplementationUse a weak reference to the Flask app and skip recording operations if that app has been collected.
DocumentationDocument the app-lifecycle fix in the 0.22.0 release notes.
TestsVerify that apps are collected while API objects remain alive and that operations on a replaced app are not recorded.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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
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]>
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 enabledDEBUGfor 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.tests/api/test_routes.py::TestInstall::test_apps_collectedkeeps an API object alive, drops the app, runsgc.collect()and checks that the app is gone. It failed before the fix.Closes #735
Other per-app registries checked
foca.api.validation(weak since feat: list body and parameter errors together #734): apps are collected.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.weak=False, infoca/factories/celery_app.py) keep every such app'sMongoConfigand its client alive, but not the Flask app. Not changed here; it can be tracked separately.Verification
pytest tests/api: 266 passed.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.devat 92e24a1: 1722 passed, 11 skipped, 100% coverage.devat 31c7b8f: 2338 passed, 11 skipped, 100% coverage.cnx_app.appwas replaced and the original collected raisedAttributeError. 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 testTestInstall::test_app_replaced;tests/apipasses (267), withfoca/api/routes.py100% covered. Round 2 adversarial review: no findings.Declined review findings
Open questions
None.
🤖 Generated with Claude Code