Changeset: e10c9bbe11a6 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e10c9bbe11a6
Modified Files:
        tools/merovingian/daemon/controlrunner.c
        tools/merovingian/daemon/merovingian.c
        tools/merovingian/daemon/multiplex-funnel.c
Branch: default
Log Message:

mfunnels: serialise stopping of multiplex-funnels

Don't leak threads (multiplexThreads are joinable), which at the same
time also resolves a problem where the funnels terminate after the main
thread, which looks odd.  We now wait for the funnel to actually
terminate before returning to the client.


diffs (122 lines):

diff --git a/tools/merovingian/daemon/controlrunner.c 
b/tools/merovingian/daemon/controlrunner.c
--- a/tools/merovingian/daemon/controlrunner.c
+++ b/tools/merovingian/daemon/controlrunner.c
@@ -281,6 +281,7 @@ static void ctl_handle_client(int msgsoc
                                dp = _mero_topdp->next; /* don't need the 
console/log */
                                while (dp != NULL) {
                                        if (dp->type == MERODB && 
strcmp(dp->dbname, q) == 0) {
+                                               
pthread_mutex_unlock(&_mero_topdp_lock);
                                                if (strcmp(p, "stop") == 0) {
                                                        terminateProcess(dp);
                                                        Mfprintf(_mero_ctlout, 
"%s: stopped "
@@ -294,6 +295,8 @@ static void ctl_handle_client(int msgsoc
                                                send(msgsock, buf2, len, 0);
                                                break;
                                        } else if (dp->type == MEROFUN && 
strcmp(dp->dbname, q) == 0) {
+                                               /* multiplexDestroy needs topdp 
lock to remove itself */
+                                               
pthread_mutex_unlock(&_mero_topdp_lock);
                                                multiplexDestroy(dp->dbname);
                                                len = snprintf(buf2, 
sizeof(buf2), "OK\n");
                                                send(msgsock, buf2, len, 0);
@@ -303,13 +306,13 @@ static void ctl_handle_client(int msgsoc
                                        dp = dp->next;
                                }
                                if (dp == NULL) {
+                                       pthread_mutex_unlock(&_mero_topdp_lock);
                                        Mfprintf(_mero_ctlerr, "%s: received 
stop signal for "
                                                        "non running database: 
%s\n", origin, q);
                                        len = snprintf(buf2, sizeof(buf2),
                                                        "database is not 
running: %s\n", q);
                                        send(msgsock, buf2, len, 0);
                                }
-                               pthread_mutex_unlock(&_mero_topdp_lock);
                        } else if (strcmp(p, "create") == 0) {
                                err e = db_create(q);
                                if (e != NO_ERR) {
diff --git a/tools/merovingian/daemon/merovingian.c 
b/tools/merovingian/daemon/merovingian.c
--- a/tools/merovingian/daemon/merovingian.c
+++ b/tools/merovingian/daemon/merovingian.c
@@ -303,6 +303,17 @@ terminateProcess(void *p)
                        return;
        }
 
+       if (d->type == MEROFUN) {
+               multiplexDestroy(dbname);
+               free(dbname);
+               return;
+       } else if (d->type != MERODB) {
+               /* barf */
+               Mfprintf(stderr, "cannot stop merovingian process role: %s\n", 
dbname);
+               free(dbname);
+               return;
+       }
+
        /* ok, once we get here, we'll be shutting down the server */
        Mfprintf(stdout, "sending process " LLFMT " (database '%s') the "
                        "TERM signal\n", (long long int)pid, dbname);
@@ -1123,24 +1134,20 @@ shutdown:
                pthread_mutex_lock(&_mero_topdp_lock);
                t = d;
                while (t != NULL) {
-                       if (t->type == MERODB) {
-                               if (tl == NULL) {
-                                       tl = tlw = malloc(sizeof(struct 
_threadlist));
-                               } else {
-                                       tlw = tlw->next = malloc(sizeof(struct 
_threadlist));
-                               }
+                       if (tl == NULL) {
+                               tl = tlw = malloc(sizeof(struct _threadlist));
+                       } else {
+                               tlw = tlw->next = malloc(sizeof(struct 
_threadlist));
+                       }
 
-                               tlw->next = NULL;
-                               if ((thret = pthread_create(&(tlw->tid), NULL,
-                                                       (void *(*)(void 
*))terminateProcess, (void *)t)) != 0)
-                               {
-                                       Mfprintf(stderr, "%s: unable to create 
thread to terminate "
-                                                       "database '%s': %s\n",
-                                                       argv[0], d->dbname, 
strerror(thret));
-                                       tlw->tid = 0;
-                               }
-                       } else if (t->type == MEROFUN) {
-                               multiplexDestroy(t->dbname);
+                       tlw->next = NULL;
+                       if ((thret = pthread_create(&(tlw->tid), NULL,
+                                               (void *(*)(void 
*))terminateProcess, (void *)t)) != 0)
+                       {
+                               Mfprintf(stderr, "%s: unable to create thread 
to terminate "
+                                               "database '%s': %s\n",
+                                               argv[0], d->dbname, 
strerror(thret));
+                               tlw->tid = 0;
                        }
 
                        t = t->next;
diff --git a/tools/merovingian/daemon/multiplex-funnel.c 
b/tools/merovingian/daemon/multiplex-funnel.c
--- a/tools/merovingian/daemon/multiplex-funnel.c
+++ b/tools/merovingian/daemon/multiplex-funnel.c
@@ -479,6 +479,7 @@ multiplexDestroy(char *mp)
 
        /* signal the thread to stop and cleanup */
        m->shutdown = 1;
+       pthread_join(m->tid, NULL);
 }
 
 static void
@@ -756,6 +757,8 @@ multiplexThread(void *d)
                }
        }
 
+       Mfprintf(stdout, "stopping mfunnel '%s'\n", m->name);
+
        /* free, cleanup, etc. */
        while (m->clients != NULL) {
                c = m->clients;
@@ -808,6 +811,7 @@ multiplexThread(void *d)
        pthread_mutex_unlock(&_mero_topdp_lock);
 
        free(m->name);
+       free(m);
 }
 
 void
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to