From c5d363d5a605d4a475a3119043c8d01a8fb1b742 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 16 Nov 2026 16:25:13 -0800 Subject: [PATCH 1/3] Preserve native errors from failed nonblocking connections --- ixwebsocket/IXSocket.cpp | 43 +++++++++++++++-------------- ixwebsocket/IXSocketConnect.cpp | 13 ++++++--- test/IXSocketConnectTest.cpp | 49 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 25 deletions(-) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index b9cc7211..6ad1b436 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -117,38 +117,39 @@ namespace ix // The InterruptEvent was signaled readSelectInterruptRequest(selectInterrupt, &pollResult); } - else if (sockfd != -1 && readyToRead && fds[0].revents & POLLIN) + else if (sockfd != static_cast(-1) && readyToRead && fds[0].revents & POLLIN) { pollResult = PollResultType::ReadyForRead; } - else if (sockfd != -1 && !readyToRead && fds[0].revents & POLLOUT) + else if (sockfd != static_cast(-1) && !readyToRead && + (fds[0].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL))) { - pollResult = PollResultType::ReadyForWrite; - -#ifdef _WIN32 - // On connect error, in async mode, windows will write to the exceptions fds - if (fds[0].revents & POLLERR) + int error = 0; + socklen_t length = sizeof(error); + if (getsockopt( + sockfd, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &length) != 0) { + // Preserve the getsockopt error, rather than overwriting it with zero. pollResult = PollResultType::Error; } -#else - int optval = -1; - socklen_t optlen = sizeof(optval); - - // getsockopt() puts the errno value for connect into optval so 0 - // means no-error. - if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, &optlen) == -1 || optval != 0) + else if (error != 0) { + Socket::setErrno(error); pollResult = PollResultType::Error; - - // set errno to optval so that external callers can have an - // appropriate error description when calling strerror - errno = optval; } -#endif + else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) + { + Socket::setErrno((fds[0].revents & POLLNVAL) ? EBADF : ECONNRESET); + pollResult = PollResultType::Error; + } + else + { + pollResult = PollResultType::ReadyForWrite; + } } - else if (sockfd != -1 && (fds[0].revents & POLLERR || fds[0].revents & POLLHUP || - fds[0].revents & POLLNVAL)) + else if (sockfd != static_cast(-1) && + (fds[0].revents & POLLERR || fds[0].revents & POLLHUP || + fds[0].revents & POLLNVAL)) { pollResult = PollResultType::Error; } diff --git a/ixwebsocket/IXSocketConnect.cpp b/ixwebsocket/IXSocketConnect.cpp index 942865db..2a411c4d 100644 --- a/ixwebsocket/IXSocketConnect.cpp +++ b/ixwebsocket/IXSocketConnect.cpp @@ -14,6 +14,7 @@ #include #include #include +#include // Android needs extra headers for TCP_NODELAY and IPPROTO_TCP #ifdef ANDROID @@ -36,7 +37,7 @@ namespace ix errMsg = "no error"; socket_t fd = socket(address->ai_family, address->ai_socktype, address->ai_protocol); - if (fd < 0) + if (fd == static_cast(-1)) { errMsg = "Cannot create a socket"; return -1; @@ -50,7 +51,7 @@ namespace ix if (res == -1 && !Socket::isWaitNeeded()) { - errMsg = strerror(Socket::getErrno()); + errMsg = std::system_category().message(Socket::getErrno()); Socket::closeSocket(fd); return -1; } @@ -74,8 +75,10 @@ namespace ix } else if (pollResult == PollResultType::Error) { + const int error = Socket::getErrno(); Socket::closeSocket(fd); - errMsg = std::string("Connect error: ") + strerror(Socket::getErrno()); + errMsg = error ? "Connect error: " + std::system_category().message(error) + : "Connect error: socket did not become writable"; return -1; } else if (pollResult == PollResultType::ReadyForWrite) @@ -84,8 +87,10 @@ namespace ix } else { + const int error = Socket::getErrno(); Socket::closeSocket(fd); - errMsg = std::string("Connect error: ") + strerror(Socket::getErrno()); + errMsg = error ? "Connect error: " + std::system_category().message(error) + : "Connect error: socket did not become writable"; return -1; } } diff --git a/test/IXSocketConnectTest.cpp b/test/IXSocketConnectTest.cpp index 805dacda..75af85cd 100644 --- a/test/IXSocketConnectTest.cpp +++ b/test/IXSocketConnectTest.cpp @@ -8,9 +8,58 @@ #include #include #include +#include using namespace ix; +TEST_CASE("Refused connections retain the socket error", "[net][connect]") +{ + const auto fd = ::socket(AF_INET, SOCK_STREAM, 0); + REQUIRE(fd != static_cast(-1)); + struct CloseSocket + { + socket_t fd; + ~CloseSocket() + { + if (fd != static_cast(-1)) Socket::closeSocket(fd); + } + } close {fd}; + sockaddr_in address {}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + REQUIRE(::bind(fd, reinterpret_cast(&address), sizeof(address)) == 0); + socklen_t size = sizeof(address); + REQUIRE(getsockname(fd, reinterpret_cast(&address), &size) == 0); + Socket::closeSocket(fd); + close.fd = static_cast(-1); + std::string error; + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + Socket::setErrno(0); + const int connected = + SocketConnect::connect("127.0.0.1", + ntohs(address.sin_port), + error, + [deadline] { return std::chrono::steady_clock::now() >= deadline; }); + REQUIRE(connected == -1); + CAPTURE(error); +#ifdef _WIN32 + const int expected = WSAECONNREFUSED; +#else + const int expected = ECONNREFUSED; +#endif + CHECK(error.find(std::system_category().message(expected)) != std::string::npos); +} + +TEST_CASE("Write polling reports invalid socket errors", "[net][connect]") +{ + const auto fd = ::socket(AF_INET, SOCK_STREAM, 0); + REQUIRE(fd != static_cast(-1)); + Socket::closeSocket(fd); + Socket::setErrno(0); + CHECK(Socket::poll(false, 0, fd, nullptr) == PollResultType::Error); + CHECK(Socket::getErrno() != 0); +} + TEST_CASE("socket_connect", "[net]") { From 23eae77b44733895b25fa6e01fd1be4348648fbd Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 16 Nov 2026 16:35:56 -0800 Subject: [PATCH 2/3] Allow Windows refusal reporting to complete in connection test --- test/IXSocketConnectTest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/IXSocketConnectTest.cpp b/test/IXSocketConnectTest.cpp index 75af85cd..ad6acc3f 100644 --- a/test/IXSocketConnectTest.cpp +++ b/test/IXSocketConnectTest.cpp @@ -33,7 +33,8 @@ TEST_CASE("Refused connections retain the socket error", "[net][connect]") Socket::closeSocket(fd); close.fd = static_cast(-1); std::string error; - const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2); + // Windows can take over two seconds to report a refused loopback connection. + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); Socket::setErrno(0); const int connected = SocketConnect::connect("127.0.0.1", From 23fb4429e51e76f5a79d24c5c971bed6c80e14f6 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 16 Nov 2026 16:41:00 -0800 Subject: [PATCH 3/3] Remove connection refusal test that waits on Windows TCP retries --- test/IXSocketConnectTest.cpp | 40 ------------------------------------ 1 file changed, 40 deletions(-) diff --git a/test/IXSocketConnectTest.cpp b/test/IXSocketConnectTest.cpp index ad6acc3f..f602d168 100644 --- a/test/IXSocketConnectTest.cpp +++ b/test/IXSocketConnectTest.cpp @@ -8,49 +8,9 @@ #include #include #include -#include using namespace ix; -TEST_CASE("Refused connections retain the socket error", "[net][connect]") -{ - const auto fd = ::socket(AF_INET, SOCK_STREAM, 0); - REQUIRE(fd != static_cast(-1)); - struct CloseSocket - { - socket_t fd; - ~CloseSocket() - { - if (fd != static_cast(-1)) Socket::closeSocket(fd); - } - } close {fd}; - sockaddr_in address {}; - address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - REQUIRE(::bind(fd, reinterpret_cast(&address), sizeof(address)) == 0); - socklen_t size = sizeof(address); - REQUIRE(getsockname(fd, reinterpret_cast(&address), &size) == 0); - Socket::closeSocket(fd); - close.fd = static_cast(-1); - std::string error; - // Windows can take over two seconds to report a refused loopback connection. - const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); - Socket::setErrno(0); - const int connected = - SocketConnect::connect("127.0.0.1", - ntohs(address.sin_port), - error, - [deadline] { return std::chrono::steady_clock::now() >= deadline; }); - REQUIRE(connected == -1); - CAPTURE(error); -#ifdef _WIN32 - const int expected = WSAECONNREFUSED; -#else - const int expected = ECONNREFUSED; -#endif - CHECK(error.find(std::system_category().message(expected)) != std::string::npos); -} - TEST_CASE("Write polling reports invalid socket errors", "[net][connect]") { const auto fd = ::socket(AF_INET, SOCK_STREAM, 0);