Skip to content

Commit

Permalink
Merge pull request #531 from humanmade/backport-528-to-v19-branch
Browse files Browse the repository at this point in the history
[Backport v19-branch] Fix memory leak on indexing ES on local / dev / staging
  • Loading branch information
joehoyle authored Jan 24, 2025
2 parents cd46e53 + cb92217 commit d1ea426
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ElasticPress\Utils;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use ReflectionProperty;
use WP_CLI;
use WP_Error;
use WP_Post;
Expand Down Expand Up @@ -211,6 +212,9 @@ function load_elasticpress() {

// Set up packages feature.
Packages\bootstrap();

// Hook the reset_elasticsearch_queries function to the ep_stop_the_insanity action.
add_action( 'ep_stop_the_insanity', __NAMESPACE__ . '\\reset_elasticsearch_queries' );
}

/**
Expand Down Expand Up @@ -2194,3 +2198,17 @@ function sanitize_query_args( WP_Query $query ) : void {
$query->set( $key, array_values( array_filter( (array) $query->get( $key ) ) ) );
}
}

/**
* Reset Elasticsearch queries.
*
* This function resets the Elasticsearch queries by using reflection to access
* and modify the private 'queries' property of the \ElasticPress\Elasticsearch class.
*
* @return void
*/
function reset_elasticsearch_queries() {
$reflection = new ReflectionProperty( Elasticsearch::class, 'queries' );
$reflection->setAccessible( true );
$reflection->setValue( Elasticsearch::factory(), [] );
}

0 comments on commit d1ea426

Please sign in to comment.