From fb739dbaecf38c6b5e4b794d10f4c4edb83ea52c Mon Sep 17 00:00:00 2001 From: Karen Dombroski Date: Wed, 1 May 2024 21:43:38 +1200 Subject: [PATCH] threadsafe accept on windows, linux * Windows has WSAAccept() which will create sockets inheriting flags from the server socket * Linux has accept4() which has a flags argument supporting SOCK_CLOEXEC --- httplib.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/httplib.h b/httplib.h index 5e2eff2..f1b7891 100644 --- a/httplib.h +++ b/httplib.h @@ -6484,7 +6484,15 @@ inline bool Server::listen_internal() { #ifndef _WIN32 } #endif + +#if defined _WIN32 + // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, OVERLAPPED + socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); +#elif defined __linux__ + socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); +#else socket_t sock = accept(svr_sock_, nullptr, nullptr); +#endif if (sock == INVALID_SOCKET) { if (errno == EMFILE) {