Skip to content

Commit

Permalink
Merge pull request #888 from php-amqplib/fix_value_error_on_closed_so…
Browse files Browse the repository at this point in the history
…cket

fix ValueError on closed or broken socket
  • Loading branch information
lukebakken committed Feb 24, 2021
2 parents 688a125 + eaf4507 commit 85da8ec
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions PhpAmqpLib/Wire/IO/AbstractIO.php
Expand Up @@ -2,6 +2,7 @@

namespace PhpAmqpLib\Wire\IO;

use PhpAmqpLib\Exception\AMQPConnectionClosedException;
use PhpAmqpLib\Exception\AMQPHeartbeatMissedException;
use PhpAmqpLib\Exception\AMQPIOWaitException;
use PhpAmqpLib\Wire\AMQPWriter;
Expand Down Expand Up @@ -105,6 +106,7 @@ public function select($sec, $usec)
* @param int|null $sec
* @param int|null $usec
* @return int|bool
* @throws AMQPConnectionClosedException
*/
abstract protected function do_select($sec, $usec);

Expand Down
5 changes: 5 additions & 0 deletions PhpAmqpLib/Wire/IO/SocketIO.php
Expand Up @@ -241,6 +241,11 @@ public function close()
*/
protected function do_select($sec, $usec)
{
if ($this->sock === null || !is_resource($this->sock)) {
$this->sock = null;
throw new AMQPConnectionClosedException('Broken pipe or closed connection', 0);
}

$read = array($this->sock);
$write = null;
$except = null;
Expand Down
5 changes: 5 additions & 0 deletions PhpAmqpLib/Wire/IO/StreamIO.php
Expand Up @@ -335,6 +335,11 @@ public function getSocket()
*/
protected function do_select($sec, $usec)
{
if ($this->sock === null || !is_resource($this->sock)) {
$this->sock = null;
throw new AMQPConnectionClosedException('Broken pipe or closed connection', 0);
}

$read = array($this->sock);
$write = null;
$except = null;
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Wire/IO/SocketIOTest.php
Expand Up @@ -2,6 +2,7 @@

namespace PhpAmqpLib\Tests\Unit\Wire\IO;

use PhpAmqpLib\Exception\AMQPConnectionClosedException;
use PhpAmqpLib\Wire\IO\SocketIO;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -75,4 +76,24 @@ public function write_when_closed(SocketIO $socketIO)
{
$socketIO->write('data');
}

/**
* @test
* @group linux
* @requires OS Linux
*/
public function select_must_throw_io_exception()
{
$this->expectException(AMQPConnectionClosedException::class);
$property = new \ReflectionProperty(SocketIO::class, 'sock');
$property->setAccessible(true);

$resource = fopen('php://temp', 'r');
fclose($resource);

$socket = new SocketIO('0.0.0.0', PORT, 0.1, 0.1, null, false, 0);
$property->setValue($socket, $resource);

$socket->select(0, 0);
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Wire/IO/StreamIOTest.php
Expand Up @@ -2,6 +2,7 @@

namespace PhpAmqpLib\Tests\Unit\Wire\IO;

use PhpAmqpLib\Exception\AMQPConnectionClosedException;
use PhpAmqpLib\Wire\IO\StreamIO;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -32,11 +33,11 @@ public function read_write_timeout_must_be_at_least_2x_the_heartbeat()
/**
* @test
* @group linux
* @expectedException \PhpAmqpLib\Exception\AMQPIOWaitException
* @requires OS Linux
*/
public function select_must_throw_io_exception()
{
$this->expectException(AMQPConnectionClosedException::class);
$property = new \ReflectionProperty(StreamIO::class, 'sock');
$property->setAccessible(true);

Expand Down

0 comments on commit 85da8ec

Please sign in to comment.