View Issue Details

IDProjectCategoryView StatusLast Update
0004641libmicrohttpdotherpublic2016-10-17 17:21
ReporterHawk Assigned ToChristian Grothoff Due Date
PriorityhighSeveritymajorReproducibilityalways
Status closedResolutionfixed 
PlatformMINGWOSWINDOWSOS Version10 PRO
Product Version0.9.51 
Target Version0.9.52Fixed in Version0.9.52 
Summary0004641: libmicrohttpd 0.9.51 use 100% of the cpu
DescriptionHello,

If I use libmicrohttpd with MHD_USE_THREAD_PER_CONNECTION and MHD_USE_POLL, after a while libmicrohttpd use 100% of the cpu
Additional InformationIf I add :
printf("HERE WITH REVENT=%04x\n", p[1].revents);

after MHD_sys_poll_ in MHD_handle_connection (daemon.c file), my sample application display :

HERE WITH REVENT=0100
HERE WITH REVENT=0100
HERE WITH REVENT=0100
...


The pipe seems to be never read and MHD_sys_poll return immediately..

if I modify :

          /* drain signaling pipe before other processing */
          if ( (MHD_INVALID_PIPE_ != spipe) &&
               (0 != (p[1].revents & (POLLERR | POLLHUP))) )
            MHD_pipe_drain_ (spipe);

to

          /* drain signaling pipe before other processing */
          if ( (MHD_INVALID_PIPE_ != spipe) &&
               (0 != (p[1].revents & (POLLERR | POLLHUP | POLLIN))) )
            MHD_pipe_drain_ (spipe);

libmicrohttd use 100% cpu only after a period of inactivity. I a open a new connection libmicrohttpd use again 0% cpu time (with this patch).

I have no issue with libmicrohttpd 0.9.50
TagsNo tags attached.

Activities

Christian Grothoff

2016-09-06 21:01

manager   ~0011093

The patch certainly looks right to me (committed as SVN 37898), but I don't understand what is going on if you say that you do see 100% CPU even with the patch. Could you give use some hint where the code is stuck in a loop, or provide a test? (I don't use W32, so Evgeny is the right person to look into this.)

Hawk

2016-09-07 10:34

reporter   ~0011097

I found the problem :

libmicrohttpd 0.9.51 has add :

  /* if we were at the connection limit before and are in
     thread-per-connection mode, signal the main thread
     to resume accepting connections */
  if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
       (MHD_INVALID_PIPE_ != daemon->wpipe[1]) &&
       (1 != MHD_pipe_write_ (daemon->wpipe[1], "c", 1)) )
    {
#ifdef HAVE_MESSAGES
      MHD_DLOG (daemon,
                "failed to signal end of connection via pipe");
#endif
    }

in MHD_connection_close_ function (src/microhttpd/connection.c)


To correct "100% CPU usage" you must also commit this patch :

-------------------------------------------------------------------

--- ../libmicrohttpd-0.9.51/src/microhttpd/daemon.c_ori 2016-09-07 13:27:11.000000000 +0200
+++ ../libmicrohttpd-0.9.51/src/microhttpd/daemon.c 2016-09-07 13:26:45.000000000 +0200
@@ -2511,9 +2511,11 @@
   int timeout;
   unsigned int poll_count;
   int poll_listen;
+ int poll_pipe;
   memset (&p, 0, sizeof (p));
   poll_count = 0;
   poll_listen = -1;
+ poll_pipe = -1;
   if (MHD_INVALID_SOCKET != daemon->socket_fd)
     {
       p[poll_count].fd = daemon->socket_fd;
@@ -2527,6 +2529,7 @@
       p[poll_count].fd = daemon->wpipe[0];
       p[poll_count].events = POLLIN;
       p[poll_count].revents = 0;
+ poll_pipe = poll_count;
       poll_count++;
     }
   if (MHD_NO == may_block)
@@ -2547,6 +2550,11 @@
 #endif
       return MHD_NO;
     }
+
+ if ( (-1 != poll_pipe) &&
+ (0 != (p[poll_pipe].revents & POLLIN)) )
+ MHD_pipe_drain_ (daemon->wpipe[0]);
+
   /* handle shutdown */
   if (MHD_YES == daemon->shutdown)
     return MHD_NO;


-------------------------------------------------------------------

Hawk

2016-09-07 10:37

reporter   ~0011098

the second patch probably affect also Linux

Christian Grothoff

2016-09-07 11:22

manager   ~0011099

Fixed as suggested in SVN 37903.

Issue History

Date ModifiedUsernameFieldChange
2016-09-05 13:41HawkNew Issue
2016-09-06 21:01Christian GrothoffNote Added: 0011093
2016-09-06 21:01Christian GrothoffAssigned To => Karlson2k
2016-09-06 21:01Christian GrothoffStatusnew => assigned
2016-09-07 10:34HawkNote Added: 0011097
2016-09-07 10:37HawkNote Added: 0011098
2016-09-07 11:22Christian GrothoffNote Added: 0011099
2016-09-07 11:22Christian GrothoffStatusassigned => resolved
2016-09-07 11:22Christian GrothoffFixed in Version => 0.9.52
2016-09-07 11:22Christian GrothoffResolutionopen => fixed
2016-09-07 11:22Christian GrothoffAssigned ToKarlson2k => Christian Grothoff
2016-09-07 11:22Christian GrothoffTarget Version => 0.9.52
2016-10-17 17:21Christian GrothoffStatusresolved => closed