chan_sip: Fix deadlock of monlock between unload_module and do_monitor
authorCorey Farrell <git@cfware.com>
Fri, 7 Mar 2014 22:56:15 +0000 (22:56 +0000)
committerCorey Farrell <git@cfware.com>
Fri, 7 Mar 2014 22:56:15 +0000 (22:56 +0000)
Release monlock before calling pthread_join.  This ensures do_monitor
cannot freeze by locking monlock during module unload.

(closes issue ASTERISK-21406)
Reported by: Corey Farrell
Review: https://reviewboard.asterisk.org/r/3284/
........

Merged revisions 410224 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 410225 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 410226 from http://svn.asterisk.org/svn/asterisk/branches/12

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

channels/chan_sip.c

index 36680d9..cda8153 100644 (file)
@@ -34430,12 +34430,16 @@ static int unload_module(void)
 
        ast_mutex_lock(&monlock);
        if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
-               pthread_cancel(monitor_thread);
-               pthread_kill(monitor_thread, SIGURG);
-               pthread_join(monitor_thread, NULL);
+               pthread_t th = monitor_thread;
+               monitor_thread = AST_PTHREADT_STOP;
+               pthread_cancel(th);
+               pthread_kill(th, SIGURG);
+               ast_mutex_unlock(&monlock);
+               pthread_join(th, NULL);
+       } else {
+               monitor_thread = AST_PTHREADT_STOP;
+               ast_mutex_unlock(&monlock);
        }
-       monitor_thread = AST_PTHREADT_STOP;
-       ast_mutex_unlock(&monlock);
 
        /* Destroy all the dialogs and free their memory */
        i = ao2_iterator_init(dialogs, 0);