From 4978ea7e331989d7b1fe5c71b9ef94976d54adf4 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Sat, 11 May 2024 17:49:34 +0200 Subject: [PATCH 1/2] Prevent TcpTransport initialization errors from stopping TcpServer --- src/impl/tcpserver.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/impl/tcpserver.cpp b/src/impl/tcpserver.cpp index 25df61dcb..7e5777815 100644 --- a/src/impl/tcpserver.cpp +++ b/src/impl/tcpserver.cpp @@ -66,8 +66,12 @@ shared_ptr TcpServer::accept() { socket_t incomingSock = ::accept(mSock, (struct sockaddr *)&addr, &addrlen); if (incomingSock != INVALID_SOCKET) { - return std::make_shared(incomingSock, nullptr); // no state callback - + try { + return std::make_shared(incomingSock, nullptr); // no state callback + } catch(const std::exception &e) { + PLOG_WARNING << e.what(); + ::closesocket(incomingSock); + } } else if (sockerrno != SEAGAIN && sockerrno != SEWOULDBLOCK) { PLOG_ERROR << "TCP server failed, errno=" << sockerrno; throw std::runtime_error("TCP server failed"); From c1985b63d19f330dc4c662c4b1ffd3657158fa5b Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Sat, 11 May 2024 17:51:09 +0200 Subject: [PATCH 2/2] Fix getsockname instead of getpeername in exception message --- src/impl/tcptransport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/tcptransport.cpp b/src/impl/tcptransport.cpp index b3af2c209..a83fda958 100644 --- a/src/impl/tcptransport.cpp +++ b/src/impl/tcptransport.cpp @@ -69,7 +69,7 @@ TcpTransport::TcpTransport(socket_t sock, state_callback callback) struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); if (::getpeername(mSock, reinterpret_cast(&addr), &addrlen) < 0) - throw std::runtime_error("getsockname failed"); + throw std::runtime_error("getpeername failed"); unmap_inet6_v4mapped(reinterpret_cast(&addr), &addrlen);