Skip to content

Commit e57d51b

Browse files
committed
Discard abandoned pipelines when a bulk command fails (#628)
PhpRedis and Relay put the connection itself into pipeline mode, so a pipeline that is never executed leaves the connection answering every subsequent command with the client object instead of a value. The cache then fatals while cloning that object in add_to_internal_cache(), fatals in array_combine() on get_multiple(), and reports false from set() and delete() even though the writes reach Redis. The discard() calls sat in the count mismatch branches, which are only reachable after exec() already closed the pipeline, so they never ran when it mattered. Move them to the catch blocks, where the pipeline is still open. Predis needs no cleanup here, since its pipeline is a standalone object rather than a connection mode, but its exceptions all extend Exception and discard() on a Predis pipeline is harmless.
1 parent 7264384 commit e57d51b

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

includes/object-cache.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,8 +1250,6 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir
12501250
}, $tx->{$method}() ?: [] );
12511251

12521252
if ( count( $results ) !== count( $keys ) ) {
1253-
$tx->discard();
1254-
12551253
return array_fill_keys( $keys, false );
12561254
}
12571255

@@ -1268,6 +1266,10 @@ protected function add_multiple_at_once( array $data, $group = 'default', $expir
12681266
$this->cache_calls++;
12691267
$this->cache_time += $execute_time;
12701268
} catch ( Exception $exception ) {
1269+
if ( isset( $tx ) ) {
1270+
$tx->discard();
1271+
}
1272+
12711273
$this->handle_exception( $exception );
12721274

12731275
return array_combine( $keys, array_fill( 0, count( $keys ), false ) );
@@ -1501,13 +1503,15 @@ protected function delete_multiple_at_once( array $keys, $group = 'default' ) {
15011503
}, $tx->{$method}() ?: [] );
15021504

15031505
if ( count( $results ) !== count( $keys ) ) {
1504-
$tx->discard();
1505-
15061506
return array_fill_keys( $keys, false );
15071507
}
15081508

15091509
$execute_time = microtime( true ) - $start_time;
15101510
} catch ( Exception $exception ) {
1511+
if ( isset( $tx ) ) {
1512+
$tx->discard();
1513+
}
1514+
15111515
$this->handle_exception( $exception );
15121516

15131517
return array_combine( $keys, array_fill( 0, count( $keys ), false ) );
@@ -2287,8 +2291,6 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir
22872291
}, $tx->{$method}() ?: [] );
22882292

22892293
if ( count( $results ) !== count( $keys ) ) {
2290-
$tx->discard();
2291-
22922294
return array_fill_keys( $keys, false );
22932295
}
22942296

@@ -2300,6 +2302,10 @@ protected function set_multiple_at_once( array $data, $group = 'default', $expir
23002302
}
23012303
}
23022304
} catch ( Exception $exception ) {
2305+
if ( isset( $tx ) ) {
2306+
$tx->discard();
2307+
}
2308+
23032309
$this->handle_exception( $exception );
23042310

23052311
return array_combine( $keys, array_fill( 0, count( $keys ), false ) );

0 commit comments

Comments
 (0)