Fix socket leak when there are too many open fds

CL: Mongoose Web Server: Fix socket leak when there are too many open file descriptors

Fixes https://github.com/cesanta/mongoose/issues/870

PUBLISHED_FROM=c802b6834a54eca37821d46efde192c527e0a6b7
This commit is contained in:
Dmitry Frank 2018-01-30 14:25:42 +02:00 committed by Cesanta Bot
parent 5d5badfcee
commit 255da78bed

View File

@ -3914,10 +3914,16 @@ time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
/* A hack to make sure all our file descriptos fit into FD_SETSIZE. */
if (nc->sock >= (sock_t) FD_SETSIZE && try_dup) {
int new_sock = dup(nc->sock);
if (new_sock >= 0 && new_sock < (sock_t) FD_SETSIZE) {
if (new_sock >= 0) {
if (new_sock < (sock_t) FD_SETSIZE) {
closesocket(nc->sock);
DBG(("new sock %d -> %d", nc->sock, new_sock));
nc->sock = new_sock;
} else {
closesocket(new_sock);
DBG(("new sock is still larger than FD_SETSIZE, disregard"));
try_dup = 0;
}
} else {
try_dup = 0;
}