[Asterisk-code-review] stasis/app: don't lock an app before a call to send (asterisk[16.8])

George Joseph asteriskteam at digium.com
Thu Jan 30 09:43:33 CST 2020


George Joseph has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/13717 )

Change subject: stasis/app: don't lock an app before a call to send
......................................................................

stasis/app: don't lock an app before a call to send

Calling 'app_send' eventually calls the app's message handler. It's possible
for a handler to obtain a lock on another object, and then need/want to lock
the app object. If the caller of 'app_send' locks the app object prior to
calling then there's a potential for a deadlock, if another thread calls
'app_send' without locking.

This patch makes it so 'app_send' is not called with the app object locked in
the section of code doing such.

ASTERISK-28423 #close

Change-Id: I6767c6d0933c7db1b984018966eefca4c0638a27
(cherry picked from commit e103339f02f0445b8c77b1c3c6f7d1c80e37f675)
---
M res/stasis/app.c
1 file changed, 15 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/res/stasis/app.c b/res/stasis/app.c
index 859c3d1..dff04b6 100644
--- a/res/stasis/app.c
+++ b/res/stasis/app.c
@@ -1166,8 +1166,23 @@
 			"timestamp", ast_json_timeval(ast_tvnow(), NULL),
 			"application", app->name);
 		if (msg) {
+			/*
+			 * The app must be unlocked before calling 'send' since a handler may
+			 * subsequently attempt to grab the app lock after first obtaining a
+			 * lock for another object, thus causing a deadlock.
+			 */
+			ao2_unlock(app);
 			app_send(app, msg);
+			ao2_lock(app);
 			ast_json_unref(msg);
+			if (!app->handler) {
+				/*
+				 * If the handler disappeared then the app was deactivated. In that
+				 * case don't replace. Re-activation will reset the handler later.
+				 */
+				ao2_unlock(app);
+				return;
+			}
 		}
 	} else {
 		ast_verb(1, "Activating Stasis app '%s'\n", app->name);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/13717
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16.8
Gerrit-Change-Id: I6767c6d0933c7db1b984018966eefca4c0638a27
Gerrit-Change-Number: 13717
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-CC: Friendly Automation
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200130/6e687baf/attachment-0001.html>


More information about the asterisk-code-review mailing list