From 59b30c5cbc098fbe8b102bcba798987a440cfd95 Mon Sep 17 00:00:00 2001 From: Tom Dryer Date: Sat, 26 Jun 2021 18:41:19 -0700 Subject: [PATCH] Fix high CPU usage when timeout is disabled (#8) When darkhttpd is running with `--timeout 0` (timeout disabled), and any connection is idle, it will use 100% of the CPU. This happens because `select` returns immediately when its timeout is zero, causing the main `httpd_poll` loop to spin. Fix this by adding a check to `httpd_poll` making `select` always receive a `NULL` timeout when `timeout_secs` is zero. --- darkhttpd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/darkhttpd.c b/darkhttpd.c index 021faab..3ae8ea5 100644 --- a/darkhttpd.c +++ b/darkhttpd.c @@ -2518,6 +2518,9 @@ static void httpd_poll(void) { #endif /* -select- */ + if (timeout_secs == 0) { + bother_with_timeout = 0; + } if (debug) { printf("select() with max_fd %d timeout %d\n", max_fd, bother_with_timeout ? (int)timeout.tv_sec : 0);