Merged revisions 273793 via svnmerge from
authorTilghman Lesher <tilghman@meg.abyt.es>
Sat, 3 Jul 2010 02:36:31 +0000 (02:36 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Sat, 3 Jul 2010 02:36:31 +0000 (02:36 +0000)
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
  r273793 | tilghman | 2010-07-02 16:36:39 -0500 (Fri, 02 Jul 2010) | 9 lines

  Have the DEADLOCK_AVOIDANCE macro warn when an unlock fails, to help catch potentially large software bugs.

  (closes issue #17407)
   Reported by: pdf
   Patches:
         20100527__issue17407.diff.txt uploaded by tilghman (license 14)

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

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

channels/chan_agent.c
channels/chan_h323.c
channels/chan_local.c
include/asterisk/lock.h

index 3451de2..fd05bed 100644 (file)
@@ -715,7 +715,12 @@ static int agent_indicate(struct ast_channel *ast, int condition, const void *da
        ast_mutex_lock(&p->lock);
        if (p->chan && !ast_check_hangup(p->chan)) {
                while (ast_channel_trylock(p->chan)) {
        ast_mutex_lock(&p->lock);
        if (p->chan && !ast_check_hangup(p->chan)) {
                while (ast_channel_trylock(p->chan)) {
-                       ast_channel_unlock(ast);
+                       int res;
+                       if ((res = ast_channel_unlock(ast))) {
+                               ast_log(LOG_ERROR, "chan_agent bug! Channel was not locked upon entry to agent_indicate: %s\n", strerror(res));
+                               ast_mutex_unlock(&p->lock);
+                               return -1;
+                       }
                        usleep(1);
                        ast_channel_lock(ast);
                }
                        usleep(1);
                        ast_channel_lock(ast);
                }
index 3d07bce..1023419 100644 (file)
@@ -306,7 +306,7 @@ static int oh323_simulate_dtmf_end(const void *data)
        if (pvt) {
                ast_mutex_lock(&pvt->lock);
                /* Don't hold pvt lock while trying to lock the channel */
        if (pvt) {
                ast_mutex_lock(&pvt->lock);
                /* Don't hold pvt lock while trying to lock the channel */
-               while(pvt->owner && ast_channel_trylock(pvt->owner)) {
+               while (pvt->owner && ast_channel_trylock(pvt->owner)) {
                        DEADLOCK_AVOIDANCE(&pvt->lock);
                }
 
                        DEADLOCK_AVOIDANCE(&pvt->lock);
                }
 
index e17cc27..c372394 100644 (file)
@@ -250,7 +250,11 @@ static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_fra
 
        /* Ensure that we have both channels locked */
        while (other && ast_channel_trylock(other)) {
 
        /* Ensure that we have both channels locked */
        while (other && ast_channel_trylock(other)) {
-               ast_mutex_unlock(&p->lock);
+               int res;
+               if ((res = ast_mutex_unlock(&p->lock))) {
+                       ast_log(LOG_ERROR, "chan_local bug! '&p->lock' was not locked when entering local_queue_frame! (%s)\n", strerror(res));
+                       return -1;
+               }
                if (us && us_locked) {
                        do {
                                CHANNEL_DEADLOCK_AVOIDANCE(us);
                if (us && us_locked) {
                        do {
                                CHANNEL_DEADLOCK_AVOIDANCE(us);
index 5c42d1e..711edcd 100644 (file)
@@ -60,6 +60,7 @@
 #endif
 
 #include "asterisk/logger.h"
 #endif
 
 #include "asterisk/logger.h"
+#include "asterisk/compiler.h"
 
 #define AST_PTHREADT_NULL (pthread_t) -1
 #define AST_PTHREADT_STOP (pthread_t) -2
 
 #define AST_PTHREADT_NULL (pthread_t) -1
 #define AST_PTHREADT_STOP (pthread_t) -2
@@ -339,12 +340,20 @@ int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, in
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(ao2_object_get_lockaddr(chan), __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
-               ast_channel_unlock(chan); \
+               int __res2 = ast_channel_unlock(chan); \
                usleep(1); \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
                usleep(1); \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
-                       ast_channel_lock(chan); \
+                       if (__res2) { \
+                               ast_log(LOG_WARNING, "Could not unlock channel '%s': %s and no lock info found!  I will NOT try to relock.\n", #chan, strerror(__res2)); \
+                       } else { \
+                               ast_channel_lock(chan); \
+                       } \
                } else { \
                } else { \
-                       __ao2_lock(chan, __filename, __func, __lineno, __mutex_name); \
+                       if (__res2) { \
+                               ast_log(LOG_WARNING, "Could not unlock channel '%s': %s.  {{{Originally locked at %s line %d: (%s) '%s'}}}  I will NOT try to relock.\n", #chan, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
+                       } else { \
+                               __ao2_lock(chan, __filename, __func, __lineno, __mutex_name); \
+                       } \
                } \
        } while (0)
 
                } \
        } while (0)
 
@@ -353,12 +362,20 @@ int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, in
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
-               ast_mutex_unlock(lock); \
+               int __res2 = ast_mutex_unlock(lock); \
                usleep(1); \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
                usleep(1); \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
-                       ast_mutex_lock(lock); \
+                       if (__res2 == 0) { \
+                               ast_mutex_lock(lock); \
+                       } else { \
+                               ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s and no lock info found!  I will NOT try to relock.\n", #lock, strerror(__res2)); \
+                       } \
                } else { \
                } else { \
-                       __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
+                       if (__res2 == 0) { \
+                               __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
+                       } else { \
+                               ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s.  {{{Originally locked at %s line %d: (%s) '%s'}}}  I will NOT try to relock.\n", #lock, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
+                       } \
                } \
        } while (0)
 
                } \
        } while (0)
 
@@ -379,7 +396,7 @@ int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, in
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
                char __filename[80], __func[80], __mutex_name[80]; \
                int __lineno; \
                int __res = ast_find_lock_info(lock, __filename, sizeof(__filename), &__lineno, __func, sizeof(__func), __mutex_name, sizeof(__mutex_name)); \
-               ast_mutex_unlock(lock);
+               int __res2 = ast_mutex_unlock(lock);
 
 /*!
  * \brief Deadlock avoidance lock
 
 /*!
  * \brief Deadlock avoidance lock
@@ -395,9 +412,17 @@ int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, in
  */
 #define DLA_LOCK(lock) \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
  */
 #define DLA_LOCK(lock) \
                if (__res < 0) { /* Shouldn't ever happen, but just in case... */ \
-                       ast_mutex_lock(lock); \
+                       if (__res2) { \
+                               ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s and no lock info found!  I will NOT try to relock.\n", #lock, strerror(__res2)); \
+                       } else { \
+                               ast_mutex_lock(lock); \
+                       } \
                } else { \
                } else { \
-                       __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
+                       if (__res2) { \
+                               ast_log(LOG_WARNING, "Could not unlock mutex '%s': %s.  {{{Originally locked at %s line %d: (%s) '%s'}}}  I will NOT try to relock.\n", #lock, strerror(__res2), __filename, __lineno, __func, __mutex_name); \
+                       } else { \
+                               __ast_pthread_mutex_lock(__filename, __lineno, __func, __mutex_name, lock); \
+                       } \
                } \
        } while (0)
 
                } \
        } while (0)
 
@@ -447,9 +472,15 @@ static inline void delete_reentrancy_cs(struct ast_lock_track *lt)
        ast_channel_lock(chan);
 
 #define        DEADLOCK_AVOIDANCE(lock) \
        ast_channel_lock(chan);
 
 #define        DEADLOCK_AVOIDANCE(lock) \
-       ast_mutex_unlock(lock); \
-       usleep(1); \
-       ast_mutex_lock(lock);
+       do { \
+               int __res; \
+               if (!(__res = ast_mutex_unlock(lock))) { \
+                       usleep(1); \
+                       ast_mutex_lock(lock); \
+               } else { \
+                       ast_log(LOG_WARNING, "Failed to unlock mutex '%s' (%s).  I will NOT try to relock. {{{ THIS IS A BUG. }}}\n", #lock, strerror(__res)); \
+               } \
+       } while (0)
 
 #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)
 
 
 #define DLA_UNLOCK(lock) ast_mutex_unlock(lock)