View Issue Details

IDProjectCategoryView StatusLast Update
0001914libmicrohttpdotherpublic2011-11-20 14:48
Reporterrboulton Assigned ToChristian Grothoff  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
PlatformLinuxOSUbuntuOS Version11.04
Product Version0.9.16 
Target Version0.9.17Fixed in Version0.9.17 
Summary0001914: Busy wait when no connections have timeout set
DescriptionI have a server using external select and not setting any timeout for connections. I recently upgraded from a libmicrohttpd snapshot from April to libmicrohttpd-0.9.16. After this upgrade, I found that after a request has completed, but while the connection is still open, my server was using 100% CPU, and on inspection it was busy waiting, calling select with a timeout of 0. Previously, in this situation, it would use a NULL timeout to wait indefinitely.
Steps To ReproduceI'm afraid I don't currently have a standalone testcase.
Additional InformationBrowsing the repository (which I pulled into github for easier browsing), the change appears to have occurred in a commit on September 12, 2011 (revision 16764). Specifically, in https://github.com/rboulton/libmicrohttpd/commit/7523284e26f3e7bad4d042d5a11b7833049b9660#L4R1233

Before this change, MHD_get_timeout() would return MHD_NO if the daemon connection_timeout setting was 0 (which is documented to mean "no timeout"). Afterwards, even if all the connection timeouts are 0, MHD_get_timeout() returns MHD_YES.

I believe it would be appropriate to change this function to return MHD_NO if all connection timeouts are 0. I attach a patch which implements this.
TagsNo tags attached.
Attached Files
libmicrohttpd.patch (1,532 bytes)   
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 2b8eac5..47c7c52 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -1204,6 +1204,7 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
   time_t earliest_deadline;
   time_t now;
   struct MHD_Connection *pos;
+  int have_timeout = MHD_NO;
 
   if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
     {
@@ -1215,19 +1216,22 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
   pos = daemon->connections_head;
   if (pos == NULL)
     return MHD_NO;              /* no connections */
-  earliest_deadline = pos->last_activity + pos->connection_timeout;
-  pos = pos->next;
   while (pos != NULL)
     {
-      if (earliest_deadline > pos->last_activity + pos->connection_timeout)
-        earliest_deadline = pos->last_activity + pos->connection_timeout;
+      if (0 != pos->connection_timeout) {
+        have_timeout = MHD_YES;
+        if (earliest_deadline > pos->last_activity + pos->connection_timeout)
+          earliest_deadline = pos->last_activity + pos->connection_timeout;
 #if HTTPS_SUPPORT
-      if (  (0 != (daemon->options & MHD_USE_SSL)) &&
-	    (0 != gnutls_record_check_pending (pos->tls_session)) )
-	earliest_deadline = 0;
+        if (  (0 != (daemon->options & MHD_USE_SSL)) &&
+	      (0 != gnutls_record_check_pending (pos->tls_session)) )
+	  earliest_deadline = 0;
 #endif
+      }
       pos = pos->next;
     }
+  if (!have_timeout)
+    return MHD_NO;
   now = time (NULL);
   if (earliest_deadline < now)
     *timeout = 0;
libmicrohttpd.patch (1,532 bytes)   

Activities

Christian Grothoff

2011-11-18 20:18

manager   ~0004949

Fixed as suggested in SVN 18212.

Issue History

Date Modified Username Field Change
2011-11-18 18:00 rboulton New Issue
2011-11-18 18:00 rboulton File Added: patch
2011-11-18 19:34 rboulton File Added: libmicrohttpd.patch
2011-11-18 20:16 Christian Grothoff File Deleted: patch
2011-11-18 20:18 Christian Grothoff Note Added: 0004949
2011-11-18 20:18 Christian Grothoff Status new => resolved
2011-11-18 20:18 Christian Grothoff Fixed in Version => Git master
2011-11-18 20:18 Christian Grothoff Resolution open => fixed
2011-11-18 20:18 Christian Grothoff Assigned To => Christian Grothoff
2011-11-20 14:48 Christian Grothoff Status resolved => closed
2011-11-20 14:48 Christian Grothoff Fixed in Version Git master => 0.9.17
2011-11-20 14:48 Christian Grothoff Target Version => 0.9.17