commit f5d387df7e7fb8de1a5dd0739ddb83a8b19fe64b
parent e9fbb331585488199fb3ed049f48acda672dc6aa
Author: Alexander Irion <alexander_irion@mentor.com>
Date: Wed, 11 Aug 2021 17:13:12 +0200
Fix busy waiting up to one second using connection timeout
MHD_daemon_get_timeout computes the time to wait as:
timeout = (last_activity + connection_timeout - MHD_monotonic_sec_counter ()) * 1000
After sleeping timeout milliseconds, MHD_monotonic_sec_counter () will be:
MHD_monotonic_sec_counter () = last_activity + connection_timeout
With that (timeout < (MHD_monotonic_sec_counter () - connection->last_activity)) evaluates to false and the MHD_connection_close_ is not done.
For the next cycle, MHD_daemon_get_timeout() will return 0 and this goes on, until MHD_monotonic_sec_counter jumps to the next second.
This bug causes thousands of unnecessary calls to MHD_run, when a connection timed out.
Diffstat:
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/lib/connection_call_handlers.c b/src/lib/connection_call_handlers.c
@@ -3582,8 +3582,8 @@ MHD_request_handle_idle_ (struct MHD_Request *request)
time_t timeout;
timeout = connection->connection_timeout;
if ( (0 != timeout) &&
- (timeout < (MHD_monotonic_sec_counter ()
- - connection->last_activity)) )
+ (timeout <= (MHD_monotonic_sec_counter ()
+ - connection->last_activity)) )
{
MHD_connection_close_ (connection,
MHD_REQUEST_TERMINATED_TIMEOUT_REACHED);
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
@@ -4306,8 +4306,8 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
time_t timeout;
timeout = connection->connection_timeout;
if ( (0 != timeout) &&
- (timeout < (MHD_monotonic_sec_counter ()
- - connection->last_activity)) )
+ (timeout <= (MHD_monotonic_sec_counter ()
+ - connection->last_activity)) )
{
MHD_connection_close_ (connection,
MHD_REQUEST_TERMINATED_TIMEOUT_REACHED);