From 42143b138fc7fd8621dcfc8f2cc69276a1fe9bfe Mon Sep 17 00:00:00 2001 From: "jacob.huang" Date: Mon, 29 Nov 2021 14:24:24 +0800 Subject: [PATCH] fix memory leak/high cpu use on fd exhaustion if the maximum numbers of open file descriptors (often 1024, see ulimit -n) is exhausted, accept() fails and we end up leaking memory. in this fix, whenever failed to service client properly, wait for 1 ms before accepting new connection. --- sockssrv.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sockssrv.c b/sockssrv.c index 98e4622..c81347b 100644 --- a/sockssrv.c +++ b/sockssrv.c @@ -439,7 +439,12 @@ int main(int argc, char** argv) { struct thread *curr = malloc(sizeof (struct thread)); if(!curr) goto oom; curr->done = 0; - if(server_waitclient(&s, &c)) continue; + if(server_waitclient(&s, &c)) { + dolog("failed to accept connection\n"); + free(curr); + usleep(1000); + continue; + } curr->client = c; if(!sblist_add(threads, &curr)) { close(curr->client.fd); -- 2.11.4.GIT