Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v21-branch] Fix memory leak on indexing ES on local / dev / staging #530

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), [] );
}