Skip to content

Improve error messages when accepting connections with errno/errstr #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,44 @@ public function close()
{
$this->server->close();
}

/**
* [internal] Internal helper method to accept new connection from given server socket
*
* @param resource $socket server socket to accept connection from
* @return resource new client socket if any
* @throws \RuntimeException if accepting fails
* @internal
*/
public static function accept($socket)
{
$newSocket = @\stream_socket_accept($socket, 0);

if (false === $newSocket) {
// Match errstr from PHP's warning message.
// stream_socket_accept(): accept failed: Connection timed out
$error = \error_get_last();
$errstr = \preg_replace('#.*: #', '', $error['message']);

// Go through list of possible error constants to find matching errno.
// @codeCoverageIgnoreStart
$errno = 0;
if (\function_exists('socket_strerror')) {
foreach (\get_defined_constants(false) as $name => $value) {
if (\strpos($name, 'SOCKET_E') === 0 && \socket_strerror($value) === $errstr) {
$errno = $value;
break;
}
}
}
// @codeCoverageIgnoreEnd

throw new \RuntimeException(
'Unable to accept new connection: ' . $errstr,
$errno
);
}

return $newSocket;
}
}
8 changes: 4 additions & 4 deletions src/TcpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ public function resume()

$that = $this;
$this->loop->addReadStream($this->master, function ($master) use ($that) {
$newSocket = @\stream_socket_accept($master, 0);
if (false === $newSocket) {
$that->emit('error', array(new \RuntimeException('Error accepting new connection')));

try {
$newSocket = SocketServer::accept($master);
} catch (\RuntimeException $e) {
$that->emit('error', array($e));
return;
}
$that->handleConnection($newSocket);
Expand Down
8 changes: 4 additions & 4 deletions src/UnixServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function resume()

$that = $this;
$this->loop->addReadStream($this->master, function ($master) use ($that) {
$newSocket = @\stream_socket_accept($master, 0);
if (false === $newSocket) {
$that->emit('error', array(new \RuntimeException('Error accepting new connection')));

try {
$newSocket = SocketServer::accept($master);
} catch (\RuntimeException $e) {
$that->emit('error', array($e));
return;
}
$that->handleConnection($newSocket);
Expand Down
22 changes: 21 additions & 1 deletion tests/TcpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ public function testEmitsErrorWhenAcceptListenerFails()

$server = new TcpServer(0, $loop);

$server->on('error', $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));
$exception = null;
$server->on('error', function ($e) use (&$exception) {
$exception = $e;
});

$this->assertNotNull($listener);
$socket = stream_socket_server('tcp://127.0.0.1:0');
Expand All @@ -297,6 +300,23 @@ public function testEmitsErrorWhenAcceptListenerFails()
$time = microtime(true) - $time;

$this->assertLessThan(1, $time);

$this->assertInstanceOf('RuntimeException', $exception);
assert($exception instanceof \RuntimeException);
$this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage());

return $exception;
}

/**
* @param \RuntimeException $e
* @requires extension sockets
* @depends testEmitsErrorWhenAcceptListenerFails
*/
public function testEmitsTimeoutErrorWhenAcceptListenerFails(\RuntimeException $exception)
{
$this->assertEquals('Unable to accept new connection: ' . socket_strerror(SOCKET_ETIMEDOUT), $exception->getMessage());
$this->assertEquals(SOCKET_ETIMEDOUT, $exception->getCode());
}

public function testListenOnBusyPortThrows()
Expand Down
22 changes: 21 additions & 1 deletion tests/UnixServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ public function testEmitsErrorWhenAcceptListenerFails()

$server = new UnixServer($this->getRandomSocketUri(), $loop);

$server->on('error', $this->expectCallableOnceWith($this->isInstanceOf('RuntimeException')));
$exception = null;
$server->on('error', function ($e) use (&$exception) {
$exception = $e;
});

$this->assertNotNull($listener);
$socket = stream_socket_server('tcp://127.0.0.1:0');
Expand All @@ -302,6 +305,23 @@ public function testEmitsErrorWhenAcceptListenerFails()
$time = microtime(true) - $time;

$this->assertLessThan(1, $time);

$this->assertInstanceOf('RuntimeException', $exception);
assert($exception instanceof \RuntimeException);
$this->assertStringStartsWith('Unable to accept new connection: ', $exception->getMessage());

return $exception;
}

/**
* @param \RuntimeException $e
* @requires extension sockets
* @depends testEmitsErrorWhenAcceptListenerFails
*/
public function testEmitsTimeoutErrorWhenAcceptListenerFails(\RuntimeException $exception)
{
$this->assertEquals('Unable to accept new connection: ' . socket_strerror(SOCKET_ETIMEDOUT), $exception->getMessage());
$this->assertEquals(SOCKET_ETIMEDOUT, $exception->getCode());
}

public function testListenOnBusyPortThrows()
Expand Down