View Issue Details

IDProjectCategoryView StatusLast Update
0002803libmicrohttpdthread poolpublic2013-03-29 20:09
Reporterulion Assigned ToChristian Grothoff  
PriorityhighSeveritycrashReproducibilityalways
Status closedResolutionfixed 
PlatformdarwinOSiosOS Version5
Product Version0.9.24 
Target Version0.9.26Fixed in Version0.9.26 
Summary0002803: thread pool shared mutex crash on some platform.
Descriptionin current thread pool code, it direct copy the daemon struct from the master to each thread's, include the cleanup_connection_mutex struct, but this will cause crash on some platform, such as darwin/ios5 in my test, it always crash after first thread in the pool exited, and the second thread will crash in MHD_cleanup_connections() at pthread_mutex_lock.

so on some platform, the pthread mutex struct is not copy and sharable, it cause crash.
I made a patch to let each worker thread use it's own cleanup mutex, that save us from the crash when stopping the daemon in xbmc development.
TagsNo tags attached.
Attached Files
0001-Fix-thread-pool-mutex-crash.-the-cleanup-mutex-struc.patch (1,880 bytes)   
From c679a913c831a2c9e7a1deb35469fa701ab30536 Mon Sep 17 00:00:00 2001
From: ulion <ulion2002@gmail.com>
Date: Wed, 20 Feb 2013 18:49:10 +0800
Subject: [PATCH] Fix thread pool mutex crash. the cleanup mutex struct should
 not be copied and share used by multiple thread, which
 cause crash on some platform, e.g. ios/darwin

---
 src/daemon/daemon.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 5d4d20d..72619c3 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -2536,6 +2536,16 @@ MHD_start_daemon_va (unsigned int options,
           if (i < leftover_conns)
             ++d->max_connections;
 
+          /* Must init cleanup connection mutex for each worker */
+          if (0 != pthread_mutex_init (&d->cleanup_connection_mutex, NULL))
+            {
+#if HAVE_MESSAGES
+              MHD_DLOG (daemon,
+                       "MHD failed to initialize cleanup connection mutex for thread worker %d\n", i);
+#endif
+              goto thread_failed;
+            }
+
           /* Spawn the worker thread */
           if (0 != (res_thread_create = create_thread (&d->pid, daemon, &MHD_select_thread, d)))
             {
@@ -2546,6 +2556,7 @@ MHD_start_daemon_va (unsigned int options,
 #endif
               /* Free memory for this worker; cleanup below handles
                * all previously-created workers. */
+              pthread_mutex_destroy (&d->cleanup_connection_mutex);
               goto thread_failed;
             }
         }
@@ -2707,6 +2718,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
 	      MHD_PANIC ("Failed to join a thread\n");
 	    }
 	  close_all_connections (&daemon->worker_pool[i]);
+	  pthread_mutex_destroy (&daemon->worker_pool[i].cleanup_connection_mutex);
 	}
       free (daemon->worker_pool);
     }
-- 
1.7.7.5 (Apple Git-26)

Activities

Christian Grothoff

2013-03-04 13:38

manager   ~0006923

Was fixed on March 1st.

Issue History

Date Modified Username Field Change
2013-02-20 12:32 ulion New Issue
2013-02-20 12:32 ulion File Added: 0001-Fix-thread-pool-mutex-crash.-the-cleanup-mutex-struc.patch
2013-03-04 13:38 Christian Grothoff Note Added: 0006923
2013-03-04 13:38 Christian Grothoff Status new => resolved
2013-03-04 13:38 Christian Grothoff Fixed in Version => 0.9.25
2013-03-04 13:38 Christian Grothoff Resolution open => fixed
2013-03-04 13:38 Christian Grothoff Assigned To => Christian Grothoff
2013-03-04 13:39 Christian Grothoff Target Version => 0.9.25
2013-03-04 13:40 Christian Grothoff Fixed in Version 0.9.25 => 0.9.26
2013-03-04 13:40 Christian Grothoff Target Version 0.9.25 => 0.9.26
2013-03-29 20:09 Christian Grothoff Status resolved => closed
2013-05-06 12:53 Christian Grothoff Category multi-threaded operation => libmicrohttpd multi-threaded operation
2024-01-21 13:25 Christian Grothoff Category libmicrohttpd multi-threaded operation => thread pool