From 224119a60ab81cec41e21d35b91d3d54f4136747 Mon Sep 17 00:00:00 2001 From: Alin Gherman Date: Mon, 30 Sep 2019 11:48:02 +0200 Subject: [PATCH] Retry in case of too many sockets opened instead of stopping the server. --- httplib.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/httplib.h b/httplib.h index b3441e7..e640670 100644 --- a/httplib.h +++ b/httplib.h @@ -98,6 +98,7 @@ typedef int socket_t; #include #include #include +#include #include #include #include @@ -2550,6 +2551,12 @@ inline bool Server::listen_internal() { socket_t sock = accept(svr_sock_, nullptr, nullptr); if (sock == INVALID_SOCKET) { + if (errno == EMFILE) { + // The per-process limit of open file descriptors has been reached. + // Try to accept new connections after a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + continue; + } if (svr_sock_ != INVALID_SOCKET) { detail::close_socket(svr_sock_); ret = false;