Skip to content

Commit

Permalink
Merge pull request #13 from humanmade/psocket-corrupted-fix
Browse files Browse the repository at this point in the history
Disconnect the Redis socket on fatal shutdown
  • Loading branch information
kovshenin authored Oct 12, 2023
2 parents 9ef2839 + ebc17b9 commit ea2b632
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function add_filters( $add_filter_fn = 'add_filter' ) {
$add_filter_fn( 'wp_redis_prepare_client_connection_callback', 'WP_Predis\prepare_client_connection_callback' );
$add_filter_fn( 'wp_redis_perform_client_connection_callback', 'WP_Predis\perform_client_connection_callback', 10, 3 );
$add_filter_fn( 'wp_redis_retry_exception_messages', 'WP_Predis\append_error_messages' );
$add_filter_fn( 'shutdown', 'WP_Predis\shutdown', 10, 0 );
}

function check_client_dependencies() {
Expand Down Expand Up @@ -107,3 +108,22 @@ function prepare_client_connection_callback() {
function perform_client_connection_callback() {
return 'WP_Predis\perform_client_connection';
}

/**
* Shutdown hook.
*
* Runs during shutdown, even if the shutdown happened due
* to a fatal error/exception.
*/
function shutdown() {
global $wp_object_cache;

// Disconnect a Redis socket if the shutdown happened with a fatal.
// Prevents a persistent socket from being corrupted if the error
// happens while reading from Redis.

$error = error_get_last();
if ( $error && $error['type'] === E_ERROR && isset( $wp_object_cache->redis ) ) {
$wp_object_cache->redis->disconnect();
}
}

0 comments on commit ea2b632

Please sign in to comment.