Fix shutdown handling of sqlite3 astdb.
authorWalter Doekes <walter+asterisk@wjd.nu>
Mon, 9 Jan 2012 19:37:23 +0000 (19:37 +0000)
committerWalter Doekes <walter+asterisk@wjd.nu>
Mon, 9 Jan 2012 19:37:23 +0000 (19:37 +0000)
If a db_sync was scheduled just before shutdown, the atexit code calling
db_sync would have no effect, causing the astdb commit thread to stay
alive. This caused the SIP/realtime_sipregs test to fail. (The fallback
kill would run the atexit code again and that would wreak havoc.) This
fixes that the atexit kill condition is picked up properly.

(closes issue ASTERISK-18883)
Reviewed by: Terry Wilson

Review: https://reviewboard.asterisk.org/r/1659
........

Merged revisions 350180 from http://svn.asterisk.org/svn/asterisk/branches/10

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@350181 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/db.c

index c3e71c5..96c8145 100644 (file)
--- a/main/db.c
+++ b/main/db.c
@@ -891,6 +891,15 @@ static void *db_sync_thread(void *data)
                ast_mutex_unlock(&dblock);
                sleep(1);
                ast_mutex_lock(&dblock);
                ast_mutex_unlock(&dblock);
                sleep(1);
                ast_mutex_lock(&dblock);
+               /* Unfortunately, it is possible for signaling to happen
+                * when we're not waiting: in the bit when we're unlocked
+                * above. Do the do-exit check here again. (We could do
+                * it once, but that would impose a forced delay of 1
+                * second always.) */
+               if (doexit) {
+                       ast_mutex_unlock(&dblock);
+                       break;
+               }
        }
 
        return NULL;
        }
 
        return NULL;
@@ -898,8 +907,13 @@ static void *db_sync_thread(void *data)
 
 static void astdb_atexit(void)
 {
 
 static void astdb_atexit(void)
 {
+       /* Set doexit to 1 to kill thread. db_sync must be called with
+        * mutex held. */
        doexit = 1;
        doexit = 1;
+       ast_mutex_lock(&dblock);
        db_sync();
        db_sync();
+       ast_mutex_unlock(&dblock);
+
        pthread_join(syncthread, NULL);
        ast_mutex_lock(&dblock);
        sqlite3_close(astdb);
        pthread_join(syncthread, NULL);
        ast_mutex_lock(&dblock);
        sqlite3_close(astdb);