From caceba7988af5ac32b93167600d04328a95a8ad4 Mon Sep 17 00:00:00 2001
From: Jaco Kroon <jaco@uls.co.za>
Date: Sat, 22 May 2021 14:48:18 +0200
Subject: [PATCH] func_lock: Fix requesters counter in error paths.

In two places we bail out with failure after we've already incremented
the requesters counter, if this occured then it would effectively result
in unload to wait indefinitely, thus preventing clean shutdown.

Change-Id: I362a6c0dc424f736d4a9c733d818e72d19675283
Signed-off-by: Jaco Kroon <jaco@uls.co.za>
---
 funcs/func_lock.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/funcs/func_lock.c b/funcs/func_lock.c
index 072640751e..ad4d58ef8b 100644
--- a/funcs/func_lock.c
+++ b/funcs/func_lock.c
@@ -268,7 +268,13 @@ static int get_lock(struct ast_channel *chan, char *lockname, int trylock)
 
 	if (!clframe) {
 		if (unloading) {
+			ast_log(LOG_ERROR,
+				"Busy unloading.  %sLOCK will fail.\n",
+				trylock ? "TRY" : "");
 			/* Don't bother */
+			ast_mutex_lock(&current->mutex);
+			current->requesters--;
+			ast_mutex_unlock(&current->mutex);
 			AST_LIST_UNLOCK(list);
 			return -1;
 		}
@@ -277,6 +283,9 @@ static int get_lock(struct ast_channel *chan, char *lockname, int trylock)
 			ast_log(LOG_ERROR,
 				"Unable to allocate channel lock frame.  %sLOCK will fail.\n",
 				trylock ? "TRY" : "");
+			ast_mutex_lock(&current->mutex);
+			current->requesters--;
+			ast_mutex_unlock(&current->mutex);
 			AST_LIST_UNLOCK(list);
 			return -1;
 		}
-- 
GitLab