Skip to content

track provider names per module in registry so reload_plugins actually evicts them#17

Open
HrachShah wants to merge 1 commit into
mainfrom
fix/registry-reload-pop-by-provider-name
Open

track provider names per module in registry so reload_plugins actually evicts them#17
HrachShah wants to merge 1 commit into
mainfrom
fix/registry-reload-pop-by-provider-name

Conversation

@HrachShah

@HrachShah HrachShah commented Jun 24, 2026

Copy link
Copy Markdown
Owner

ProviderRegistry.reload_plugins recorded _loaded_providers keyed by the configured name but tried to evict with self._loaded_providers.pop(provider_class.__name__) — using the class name (e.g. "OpenAIProvider") as the key when the key was the configured alias (e.g. "openai-primary"). The pop always raised KeyError, the old entries leaked in the dict, and reloads accumulated stale provider instances forever (memory leak + the new provider module's class never actually replaced the old one).

Fix: track the configured name alongside the class identity in a parallel dict keyed by configured name, so both the load and the pop go through the same key.

Summary by Sourcery

Ensure plugin reload correctly evicts and replaces provider registrations without leaking stale instances.

Bug Fixes:

  • Fix provider eviction during plugin reload by tracking provider registrations per module instead of by class name.

Enhancements:

  • Track all provider names registered by each plugin module to support accurate reload behavior.
  • Improve plugin loading error reporting by distinguishing import errors from syntax or attribute errors in plugin modules.

…y evicts them

ProviderRegistry.reload_plugins() was reading self._loaded_modules[module_name]
and popping the result from self._providers. But self._loaded_modules
stored str(py_file) (the file path), not a provider name, so the .pop()
was a no-op against the actual provider-name keys. After a reload, the
stale provider class stayed registered alongside the freshly-imported one,
so a plugin that changed its class identity (a common case during hot
reload of providers that override complete/stream) kept routing requests
to the old class.

Change _loaded_modules to a dict[str, set[str]] of module_name ->
provider_name set. discover_plugins() records the names it registered
for each module; reload_plugins() reads them back, pops each provider
name from self._providers, then rediscovers. Host-registered providers
(stuffed in via reg.register() from freerelay/core/routing/factory.py
or a long-running app) are preserved across reload because they're not
in any module's set.

Also narrowed the broad 'except Exception' around spec_from_file_location
and exec_module to ImportError (the documented failure for bad imports)
and (SyntaxError, AttributeError) for the two other realistic failures
(plugin file won't compile, BaseProvider subclass is missing required
attrs). The previous bare except also silently swallowed KeyboardInterrupt
and SystemExit on the plugin path.
@sourcery-ai

sourcery-ai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Tracks provider names per plugin module so reload_plugins can correctly evict and reload provider instances, and tightens plugin error handling and logging.

Sequence diagram for plugin discovery and reload with tracked provider names

sequenceDiagram
    participant Registry as ProviderRegistry
    participant PluginFile as Plugin_py_file
    participant Module as Plugin_module
    participant Providers as _providers
    participant LoadedModules as _loaded_modules
    participant SysModules as sys.modules

    Note over Registry,PluginFile: discover_plugins
    Registry->>PluginFile: importlib.util.spec_from_file_location(module_name, py_file)
    PluginFile-->>Registry: spec
    Registry->>Module: spec.loader.exec_module(module)
    Module-->>Registry: loaded BaseProvider subclasses
    loop for each BaseProvider subclass
        Registry->>Registry: register(provider_cls)
        Registry->>LoadedModules: registered_names.add(provider_cls.name)
        Registry->>Providers: _providers[provider_cls.name] = provider_cls
    end
    Registry->>LoadedModules: _loaded_modules[module_name] = registered_names

    Note over Registry,SysModules: reload_plugins
    loop for each module_name, provider_names in _loaded_modules.items()
        Registry->>SysModules: sys.modules.pop(module_name)
        loop for each provider_name in provider_names
            Registry->>Providers: _providers.pop(provider_name)
        end
    end
    Registry->>LoadedModules: _loaded_modules.clear()
Loading

File-Level Changes

Change Details Files
Track provider names per plugin module and use them to evict providers on reload
  • Change _loaded_modules to map module_name to a set of provider names instead of a file path string
  • During plugin discovery, collect each registered provider's configured name into a set associated with the module
  • On reload, iterate module_name and its provider_names, removing the module from sys.modules and popping each associated provider from the registry
  • Clear the _loaded_modules mapping after eviction so a subsequent discover can repopulate it
freerelay/providers/registry.py
Improve plugin loading error classification and logging
  • Narrow broad exception handling around plugin loading to ImportError, SyntaxError, and AttributeError
  • Log ImportError as a failed plugin load and SyntaxError/AttributeError as invalid/broken plugins with clearer messaging
  • Ensure provider logging for loaded plugins references attr.name with appropriate type-ignore comments
freerelay/providers/registry.py

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

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@HrachShah, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 53 minutes and 47 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f7327015-654b-46f9-87be-fff2c791223a

📥 Commits

Reviewing files that changed from the base of the PR and between ec9d17f and 19c6439.

📒 Files selected for processing (1)
  • freerelay/providers/registry.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/registry-reload-pop-by-provider-name

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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 left some high level feedback:

  • Catching a bare AttributeError when loading plugins may conflate missing BaseProvider attributes with unrelated AttributeErrors inside plugin initialization logic; consider narrowing this to a custom validation step on discovered providers or logging the full traceback so plugin authors can debug non-schema-related issues.
  • Since _loaded_modules now tracks provider names per module, it might be helpful to skip storing entries for modules that register no providers (i.e., only set _loaded_modules[module_name] when registered_names is non-empty) to avoid later reload work on effectively no-op plugin files.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Catching a bare AttributeError when loading plugins may conflate missing BaseProvider attributes with unrelated AttributeErrors inside plugin initialization logic; consider narrowing this to a custom validation step on discovered providers or logging the full traceback so plugin authors can debug non-schema-related issues.
- Since _loaded_modules now tracks provider names per module, it might be helpful to skip storing entries for modules that register no providers (i.e., only set _loaded_modules[module_name] when registered_names is non-empty) to avoid later reload work on effectively no-op plugin files.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant