Skip to content

Improve error messages for failed connections to include errno #271

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 7 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Look up errno based on errstr when listening for connections fails
  • Loading branch information
clue committed Sep 6, 2021
commit 90d1e0b85bd8bc8659eb45e3e9421ece82803094
45 changes: 30 additions & 15 deletions src/SocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,45 @@ public static function accept($socket)
// 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;
$errstr .= ' (' . \substr($name, 7) . ')';
break;
}
}
}
// @codeCoverageIgnoreEnd
$errno = self::errno($errstr);

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

return $newSocket;
}

/**
* [Internal] Returns errno value for given errstr
*
* The errno and errstr values describes the type of error that has been
* encountered. This method tries to look up the given errstr and find a
* matching errno value which can be useful to provide more context to error
* messages. It goes through the list of known errno constants when
* ext-sockets is available to find an errno matching the given errstr.
*
* @param string $errstr
* @return int errno value (e.g. value of `SOCKET_ECONNREFUSED`) or 0 if not found
* @internal
* @copyright Copyright (c) 2018 Christian Lück, taken from https://github.com/clue/errno with permission
* @codeCoverageIgnore
*/
public static function errno($errstr)
{
if (\function_exists('socket_strerror')) {
foreach (\get_defined_constants(false) as $name => $value) {
if (\strpos($name, 'SOCKET_E') === 0 && \socket_strerror($value) === $errstr) {
return $value;
}
}
}

return 0;
}

/**
* [Internal] Returns errno constant name for given errno value
*
Expand Down
6 changes: 6 additions & 0 deletions src/TcpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a
\stream_context_create(array('socket' => $context + array('backlog' => 511)))
);
if (false === $this->master) {
if ($errno === 0) {
// PHP does not seem to report errno, so match errno from errstr
// @link https://3v4l.org/3qOBl
$errno = SocketServer::errno($errstr);
}

throw new \RuntimeException(
'Failed to listen on "' . $uri . '": ' . $errstr . SocketServer::errconst($errno),
$errno
Expand Down
20 changes: 16 additions & 4 deletions tests/TcpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
public function testServerEmitsConnectionEventForNewConnection()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
assert($client !== false);

$server = $this->server;
$promise = new Promise(function ($resolve) use ($server) {
Expand All @@ -70,6 +71,7 @@ public function testConnectionWithManyClients()
$client1 = stream_socket_client('tcp://localhost:'.$this->port);
$client2 = stream_socket_client('tcp://localhost:'.$this->port);
$client3 = stream_socket_client('tcp://localhost:'.$this->port);
assert($client1 !== false && $client2 !== false && $client3 !== false);

$this->server->on('connection', $this->expectCallableExactly(3));
$this->tick();
Expand All @@ -80,6 +82,7 @@ public function testConnectionWithManyClients()
public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
assert($client !== false);

$mock = $this->expectCallableNever();

Expand Down Expand Up @@ -150,6 +153,7 @@ public function testGetAddressAfterCloseReturnsNull()
public function testLoopWillEndWhenServerIsClosedAfterSingleConnection()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);
assert($client !== false);

// explicitly unset server because we only accept a single connection
// and then already call close()
Expand Down Expand Up @@ -203,6 +207,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo
public function testConnectionDoesNotEndWhenClientDoesNotClose()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);
assert($client !== false);

$mock = $this->expectCallableNever();

Expand Down Expand Up @@ -236,7 +241,7 @@ public function testCtorAddsResourceToLoop()
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
$loop->expects($this->once())->method('addReadStream');

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

public function testResumeWithoutPauseIsNoOp()
Expand Down Expand Up @@ -316,7 +321,7 @@ public function testEmitsErrorWhenAcceptListenerFails()
public function testEmitsTimeoutErrorWhenAcceptListenerFails(\RuntimeException $exception)
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('not supported on HHVM');
$this->markTestSkipped('Not supported on HHVM');
}

$this->assertEquals('Unable to accept new connection: ' . socket_strerror(SOCKET_ETIMEDOUT) . ' (ETIMEDOUT)', $exception->getMessage());
Expand All @@ -328,9 +333,16 @@ public function testListenOnBusyPortThrows()
if (DIRECTORY_SEPARATOR === '\\') {
$this->markTestSkipped('Windows supports listening on same port multiple times');
}
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on HHVM');
}

$this->setExpectedException('RuntimeException');
$another = new TcpServer($this->port, $this->loop);
$this->setExpectedException(
'RuntimeException',
'Failed to listen on "tcp://127.0.0.1:' . $this->port . '": ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EADDRINUSE) . ' (EADDRINUSE)' : 'Address already in use'),
defined('SOCKET_EADDRINUSE') ? SOCKET_EADDRINUSE : 0
);
new TcpServer($this->port, $this->loop);
}

/**
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy