Repository navigation
fix(plugins): bind gateway settings to their app - #525
Conversation
Foca(...) without gateway= cleared the gateway settings of the process until its host was created, e.g., when a Celery worker module with a plain Foca(...) was imported during create_app(). Hosts and backend lists also used the configuration of the FOCA app created last. The first Foca(gateway=...) now binds its settings and configuration to the process; later ones must pass the same settings, whether or not a host exists, and Foca(...) without gateway= leaves them alone. Only reset() clears them. Closes #517 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Foca(gateway=...) with equal settings but another configuration was accepted, and its configuration was ignored: create_app() checked the plugins of the first app, and hosts and backend lists used them. create_app() now binds the configuration of the app it creates, and fails while the host of another configuration is in use (comparing configurations is no option: create_app() changes them, e.g., db). Also use _bound() in get_backend_list() and test forget() and the errors after reset(). Refs #517 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR makes gateway runtime state process-safe by binding a GatewayApp to its originating Config, ignoring plain Foca instances, and sourcing plugin and backend setup from that bound pair. create_app() explicitly rebinds its own configuration when safe, reset clears the binding, documentation explains the required shared GatewayApp usage, and focused tests cover import-order, rebinding, fork, and failure scenarios. Sequence diagram for gateway configuration bindingsequenceDiagram
participant GatewayApp
participant FocaGateway
participant Runtime
participant FocaWorker
participant Host
GatewayApp->>FocaGateway: Foca(gateway=GatewayApp)
FocaGateway->>Runtime: configure(GatewayApp, Config)
Runtime-->>FocaGateway: Bind app and config
FocaWorker->>FocaWorker: Foca() without gateway
FocaWorker-->>Runtime: Leave binding unchanged
GatewayApp->>FocaGateway: create_app()
FocaGateway->>Runtime: configure(GatewayApp, AppConfig, rebind=True)
Runtime-->>FocaGateway: Rebind when host is unused
FocaGateway->>Runtime: create_host()
Runtime->>Host: Build from bound Config
Host-->>Runtime: PluginHost
State diagram for gateway runtime bindingstateDiagram-v2
[*] --> Unbound
Unbound --> Bound: configure(GatewayApp, Config)
Bound --> Bound: Foca() without gateway
Bound --> Bound: configure(same GatewayApp, Config)
Bound --> Bound: configure(same GatewayApp, new Config, rebind=True) / host unused
Bound --> Error: configure(other GatewayApp)
Bound --> Error: rebind=True / host in use
Bound --> HostActive: create_host()
HostActive --> Bound: reset()
Bound --> Unbound: reset()
Error --> Bound: reset()
File-Level Changes
Assessment against linked issues
Possibly linked issues
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 #525 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 55
Lines 6611 6616 +5
=========================================
+ Hits 6611 6616 +5
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 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/foca.py" line_range="206" />
<code_context>
# each process creates its own host on first use (after forks), so
# plugins are instantiated (`setup()`) once more there
if self.gateway is not None:
+ runtime.configure(self.gateway, self.conf, rebind=True)
runtime.create_host().close()
if self.gateway.backends is not None:
</code_context>
<issue_to_address>
**Earlier apps use later configuration**
When app A has returned without creating a host, then app B with the same gateway settings but a different configuration is created before A handles its first request, `configure()` allows the process-wide binding to change while `_runtime.host` is `None`, and A’s later `runtime.get_host()` uses B’s config, so A’s requests expose the wrong plugins or backends.
Keep the runtime configuration app-scoped, or reject rebinding once an app has been returned.
Also at `foca/plugins/runtime.py:138-144`.
</issue_to_address>
### Comment 2
<location path="foca/foca.py" line_range="206" />
<code_context>
# each process creates its own host on first use (after forks), so
# plugins are instantiated (`setup()`) once more there
if self.gateway is not None:
+ runtime.configure(self.gateway, self.conf, rebind=True)
runtime.create_host().close()
if self.gateway.backends is not None:
</code_context>
<issue_to_address>
**Startup validates another app’s configuration**
When two gateway apps with equal settings but different configurations call `create_app()` concurrently, `runtime.configure()` releases its lock before `create_host()` or `get_backend_list()` reads the binding, so one startup validates the other app’s plugins or backends and can accept an invalid app or reject a valid one.
Serialize each app’s rebind together with its plugin and backend validation.
Also at `foca/plugins/runtime.py:138-144`.
</issue_to_address>
### Comment 3
<location path="foca/foca.py" line_range="206" />
<code_context>
# each process creates its own host on first use (after forks), so
# plugins are instantiated (`setup()`) once more there
if self.gateway is not None:
+ runtime.configure(self.gateway, self.conf, rebind=True)
runtime.create_host().close()
if self.gateway.backends is not None:
</code_context>
<issue_to_address>
**Failed startup leaves config bound**
When a different app config is rebound while no persistent host exists, and startup validation raises but the process continues, `runtime.configure()` leaves the failed app’s config bound when `create_host()` or `get_backend_list()` raises, so later tasks or callers build from that config instead of the previously working one.
Restore the previous runtime binding if startup validation fails.
Also at `foca/plugins/runtime.py:138-144`.
</issue_to_address>
### Comment 4
<location path="foca/foca.py" line_range="206" />
<code_context>
# each process creates its own host on first use (after forks), so
# plugins are instantiated (`setup()`) once more there
if self.gateway is not None:
+ runtime.configure(self.gateway, self.conf, rebind=True)
runtime.create_host().close()
if self.gateway.backends is not None:
</code_context>
<issue_to_address>
**Startup checks stale plugin capabilities**
When a later gateway app’s service-info runtime fields call `service_info_fields()` before the runtime is rebound, `check_service_info()` runs before `runtime.configure()`, so `service_info_fields()` reaches `runtime.plugin_classes()` with the previously bound config; startup can validate or expose stale capabilities, or reject a valid app.
Run the service-info check using the new app’s runtime configuration.
Also at `foca/plugins/runtime.py:251`.
</issue_to_address>
### Comment 5
<location path="foca/plugins/runtime.py" line_range="130" />
<code_context>
- if _runtime.host is not None and app != _runtime.app:
+ if _runtime.app is None:
+ _runtime.app = app
+ _runtime.config = config
+ return
+ if app != _runtime.app:
</code_context>
<issue_to_address>
**Lookups fail during binding**
When the first gateway binding is being initialized while another thread calls a plugin or backend lookup, `configure()` publishes `_runtime.app` before `_runtime.config`, while `_bound()` reads both without the lock, so the lookup sees a partial binding and raises `RuntimeError`, interrupting startup or a request.
Publish the app and config atomically, or make `_bound()` read them under the same lock.
Also at `foca/plugins/runtime.py:177-179`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 5 findings to address first, and if the wrong configuration is bound, the gateway can load the wrong plugins and build its backend list from the wrong settings, potentially sending work to an unintended external backend. Reverting restores the binding but cannot recall work already dispatched.
Blocking findings: foca/foca.py:206, foca/foca.py:206, foca/foca.py:206, foca/foca.py:206, foca/plugins/runtime.py:130
Rebinding the configuration in create_app() let an app that was already returned use the configuration of a later app, raced with concurrent startups, kept a failed app's configuration bound and ran the service info check against the previous configuration (Sourcery). create_app() now serves only the app whose configuration the first Foca(gateway=...) bound, checked before anything else, and fails otherwise (reset() clears the binding, e.g., in tests). Settings and configuration are bound together as one tuple, so readers without the lock never see one without the other. Refs #517 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Fixes finding 2 (medium) of the FOCA v0.18.0 pre-release review: a later
Foca(...)withoutgateway=(e.g., the module-levelFoca(...)in proTES's Celery worker module, which Connexion imports while the gateway app is created) silently cleared the gateway settings of the process, socreate_app()or the first gateway request failed with a misleading "No gateway app configured" error.Design (gateway settings bound to their app's configuration, fails safe):
Foca(...)withoutgateway=no longer touches the runtime.Foca(gateway=...)binds itsGatewayAppand itsConfigto the process, as one tuple. Every laterFoca(gateway=...)must pass equal settings, whether or not a host exists (before, this depended on import order); onlyruntime.reset()clears them.create_host(),plugin_classes()andget_backend_list()use the bound configuration instead ofget_config()"latest" (which may be a worker module's).create_app()serves only the app whose configuration is bound, checked before anything else, and fails otherwise (e.g., on a retry after a failed startup, or in tests withoutreset()). Nothing ever rebinds a configuration. Comparing configurations was not an option:create_app()changes them (e.g.,db).docs/plugins.md: everyFoca(gateway=...)in a gateway process, also in a Celery worker module, must pass the sameGatewayApp(define it once and import it, since new lambdas compare unequal); create the gateway app'sFoca(...)before importing modules that create others.proTES flow:
app.py'sFoca(gateway=G)binds; the worker module'sFoca(...)(plain, or withgateway=G), imported duringcreate_app(), changes nothing; a Celery worker process that imports only the worker module binds the worker's own configuration.Closes #517
Verification
Focathen plainFoca(also with the plain app created last, so "latest" points to it), otherGatewayAppsettings before any host exists (other API, newlambda),create_app()of another app with the same settings,forget()keeping the binding, errors afterreset().coverage run -m pytestwith MongoDB 7.0: 2924 passed, 1 skipped;coverage report: 100%.ruff check,ruff format --check,mypy: clean.CI passed, tests on Python 3.11-3.15 with MongoDB, lowest deps, integration, codecov).Reviews
adversarial-reviewerandquality-reviewer(round 1),adversarial-reviewer(round 2, no findings). Sourcery found 5 problems in the round-2 design (rebinding the configuration increate_app()), fixed in d693330 by dropping the rebind and publishing the binding atomically.Declined review findings
!) and ship a reset fixture for consumers:foca/plugins/runtime.pyis not in v0.17.0; the API is unreleased, so nothing breaks.Foca, the module docstring,configure()and the docs: shortened theFocadocstring to point toconfigure(); the rest is user documentation.Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Bind gateway settings and their configuration atomically to the first gateway app in a process and prevent unrelated FOCA instances from changing which app is served.
Bug Fixes:
Enhancements:
Documentation:
Tests: