Hello, With MHD_USE_SELECT_INTERNALLY | MHD_USE_EPOLL, libmicrohttpd (1.0.2, 1.0.10, and master has the same code) stops serving when about 256 keep-alive connections are opened at once, e.g. `wrk -t8 -c256`. Every server socket then holds an unread request, while the daemon thread sleeps in epoll_wait() until the clients give up: about 300 req/s instead of ~165,000.
The cause is the drain loop in MHD_epoll(). It calls epoll_wait() again as long as a call returns a full batch (MAX_EVENTS, 128), and every call uses the full timeout. When a full batch was the last of the pending events, the next call blocks for the whole timeout. The connections are edge-triggered and each waits for its response, so nothing new can arrive in the meantime. 256 and 512 connections (multiples of 128) stall, and 257 as well (the listen socket's event makes it 256 plus one). 250, 300, 1000 and 2000 don't. The patch below (against master) keeps the first call's timeout and drains the rest with timeout 0. Measured with a minimal server (one handler that answers at once, pinned to one core, wrk -t8 -d5s, median of 3 runs): connections epoll (1.0.10) epoll patched poll 256 273 req/s 164,000 req/s, p99 2.0 ms 165,000, p99 2.6 ms 512 1,800 req/s 160,000, p99 4.7 ms 160,000, p99 240 ms 1000 151,000, p99 40 ms 145,000, p99 40 ms 152,000, p99 1.25 s 2000 151,000, p99 9 ms 142,000, p99 10 ms 147,000, p99 910 ms `make check` on the patched 1.0.10 (with libcurl, without HTTPS): 164/164 pass. The reproducer, the measurements and an strace of the stall are also in https://www.google.com/url?q=https://github.com/Karlson2k/libmicrohttpd/issues/43&source=gmail&ust=1795117484040000&sa=E (posted there before we noticed the mirror stops at 1.0.5). Thanks for libmicrohttpd, Ken Zangelin --- Subject: [PATCH] MHD_epoll: do not wait again after a full batch of events The drain loop in MHD_epoll() calls epoll_wait() again as long as one returns a full batch (MAX_EVENTS), and every call used the full timeout. When a full batch was the last of the pending events, the next call blocked for the whole timeout. With edge-triggered connections that each wait for their response nothing new can arrive meanwhile, so all connections stalled until the clients gave up: 256, 257 and 512 keep-alive connections opened at once gave 270-1800 req/s instead of ~165000. The first call waits as before; the following ones only drain what is pending. --- src/microhttpd/daemon.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -5865,6 +5865,11 @@ MHD_epoll (struct MHD_Daemon *daemon, events, MAX_EVENTS, timeout_ms); + /* A full batch may have been the last of the pending events: the next + call only drains what is left, it must not wait. With edge-triggered + connections nothing new arrives until they are processed, so waiting + here would stall every connection until the timeout. */ + timeout_ms = 0; if (-1 == num_events) { const int err = MHD_socket_get_error_ ();
From a5b97c8593e684b833b04c8ec03ebe31441ca162 Mon Sep 17 00:00:00 2001 From: kzangeli <[email protected]> Date: Wed, 18 Nov 2026 19:27:01 +0100 Subject: [PATCH] MHD_epoll: do not wait again after a full batch of events The drain loop in MHD_epoll() calls epoll_wait() again as long as one returns a full batch (MAX_EVENTS), and every call used the full timeout. When a full batch was the last of the pending events, the next call blocked for the whole timeout. With edge-triggered connections that each wait for their response nothing new can arrive meanwhile, so all connections stalled until the clients gave up: 256, 257 and 512 keep-alive connections opened at once gave 270-1800 req/s instead of ~165000. The first call waits as before; the following ones only drain what is pending. --- src/microhttpd/daemon.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index b42d4ee1..09db3bd0 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -5865,6 +5865,11 @@ MHD_epoll (struct MHD_Daemon *daemon, events, MAX_EVENTS, timeout_ms); + /* A full batch may have been the last of the pending events: the next + call only drains what is left, it must not wait. With edge-triggered + connections nothing new arrives until they are processed, so waiting + here would stall every connection until the timeout. */ + timeout_ms = 0; if (-1 == num_events) { const int err = MHD_socket_get_error_ (); -- 2.53.0
