Skip to content

Commit 510eb1f

Browse files
author
Andrew Carter
committed
Improved handling of internal errors on userland daemon
1 parent 01ec237 commit 510eb1f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Driver/Userland/UserlandDaemon.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPFastCGI\FastCGIDaemon\DaemonTrait;
88
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\ConnectionPoolInterface;
99
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactoryInterface;
10+
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException;
1011
use PHPFastCGI\FastCGIDaemon\Exception\ShutdownException;
1112
use PHPFastCGI\FastCGIDaemon\KernelInterface;
1213

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

104-
$dispatchedRequests = $this->connectionHandlers[$id]->ready();
105-
$this->incrementRequestCount($dispatchedRequests);
105+
try {
106+
$dispatchedRequests = $this->connectionHandlers[$id]->ready();
107+
$this->incrementRequestCount($dispatchedRequests);
108+
} catch (UserlandDaemonException $exception) {
109+
$this->daemonOptions->getOption(DaemonOptions::LOGGER)->error($exception->getMessage());
110+
}
106111

107112
if ($this->connectionHandlers[$id]->isClosed()) {
108113
unset($this->connectionHandlers[$id]);

test/Driver/Userland/UserlandDaemonTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
66
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\StreamSocketConnectionPool;
77
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactory;
8+
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Exception\UserlandDaemonException;
89
use PHPFastCGI\FastCGIDaemon\Driver\Userland\UserlandDaemon;
910
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
1011
use PHPFastCGI\Test\FastCGIDaemon\Helper\Client\ConnectionWrapper;
@@ -89,6 +90,27 @@ public function testException()
8990
$this->assertContains('boo', $context['logger']->getMessages()[0]['message']);
9091
}
9192

93+
/**
94+
* Tests that the daemon cleanly handles internal exceptions (such as
95+
* protocol and connection exceptions).
96+
*/
97+
public function testDaemonException()
98+
{
99+
$context = $this->createTestingContext();
100+
101+
$socket1 = stream_socket_client($context['address']);
102+
$connectionWrapper1 = new ConnectionWrapper($socket1);
103+
$connectionWrapper1->writeRequest(1, ['DAEMON_EXCEPTION' => 'boo'], '');
104+
105+
$socket2 = stream_socket_client($context['address']);
106+
$connectionWrapper2 = new ConnectionWrapper($socket2);
107+
$connectionWrapper2->writeRequest(2, ['SHUTDOWN' => true], '');
108+
109+
$context['daemon']->run();
110+
111+
$this->assertContains('boo', $context['logger']->getMessages()[0]['message']);
112+
}
113+
92114
/**
93115
* Tests that the daemon shuts down after receiving a SIGINT.
94116
*/
@@ -115,6 +137,8 @@ private function createTestingContext($requestLimit = DaemonOptions::NO_LIMIT, $
115137

116138
if (isset($params['EXCEPTION'])) {
117139
throw new \Exception($params['EXCEPTION']);
140+
} elseif (isset($params['DAEMON_EXCEPTION'])) {
141+
throw new UserlandDaemonException($params['DAEMON_EXCEPTION']);
118142
} elseif (isset($params['SHUTDOWN'])) {
119143
posix_kill(posix_getpid(), SIGINT);
120144
}

0 commit comments

Comments
 (0)