From: David Vossel Date: Wed, 16 Jun 2010 17:35:29 +0000 (+0000) Subject: fixes chan_iax2 race condition X-Git-Tag: 1.4.34-rc1~2^2~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3245bb4b8649e4cad5e8a2a57032edca0ed4f2f4;p=thirdparty%2Fasterisk.git fixes chan_iax2 race condition There is code in chan_iax2.c that attempts to guarantee that only a single active thread will handle a call number at a time. This code works once the thread is added to an active_list of threads, but we are not currently guaranteed that a newly activated thread will enter the active_list immediately because it is left up to the thread to add itself after frames have been queued to it. This means that if two frames come in for the same call number at the same time, it is possible for them to grab two separate threads because the first thread did not add itself to the active_list fast enough. This causes some pretty complex problems. This patch resolves this race condition by immediately adding an activated thread to the active_list within the network thread and only depending on the thread to remove itself once it is done processing the frames queued to it. By doing this we are guaranteed that if another frame for the same call number comes in at the same time, that this thread will immediately be found in the active_list of threads. Review: https://reviewboard.asterisk.org/r/720/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@270866 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index 26da023446..70adaa4807 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -8299,6 +8299,7 @@ static int socket_read(int *id, int fd, short events, void *cbdata) memcpy(&thread->ffinfo.sin, &thread->iosin, sizeof(thread->ffinfo.sin)); thread->ffinfo.type = fh->type; thread->ffinfo.csub = fh->csub; + AST_LIST_INSERT_HEAD(&active_list, thread, list); } AST_LIST_UNLOCK(&active_list); } @@ -10034,11 +10035,6 @@ static void *iax2_process_thread(void *data) if (thread->iostate == IAX_IOSTATE_IDLE) continue; - /* Add ourselves to the active list now */ - AST_LIST_LOCK(&active_list); - AST_LIST_INSERT_HEAD(&active_list, thread, list); - AST_LIST_UNLOCK(&active_list); - /* See what we need to do */ switch(thread->iostate) { case IAX_IOSTATE_READY: @@ -10061,7 +10057,9 @@ static void *iax2_process_thread(void *data) thread->curfunc[0]='\0'; #endif - /* Now... remove ourselves from the active list, and return to the idle list */ + /* The network thread added us to the active_thread list when we were given + * frames to process, Now that we are done, we must remove ourselves from + * the active list, and return to the idle list */ AST_LIST_LOCK(&active_list); AST_LIST_REMOVE(&active_list, thread, list); AST_LIST_UNLOCK(&active_list);