* Misc changes to debug chat lockups
authorArt Cancro <ajc@citadel.org>
Mon, 21 Feb 2005 19:58:39 +0000 (19:58 +0000)
committerArt Cancro <ajc@citadel.org>
Mon, 21 Feb 2005 19:58:39 +0000 (19:58 +0000)
* Dead session purge is now O(n) instead of O(something_larger)
* Session binding for each transaction is faster too

webcit/auth.c
webcit/context_loop.c
webcit/paging.c
webcit/tcp_sockets.c
webcit/webcit.c

index 42c3c0a263d2889365e26308be1c9fa0b24575ce..a5d87b08db59a2a349702940bf1328623a5188a8 100644 (file)
@@ -423,6 +423,7 @@ void changepw(void)
        serv_gets(buf);
        sprintf(WC->ImportantMessage, "%s", &buf[4]);
        if (buf[0] == '2') {
+               safestrncpy(WC->wc_password, buf, sizeof WC->wc_password);
                display_main_menu();
        }
        else {
index 387d63703e333b1c9b2ef79e498c2d535f398351..9389591d693d2c0b2a4d7529c797c8cfeeaa2b4d 100644 (file)
@@ -59,61 +59,67 @@ void free_attachments(struct wcsession *sess) {
 
 void do_housekeeping(void)
 {
-       struct wcsession *sptr, *ss, *session_to_kill;
+       struct wcsession *sptr, *ss;
+       struct wcsession *sessions_to_kill = NULL;
        int num_sessions = 0;
        static int num_threads = MIN_WORKER_THREADS;
 
-       do {
-               session_to_kill = NULL;
-               pthread_mutex_lock(&SessionListMutex);
-               num_sessions = 0;
-               for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       ++num_sessions;
-
-                       /* Kill idle sessions */
-                       if ((time(NULL) - (sptr->lastreq)) >
-                          (time_t) WEBCIT_TIMEOUT) {
-                               sptr->killthis = 1;
-                       }
-
-                       /* Remove sessions flagged for kill */
-                       if (sptr->killthis) {
-
-                               lprintf(3, "Destroying session %d\n",
-                                       sptr->wc_session);
+       /*
+        * Lock the session list, moving any candidates for euthanasia into
+        * a separate list.
+        */
+       pthread_mutex_lock(&SessionListMutex);
+       num_sessions = 0;
+       for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+               ++num_sessions;
+
+               /* Kill idle sessions */
+               if ((time(NULL) - (sptr->lastreq)) >
+                  (time_t) WEBCIT_TIMEOUT) {
+                       sptr->killthis = 1;
+               }
 
-                               /* remove session from linked list */
-                               if (sptr == SessionList) {
-                                       SessionList = SessionList->next;
-                               }
-                               else for (ss=SessionList;ss!=NULL;ss=ss->next) {
-                                       if (ss->next == sptr) {
-                                               ss->next = ss->next->next;
-                                       }
-                               }
+               /* Remove sessions flagged for kill */
+               if (sptr->killthis) {
 
-                               session_to_kill = sptr;
-                               goto BREAKOUT;
+                       /* remove session from linked list */
+                       if (sptr == SessionList) {
+                               SessionList = SessionList->next;
                        }
-               }
-BREAKOUT:      pthread_mutex_unlock(&SessionListMutex);
-
-               if (session_to_kill != NULL) {
-                       pthread_mutex_lock(&session_to_kill->SessionMutex);
-                       close(session_to_kill->serv_sock);
-                       close(session_to_kill->chat_sock);
-                       if (session_to_kill->preferences != NULL) {
-                               free(session_to_kill->preferences);
+                       else for (ss=SessionList;ss!=NULL;ss=ss->next) {
+                               if (ss->next == sptr) {
+                                       ss->next = ss->next->next;
+                               }
                        }
-                       free_attachments(session_to_kill);
-                       pthread_mutex_unlock(&session_to_kill->SessionMutex);
-                       free(session_to_kill);
+
+                       sptr->next = sessions_to_kill;
+                       sessions_to_kill = sptr;
                }
+       }
+       pthread_mutex_unlock(&SessionListMutex);
 
-       } while (session_to_kill != NULL);
+       /*
+        * Now free up and destroy the culled sessions.
+        */
+       while (sessions_to_kill != NULL) {
+               lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
+               pthread_mutex_lock(&sessions_to_kill->SessionMutex);
+               close(sessions_to_kill->serv_sock);
+               close(sessions_to_kill->chat_sock);
+               if (sessions_to_kill->preferences != NULL) {
+                       free(sessions_to_kill->preferences);
+               }
+               free_attachments(sessions_to_kill);
+               pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
+               sptr = sessions_to_kill->next;
+               free(sessions_to_kill);
+               sessions_to_kill = sessions_to_kill->next;
+               --num_sessions;
+       }
 
        /*
-        * See if we need more worker threads
+        * If there are more sessions than threads, then we should spawn
+        * more threads ... up to a predefined maximum.
         */
        while ( (num_sessions > num_threads)
              && (num_threads <= MAX_WORKER_THREADS) ) {
@@ -357,23 +363,22 @@ void context_loop(int sock)
         */
        TheSession = NULL;
 
-       if ( (TheSession == NULL) && (strlen(httpauth_user) > 0) ) {
+       if (TheSession == NULL) {
                pthread_mutex_lock(&SessionListMutex);
                for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       if ( (!strcasecmp(sptr->httpauth_user, httpauth_user))
+
+                       /* If HTTP-AUTH, look for a session with matching credentials */
+                       if ( (strlen(httpauth_user) > 0)
+                          &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
                           &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
                                TheSession = sptr;
                        }
-               }
-               pthread_mutex_unlock(&SessionListMutex);
-       }
 
-       if ( (TheSession == NULL) && (desired_session != 0) ) {
-               pthread_mutex_lock(&SessionListMutex);
-               for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
-                       if (sptr->wc_session == desired_session) {
+                       /* If cookie-session, look for a session with matching session ID */
+                       if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
                                TheSession = sptr;
                        }
+
                }
                pthread_mutex_unlock(&SessionListMutex);
        }
@@ -407,11 +412,14 @@ void context_loop(int sock)
        /*
         * Bind to the session and perform the transaction
         */
+       lprintf(9, "LOCKING SESSION %d\n", TheSession->wc_session);
        pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
+       lprintf(9, "LOCKKED SESSION %d\n", TheSession->wc_session);
        pthread_setspecific(MyConKey, (void *)TheSession);
        TheSession->http_sock = sock;
        TheSession->lastreq = time(NULL);                       /* log */
        session_loop(req);                              /* do transaction */
+       lprintf(9, "UNLKING SESSION %d\n", TheSession->wc_session);
        pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
 
        /* Free the request buffer */
index c892b7a64ed9926286d76b34962692e584450297..bc3fc936ea56f112423abb1d5e4d0c410a3888a3 100644 (file)
@@ -268,6 +268,7 @@ void chat_recv(void) {
        char cl_text[SIZ];
        char *output_data = NULL;
 
+       lprintf(9, "chat_recv() called\n");
        output_headers(0, 0, 0, 0, 0, 0, 0);
 
        wprintf("Content-type: text/html\n");
@@ -324,6 +325,7 @@ void chat_recv(void) {
        } while ( (got_data) && (!end_chat_now) );
 
        if (end_chat_now) {
+               lprintf(9, "exiting chat\n");
                close(WC->chat_sock);
                WC->chat_sock = (-1);
                wprintf("<IMG SRC=\"/static/blank.gif\" onLoad=\"parent.window.close();\">\n");
@@ -336,6 +338,7 @@ void chat_recv(void) {
                }
 
                /* Output our fun to the other frame. */
+               lprintf(9, "output to other frame\n");
                wprintf("<IMG SRC=\"/static/blank.gif\" WIDTH=1 HEIGHT=1\n"
                        "onLoad=\" \n"
                );
@@ -397,6 +400,7 @@ void chat_recv(void) {
                }
 
                wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
+               lprintf(9, "done fun\n");
        }
 
        free(output_data);
index 6752c5957a0df426f3844fa6a4b16aa70a015f74..240e8e4c70f9d1fc5ff15c0043f66524e49ccde0 100644 (file)
@@ -7,8 +7,8 @@
 
 /*
  * Uncomment this to log all communications with the Citadel server
-#define SERV_TRACE 1
  */
+#define SERV_TRACE 1
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -178,7 +178,7 @@ void serv_gets(char *strbuf)
        if (strbuf[len-1] == 10) strbuf[--len] = 0;
        if (strbuf[len-1] == 13) strbuf[--len] = 0;
 #ifdef SERV_TRACE
-       lprintf(9, ">%s\n", strbuf);
+       lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
 #endif
 }
 
@@ -216,7 +216,7 @@ void serv_puts(char *string)
        char buf[SIZ];
 
 #ifdef SERV_TRACE
-       lprintf(9, "<%s\n", string);
+       lprintf(9, "%3d<%s\n", WC->serv_sock, string);
 #endif
        sprintf(buf, "%s\n", string);
        serv_write(buf, strlen(buf));
index 91362db586c669077b91aeea7c990f3b4f4f7d8d..1339e33aec8e53e5af1e8214d88ecbe1c9c33c02 100644 (file)
@@ -451,9 +451,11 @@ void check_for_instant_messages()
 {
        char buf[SIZ];
 
+       lprintf(9, "Checking for instant messages...\n");
        serv_puts("NOOP");
        serv_gets(buf);
        if (buf[3] == '*') WC->HaveInstantMessages = 1;
+       lprintf(9, "...done\n");
 }
 
 
@@ -1050,8 +1052,6 @@ void session_loop(struct httprequest *req)
                goto SKIP_ALL_THIS_CRAP;
        }
 
-       check_for_instant_messages();
-
        /*
         * If we're not logged in, but we have username and password cookies
         * supplied by the browser, try using them to log in.
@@ -1081,6 +1081,11 @@ void session_loop(struct httprequest *req)
                }
        }
 
+       /*
+        * If there are instant messages waiting, retrieve them for display.
+        */
+       check_for_instant_messages();
+
        if (!strcasecmp(action, "image")) {
                output_image();