Setup

  • Solr version: 8.11.3
  • Drupal Core version: 11.3.5
  • Search API version: 1.40.0
  • Search API Solr version: 4.3.10
  • Configured Solr Connector: standard

Issue

SolrConnectorPluginBase::connect() creates a new Solarium\Client each time the connector is reinstantiated, but the old client is never freed. In long-lived PHP processes such as a full Behat test suite run, Solarium\Client instances accumulate indefinitely in memory and are never garbage-collected.

Solarium\Plugin\CustomizeRequest registers itself as a listener on the injected event dispatcher when first used, creating a circular reference: event dispatcher → CustomizeRequest → Solarium\Client → event dispatcher

PHP's reference counting cannot break this cycle, so every discarded client stays in memory for the lifetime of the process. In long-lived processes (Behat, Drush, migrations), this causes unbounded accumulation.

Proposed resolution

Implement __destruct() on SolrConnectorPluginBase to call Client::removePlugin()` on each plugin, which triggers deinitPlugin() and removes the listener, breaking the cycle:

public function __destruct() {
  if ($this->solr) {
    foreach (array_keys($this->solr->getPlugins()) as $key) {
      $this->solr->removePlugin($key);
    }
  }
}
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

herved created an issue. See original summary.

herved’s picture

herved’s picture

For context, even with 2,226 accumulated Solarium\Client instances across a full behat test suite run, the total memory footprint was ~800 KB. So this is not a significant memory concern in practice, but the dangling listeners are still a correctness issue as they keep stale clients reachable and could cause listeners to fire on discarded connectors.

mkalkbrenner’s picture

Status: Needs review » Needs work

Can you open a MR here at drupal.org?

herved’s picture

Oh, is github no longer used? the project page still mentions to use it, under "Patches and Issues Workflow" https://www.drupal.org/project/search_api_solr

mkalkbrenner’s picture

yes, the tests are now working here. I just removed the statement from the project page. Thanks :-)

herved’s picture

Status: Needs work » Needs review

  • mkalkbrenner committed f3b3106e on 4.x authored by herved
    Issue #3584036: Implement SolrConnectorPluginBase::__destruct to remove...
mkalkbrenner’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.