[Nodogsplash] [PATCH-0.9_beta9.9.6 4/4] Remove threading for http access

Sven Eckelmann sven at open-mesh.com
Fri Nov 8 18:58:08 CET 2013


The complete design of nodogsplash is non-thread safe. So using threads for
http (everytime a user tries to access http site) is harmful and resulted in
many crashes in the past.

This change doesn't resolve the missing correct locking but avoids one of the
biggest problems. A big rewrite would be necessary to really fix it.

Signed-off-by: Sven Eckelmann <sven at open-mesh.com>
---
 src/gateway.c      | 31 +------------------------------
 src/httpd_thread.c | 23 +++++------------------
 src/httpd_thread.h |  2 +-
 3 files changed, 7 insertions(+), 49 deletions(-)

diff --git a/src/gateway.c b/src/gateway.c
index 3d6598c..3d2058e 100644
--- a/src/gateway.c
+++ b/src/gateway.c
@@ -323,36 +323,7 @@ main_loop(void) {
       termination_handler(0);
     }
     else if (r != NULL) {
-      /*
-       * We got a connection
-       *
-       * We create another thread to handle the request,
-       * possibly sleeping first if there are too many already
-       */
-      debug(LOG_DEBUG,"%d current httpd threads.", current_httpd_threads);
-      if(config->decongest_httpd_threads && current_httpd_threads >= config->httpd_thread_threshold) {
-	msec = current_httpd_threads * config->httpd_thread_delay_ms;
-	wait_time.tv_sec = msec / 1000;
-	wait_time.tv_nsec = (msec % 1000) * 1000000;
-	debug(LOG_INFO, "Httpd thread creation delayed %ld sec %ld nanosec for congestion.",
-	      wait_time.tv_sec, wait_time.tv_nsec);
-	nanosleep(&wait_time,NULL);
-      }
-      thread_serial_num_p = (int*) malloc(sizeof(int)); /* thread_httpd() must free */
-      *thread_serial_num_p = created_httpd_threads;
-      debug(LOG_INFO, "Creating httpd request thread %d for %s", *thread_serial_num_p, r->clientAddr);
-      /* The void**'s are a simulation of the normal C
-       * function calling sequence. */
-      params = safe_malloc(3 * sizeof(void *)); /* thread_httpd() must free */
-      *params = webserver;
-      *(params + 1) = r;
-      *(params + 2) = thread_serial_num_p;
-      created_httpd_threads++;
-      result = pthread_create(&tid, &thread_attr, (void *)thread_httpd, (void *)params);
-      if (result != 0) {
-	debug(LOG_ERR, "FATAL: pthread_create failed to create httpd request thread - exiting...");
-	termination_handler(0);
-      }
+      handle_http_request(webserver, r);
     }
     else {
       /* webserver->lastError should be 2 */
diff --git a/src/httpd_thread.c b/src/httpd_thread.c
index acbaf62..84d6781 100644
--- a/src/httpd_thread.c
+++ b/src/httpd_thread.c
@@ -51,34 +51,21 @@ extern int current_httpd_threads;
 @param args Two item array of void-cast pointers to the httpd and request struct
 */
 void
-thread_httpd(void *args) {
-  void	**params;
-  httpd	*webserver;
-  request	*r;
-  int serialnum;
-	
-  current_httpd_threads++;
-
-  params = (void **)args;
-  webserver = *params;
-  r = *(params + 1);
-  serialnum = *(params + 2);
-  free(*(params + 2)); /* XXX We must release this here. */
-  free(params); /* XXX We must release this here. */
+handle_http_request(httpd *webserver, request *r) {
 	
   if (httpdReadRequest(webserver, r) == 0) {
     /*
      * We read the request fine
      */
-    debug(LOG_DEBUG, "Thread %d calling httpdProcessRequest() for %s", serialnum, r->clientAddr);
+    debug(LOG_DEBUG, "Calling httpdProcessRequest() for %s", r->clientAddr);
     httpdProcessRequest(webserver, r);
-    debug(LOG_DEBUG, "Thread %d returned from httpdProcessRequest() for %s", serialnum, r->clientAddr);
+    debug(LOG_DEBUG, "Returned from httpdProcessRequest() for %s", r->clientAddr);
   }
   else {
-    debug(LOG_DEBUG, "Thread %d: No valid request received from %s", serialnum, r->clientAddr);
+    debug(LOG_DEBUG, "No valid request received from %s", r->clientAddr);
   }
   httpdEndRequest(r);
-  debug(LOG_DEBUG, "Thread %d ended request from %s", serialnum, r->clientAddr);
+  debug(LOG_DEBUG, "Ended request from %s", r->clientAddr);
 
   current_httpd_threads--;
 }
diff --git a/src/httpd_thread.h b/src/httpd_thread.h
index b68011a..4c7e037 100644
--- a/src/httpd_thread.h
+++ b/src/httpd_thread.h
@@ -28,6 +28,6 @@
 #define _HTTPD_THREAD_H_
 
 /** @brief Handle a web request */
-void thread_httpd(void *args);
+void handle_http_request(httpd *webserver, request *r);
 
 #endif
-- 
1.8.4.2




More information about the Nodogsplash mailing list