Skip to content

Commit

Permalink
Improved handling of internal errors on userland daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Carter committed Sep 23, 2015
1 parent 01ec237 commit 510eb1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Driver/Userland/UserlandDaemon.php
Expand Up @@ -7,6 +7,7 @@
use PHPFastCGI\FastCGIDaemon\DaemonTrait;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionPoolInterface;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException;
use PHPFastCGI\FastCGIDaemon\Exception\ShutdownException;
use PHPFastCGI\FastCGIDaemon\KernelInterface;

Expand Down Expand Up @@ -101,8 +102,12 @@ private function processConnectionPool()
$this->connectionHandlers[$id] = $this->connectionHandlerFactory->createConnectionHandler($this->kernel, $connection);
}

$dispatchedRequests = $this->connectionHandlers[$id]->ready();
$this->incrementRequestCount($dispatchedRequests);
try {
$dispatchedRequests = $this->connectionHandlers[$id]->ready();
$this->incrementRequestCount($dispatchedRequests);
} catch (UserlandDaemonException $exception) {
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->error($exception->getMessage());
}

if ($this->connectionHandlers[$id]->isClosed()) {
unset($this->connectionHandlers[$id]);
Expand Down
24 changes: 24 additions & 0 deletions test/Driver/Userland/UserlandDaemonTest.php
Expand Up @@ -5,6 +5,7 @@
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\StreamSocketConnectionPool;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactory;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException;
use PHPFastCGI\FastCGIDaemon\Driver\Userland\UserlandDaemon;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use PHPFastCGI\Test\FastCGIDaemon\Helper\Client\ConnectionWrapper;
Expand Down Expand Up @@ -89,6 +90,27 @@ public function testException()
$this->assertContains('boo', $context['logger']->getMessages()[0]['message']);
}

/**
* Tests that the daemon cleanly handles internal exceptions (such as
* protocol and connection exceptions).
*/
public function testDaemonException()
{
$context = $this->createTestingContext();

$socket1 = stream_socket_client($context['address']);
$connectionWrapper1 = new ConnectionWrapper($socket1);
$connectionWrapper1->writeRequest(1, ['DAEMON_EXCEPTION' => 'boo'], '');

$socket2 = stream_socket_client($context['address']);
$connectionWrapper2 = new ConnectionWrapper($socket2);
$connectionWrapper2->writeRequest(2, ['SHUTDOWN' => true], '');

$context['daemon']->run();

$this->assertContains('boo', $context['logger']->getMessages()[0]['message']);
}

/**
* Tests that the daemon shuts down after receiving a SIGINT.
*/
Expand All @@ -115,6 +137,8 @@ private function createTestingContext($requestLimit = DaemonOptions::NO_LIMIT, $

if (isset($params['EXCEPTION'])) {
throw new \Exception($params['EXCEPTION']);
} elseif (isset($params['DAEMON_EXCEPTION'])) {
throw new UserlandDaemonException($params['DAEMON_EXCEPTION']);
} elseif (isset($params['SHUTDOWN'])) {
posix_kill(posix_getpid(), SIGINT);
}
Expand Down

0 comments on commit 510eb1f

Please sign in to comment.