Skip to content

Commit

Permalink
Merge pull request php-amqplib#630 from Shivox/process-signal-handling
Browse files Browse the repository at this point in the history
Fix and add test for signal handling with PCNTL extension
  • Loading branch information
lukebakken authored Nov 29, 2018
2 parents 4664f9f + 11fdee9 commit dbd074f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
19 changes: 10 additions & 9 deletions PhpAmqpLib/Wire/IO/StreamIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class StreamIO extends AbstractIO
private $canDispatchPcntlSignal;

/** @var string */
private static $ERRNO_EQUALS_EAGAIN;
private static $SOCKET_STRERROR_EAGAIN;

/** @var string */
private static $ERRNO_EQUALS_EWOULDBLOCK;
private static $SOCKET_STRERROR_EWOULDBLOCK;

/** @var string */
private static $ERRNO_EQUALS_EINTR;
private static $SOCKET_STRERROR_EINTR;

/**
* @param string $host
Expand All @@ -85,10 +85,10 @@ public function __construct(
throw new \InvalidArgumentException('read_write_timeout must be at least 2x the heartbeat');
}

// SOCKET_EAGAIN can't be accessed in Windows
self::$ERRNO_EQUALS_EAGAIN = 'errno=' . (defined('SOCKET_EAGAIN') ? SOCKET_EAGAIN : SOCKET_EWOULDBLOCK);
self::$ERRNO_EQUALS_EWOULDBLOCK = 'errno=' . SOCKET_EWOULDBLOCK;
self::$ERRNO_EQUALS_EINTR = 'errno=' . SOCKET_EINTR;
// SOCKET_EAGAIN is not defined in Windows
self::$SOCKET_STRERROR_EAGAIN = socket_strerror(defined(SOCKET_EAGAIN) ? SOCKET_EAGAIN : SOCKET_EWOULDBLOCK);
self::$SOCKET_STRERROR_EWOULDBLOCK = socket_strerror(SOCKET_EWOULDBLOCK);
self::$SOCKET_STRERROR_EINTR = socket_strerror(SOCKET_EINTR);

$this->protocol = 'tcp';
$this->host = $host;
Expand Down Expand Up @@ -347,13 +347,14 @@ public function write($data)
public function error_handler($errno, $errstr, $errfile, $errline, $errcontext = null)
{
// fwrite notice that the stream isn't ready - EAGAIN or EWOULDBLOCK
if (strpos($errstr, self::$ERRNO_EQUALS_EAGAIN) !== false || strpos($errstr, self::$ERRNO_EQUALS_EWOULDBLOCK) !== false) {
if (strpos($errstr, self::$SOCKET_STRERROR_EAGAIN) !== false
|| strpos($errstr, self::$SOCKET_STRERROR_EWOULDBLOCK) !== false) {
// it's allowed to retry
return null;
}

// stream_select warning that it has been interrupted by a signal - EINTR
if (strpos($errstr, self::$ERRNO_EQUALS_EINTR) !== false) {
if (strpos($errstr, self::$SOCKET_STRERROR_EINTR) !== false) {
// it's allowed while processing signals
return null;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Functional/Bug/Bug458Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace PhpAmqpLib\Tests\Functional\Bug;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PHPUnit\Framework\TestCase;

class Bug458Test extends TestCase
{
private $channel;

public function setUp()
{
if (!extension_loaded('pcntl')) {
$this->markTestSkipped('pcntl extension is not available');
}

$connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);

$this->channel = $connection->channel();
$this->addSignalHandlers();
}

/**
* This test will be skipped in Windows, because pcntl extension is not available there
*
* @test
*
* @expectedException PhpAmqpLib\Exception\AMQPIOWaitException
*/
public function stream_select_interruption()
{
$pid = getmypid();
exec('php -r "sleep(1);posix_kill(' . $pid . ', SIGTERM);" > /dev/null 2>/dev/null &');
$this->channel->wait(null, false, 2);
}

private function addSignalHandlers()
{
pcntl_signal(SIGTERM, function () {
// do nothing
});
}
}

0 comments on commit dbd074f

Please sign in to comment.