Skip to content

Commit

Permalink
Clear pending responses in reader buffer on disconnect.
Browse files Browse the repository at this point in the history
When using the phpiredis-based connection backends, failed pipelines led
to spurious responses returned after reconnecting to Redis because the
underlying reader was not properly reset by discarding buffered replies
after disconnecting.

Fixes a couple of issues reported in #363.
  • Loading branch information
nrk committed Sep 20, 2020
1 parent 521ac8f commit 5ae4ac6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v1.1.7 (2020-xx-xx)
================================================================================

- __FIX__: with the phpiredis-based connection backends, failed pipelines led to
spurious responses returned after reconnecting to Redis because the underlying
reader was not properly reset by discarding buffered replies after disconnecting
(ISSUE #363).


v1.1.6 (2020-09-11)
================================================================================

Expand Down
6 changes: 4 additions & 2 deletions src/Connection/PhpiredisSocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function __construct(ParametersInterface $parameters)
*/
public function __destruct()
{
phpiredis_reader_destroy($this->reader);

parent::__destruct();

phpiredis_reader_destroy($this->reader);
}

/**
Expand Down Expand Up @@ -342,7 +342,9 @@ public function connect()
public function disconnect()
{
if ($this->isConnected()) {
phpiredis_reader_reset($this->reader);
socket_close($this->getResource());

parent::disconnect();
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Connection/PhpiredisStreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,19 @@ public function __construct(ParametersInterface $parameters)
*/
public function __destruct()
{
parent::__destruct();

phpiredis_reader_destroy($this->reader);
}

parent::__destruct();
/**
* {@inheritdoc}
*/
public function disconnect()
{
phpiredis_reader_reset($this->reader);

parent::disconnect();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Predis/Connection/PhpiredisSocketConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ public function testThrowsExceptionOnInitializationCommandFailure()
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //

/**
* @group connected
*/
public function testClearsPendingResponsesInReaderBufferOnDisconnect()
{
$profile = $this->getCurrentProfile();
$connection = $this->createConnection();

$cmdECHO1 = $profile->createCommand('echo', array('BEFORE DISCONNECT 1'));
$cmdECHO2 = $profile->createCommand('echo', array('BEFORE DISCONNECT 2'));
$cmdECHO3 = $profile->createCommand('echo', array('AFTER DISCONNECT 1'));

$connection->writeRequest($cmdECHO1);
$connection->writeRequest($cmdECHO2);
$connection->readResponse($cmdECHO1);
$connection->disconnect();

$response = $connection->executeCommand($cmdECHO3);

$this->assertSame('AFTER DISCONNECT 1', $response);
}

/**
* @group connected
* @expectedException \Predis\Connection\ConnectionException
Expand Down
22 changes: 22 additions & 0 deletions tests/Predis/Connection/PhpiredisStreamConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ public function testThrowsExceptionOnInitializationCommandFailure()
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //

/**
* @group connected
*/
public function testClearsPendingResponsesInReaderBufferOnDisconnect()
{
$profile = $this->getCurrentProfile();
$connection = $this->createConnection();

$cmdECHO1 = $profile->createCommand('echo', array('BEFORE DISCONNECT 1'));
$cmdECHO2 = $profile->createCommand('echo', array('BEFORE DISCONNECT 2'));
$cmdECHO3 = $profile->createCommand('echo', array('AFTER DISCONNECT 1'));

$connection->writeRequest($cmdECHO1);
$connection->writeRequest($cmdECHO2);
$connection->readResponse($cmdECHO1);
$connection->disconnect();

$response = $connection->executeCommand($cmdECHO3);

$this->assertSame('AFTER DISCONNECT 1', $response);
}

/**
* @group connected
* @group slow
Expand Down

0 comments on commit 5ae4ac6

Please sign in to comment.