[Asterisk-code-review] chan sip: Don't unload module while TCP/TLS threads are stil... (asterisk[14])

Michael Kuron asteriskteam at digium.com
Wed Nov 30 13:31:00 CST 2016


Michael Kuron has uploaded a new change for review. ( https://gerrit.asterisk.org/4537 )

Change subject: chan_sip: Don't unload module while TCP/TLS threads are still running
......................................................................

chan_sip: Don't unload module while TCP/TLS threads are still running

If not all TCP/TLS threads have terminated by the time the chan_sip module is
dlclose()d, we get segfaults because the threads try to execute code from a
library that is no longer in memory. This commit changes unload behavior to
wait for all threads to terminate before completing the unload.

Change-Id: I23a8abf9e8f73b57ff7ada61984b8506aea6cbde
---
M channels/chan_sip.c
1 file changed, 12 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/37/4537/1

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 21828c5..c73e56e 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -35531,18 +35531,23 @@
 	ast_rtp_dtls_cfg_free(&default_dtls_cfg);
 
 	/*
-	 * Wait awhile for the TCP/TLS thread container to become empty.
+	 * Wait until the TCP/TLS thread container becomes empty.
+	 *
+	 * We must not unload the module while a thread is still running,
+	 * otherwise we can get segfaults due to functions called from
+	 * the thread trying to return to their caller, which is no longer
+	 * in memory.
 	 *
 	 * XXX This is a hack, but the worker threads cannot be created
 	 * joinable.  They can die on their own and remove themselves
 	 * from the container thus resulting in a huge memory leak.
 	 */
-	start = ast_tvnow();
-	while (ao2_container_count(threadt) && (ast_tvdiff_sec(ast_tvnow(), start) < 5)) {
-		sched_yield();
-	}
-	if (ao2_container_count(threadt)) {
-		ast_debug(2, "TCP/TLS thread container did not become empty :(\n");
+	while (ao2_container_count(threadt)) {
+		start = ast_tvnow();
+		ast_log(LOG_WARNING, "TCP/TLS thread container did not become empty, waiting 5 seconds\n");
+		while (ao2_container_count(threadt) && (ast_tvdiff_sec(ast_tvnow(), start) < 5)) {
+			sched_yield();
+		}
 	}
 
 	ao2_cleanup(registry_list);

-- 
To view, visit https://gerrit.asterisk.org/4537
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I23a8abf9e8f73b57ff7ada61984b8506aea6cbde
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 14
Gerrit-Owner: Michael Kuron <m.kuron at gmx.de>



More information about the asterisk-code-review mailing list