[Pkg-voip-commits] [janus] 48/163: Make sure handles are detached immediately when a session is destroyed

Jonas Smedegaard dr at jones.dk
Sat Oct 28 01:22:08 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/0.2.5-1
in repository janus.

commit b7545dc08d4b85225b6b702d01865e9ab59150d3
Author: Lorenzo Miniero <lminiero at gmail.com>
Date:   Mon Jul 31 11:57:03 2017 +0200

    Make sure handles are detached immediately when a session is destroyed
---
 ice.c   | 24 +++++++-----------------
 janus.c | 57 ++++++++++++++++++++++++++++++---------------------------
 janus.h |  4 ++--
 3 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/ice.c b/ice.c
index 2403223..9a71a34 100644
--- a/ice.c
+++ b/ice.c
@@ -506,7 +506,7 @@ static void janus_ice_notify_media(janus_ice_handle *handle, gboolean video, gbo
 		json_object_set_new(event, "seconds", json_integer(no_media_timer));
 	/* Send the event */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", handle->handle_id);
-	janus_session_notify_event(session->session_id, event);
+	janus_session_notify_event(session, event);
 	/* Notify event handlers as well */
 	if(janus_events_is_enabled()) {
 		json_t *info = json_object();
@@ -534,7 +534,7 @@ void janus_ice_notify_hangup(janus_ice_handle *handle, const char *reason) {
 		json_object_set_new(event, "reason", json_string(reason));
 	/* Send the event */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", handle->handle_id);
-	janus_session_notify_event(session->session_id, event);
+	janus_session_notify_event(session, event);
 	/* Notify event handlers as well */
 	if(janus_events_is_enabled()) {
 		json_t *info = json_object();
@@ -1005,11 +1005,9 @@ janus_ice_handle *janus_ice_handle_create(void *gateway_session, const char *opa
 	janus_mutex_init(&handle->mutex);
 
 	/* Set up other stuff. */
-	janus_mutex_lock(&session->mutex);
 	if(session->ice_handles == NULL)
 		session->ice_handles = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
 	g_hash_table_insert(session->ice_handles, janus_uint64_dup(handle->handle_id), handle);
-	janus_mutex_unlock(&session->mutex);
 
 	return handle;
 }
@@ -1018,9 +1016,7 @@ janus_ice_handle *janus_ice_handle_find(void *gateway_session, guint64 handle_id
 	if(gateway_session == NULL)
 		return NULL;
 	janus_session *session = (janus_session *)gateway_session;
-	janus_mutex_lock(&session->mutex);
 	janus_ice_handle *handle = session->ice_handles ? g_hash_table_lookup(session->ice_handles, &handle_id) : NULL;
-	janus_mutex_unlock(&session->mutex);
 	return handle;
 }
 
@@ -1030,20 +1026,19 @@ gint janus_ice_handle_attach_plugin(void *gateway_session, guint64 handle_id, ja
 	if(plugin == NULL)
 		return JANUS_ERROR_PLUGIN_NOT_FOUND;
 	janus_session *session = (janus_session *)gateway_session;
+	if(session->destroy)
+		return JANUS_ERROR_SESSION_NOT_FOUND;
 	janus_ice_handle *handle = janus_ice_handle_find(session, handle_id);
 	if(handle == NULL)
 		return JANUS_ERROR_HANDLE_NOT_FOUND;
-	janus_mutex_lock(&session->mutex);
 	if(handle->app != NULL) {
 		/* This handle is already attached to a plugin */
-		janus_mutex_unlock(&session->mutex);
 		return JANUS_ERROR_PLUGIN_ATTACH;
 	}
 	int error = 0;
 	janus_plugin_session *session_handle = g_malloc0(sizeof(janus_plugin_session));
 	if(session_handle == NULL) {
 		JANUS_LOG(LOG_FATAL, "Memory error!\n");
-		janus_mutex_unlock(&session->mutex);
 		return JANUS_ERROR_UNKNOWN;	/* FIXME Do we need something like "Internal Server Error"? */
 	}
 	session_handle->gateway_handle = handle;
@@ -1052,7 +1047,6 @@ gint janus_ice_handle_attach_plugin(void *gateway_session, guint64 handle_id, ja
 	plugin->create_session(session_handle, &error);
 	if(error) {
 		/* TODO Make error struct to pass verbose information */
-		janus_mutex_unlock(&session->mutex);
 		return error;
 	}
 	handle->app = plugin;
@@ -1061,7 +1055,6 @@ gint janus_ice_handle_attach_plugin(void *gateway_session, guint64 handle_id, ja
 	janus_mutex_lock(&old_plugin_sessions_mutex);
 	g_hash_table_remove(old_plugin_sessions, session_handle);
 	janus_mutex_unlock(&old_plugin_sessions_mutex);
-	janus_mutex_unlock(&session->mutex);
 	/* Notify event handlers */
 	if(janus_events_is_enabled())
 		janus_events_notify_handlers(JANUS_EVENT_TYPE_HANDLE,
@@ -1076,7 +1069,6 @@ gint janus_ice_handle_destroy(void *gateway_session, guint64 handle_id) {
 	janus_ice_handle *handle = janus_ice_handle_find(session, handle_id);
 	if(handle == NULL)
 		return JANUS_ERROR_HANDLE_NOT_FOUND;
-	janus_mutex_lock(&session->mutex);
 	janus_plugin *plugin_t = (janus_plugin *)handle->app;
 	if(plugin_t == NULL) {
 		/* There was no plugin attached, probably something went wrong there */
@@ -1098,7 +1090,6 @@ gint janus_ice_handle_destroy(void *gateway_session, guint64 handle_id) {
 			}
 			g_main_loop_quit(handle->iceloop);
 		}
-		janus_mutex_unlock(&session->mutex);
 		return 0;
 	}
 	JANUS_LOG(LOG_INFO, "Detaching handle from %s\n", plugin_t->get_name());
@@ -1139,8 +1130,7 @@ gint janus_ice_handle_destroy(void *gateway_session, guint64 handle_id) {
 	json_object_set_new(event, "sender", json_integer(handle_id));
 	/* Send the event */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", handle->handle_id);
-	janus_session_notify_event(session->session_id, event);
-	janus_mutex_unlock(&session->mutex);
+	janus_session_notify_event(session, event);
 	/* We only actually destroy the handle later */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Handle detached (error=%d), scheduling destruction\n", handle_id, error);
 	janus_mutex_lock(&old_handles_mutex);
@@ -1490,7 +1480,7 @@ janus_slow_link_update(janus_ice_component *component, janus_ice_handle *handle,
 			json_object_set_new(event, "nacks", json_integer(sl_nack_recent_cnt));
 			/* Send the event */
 			JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", handle->handle_id);
-			janus_session_notify_event(session->session_id, event);
+			janus_session_notify_event(session, event);
 			/* Finally, notify event handlers */
 			if(janus_events_is_enabled()) {
 				json_t *info = json_object();
@@ -4118,7 +4108,7 @@ void janus_ice_dtls_handshake_done(janus_ice_handle *handle, janus_ice_component
 	json_object_set_new(event, "sender", json_integer(handle->handle_id));
 	/* Send the event */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", handle->handle_id);
-	janus_session_notify_event(session->session_id, event);
+	janus_session_notify_event(session, event);
 	/* Notify event handlers as well */
 	if(janus_events_is_enabled()) {
 		json_t *info = json_object();
diff --git a/janus.c b/janus.c
index 6ba8577..25c12e2 100644
--- a/janus.c
+++ b/janus.c
@@ -520,16 +520,12 @@ janus_session *janus_session_find_destroyed(guint64 session_id) {
 	return session;
 }
 
-void janus_session_notify_event(guint64 session_id, json_t *event) {
-	janus_mutex_lock(&sessions_mutex);
-	janus_session *session = sessions ? g_hash_table_lookup(sessions, &session_id) : NULL;
+void janus_session_notify_event(janus_session *session, json_t *event) {
 	if(session != NULL && !session->destroy && session->source != NULL && session->source->transport != NULL) {
-		janus_mutex_unlock(&sessions_mutex);
 		/* Send this to the transport client */
 		JANUS_LOG(LOG_HUGE, "Sending event to %s (%p)\n", session->source->transport->get_package(), session->source->instance);
 		session->source->transport->send_message(session->source->instance, NULL, FALSE, event);
 	} else {
-		janus_mutex_unlock(&sessions_mutex);
 		/* No transport, free the event */
 		json_decref(event);
 	}
@@ -544,21 +540,6 @@ gint janus_session_destroy(guint64 session_id) {
 		return -1;
 	}
 	JANUS_LOG(LOG_VERB, "Destroying session %"SCNu64"\n", session_id);
-	session->destroy = 1;
-	if (session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
-		GHashTableIter iter;
-		gpointer value;
-		/* Remove all handles */
-		g_hash_table_iter_init(&iter, session->ice_handles);
-		while (g_hash_table_iter_next(&iter, NULL, &value)) {
-			janus_ice_handle *handle = value;
-			if(!handle || g_atomic_int_get(&stop)) {
-				continue;
-			}
-			janus_ice_handle_destroy(session, handle->handle_id);
-			g_hash_table_iter_remove(&iter);
-		}
-	}
 
 	/* FIXME Actually destroy session */
 	janus_session_free(session);
@@ -777,7 +758,9 @@ int janus_process_incoming_request(janus_request *request) {
 	session->last_activity = janus_get_monotonic_time();
 	janus_ice_handle *handle = NULL;
 	if(handle_id > 0) {
+		janus_mutex_lock(&session->mutex);
 		handle = janus_ice_handle_find(session, handle_id);
+		janus_mutex_unlock(&session->mutex);
 		if(!handle) {
 			JANUS_LOG(LOG_ERR, "Couldn't find any handle %"SCNu64" in session %"SCNu64"...\n", handle_id, session_id);
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_HANDLE_NOT_FOUND, "No such handle %"SCNu64" in session %"SCNu64"", handle_id, session_id);
@@ -832,9 +815,11 @@ int janus_process_incoming_request(janus_request *request) {
 		json_t *opaque = json_object_get(root, "opaque_id");
 		const char *opaque_id = opaque ? json_string_value(opaque) : NULL;
 		/* Create handle */
+		janus_mutex_lock(&session->mutex);
 		handle = janus_ice_handle_create(session, opaque_id);
 		if(handle == NULL) {
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_UNKNOWN, "Memory error");
+			janus_mutex_unlock(&session->mutex);
 			goto jsondone;
 		}
 		handle_id = handle->handle_id;
@@ -845,13 +830,13 @@ int janus_process_incoming_request(janus_request *request) {
 		if((error = janus_ice_handle_attach_plugin(session, handle_id, plugin_t)) != 0) {
 			/* TODO Make error struct to pass verbose information */
 			janus_ice_handle_destroy(session, handle_id);
-			janus_mutex_lock(&session->mutex);
 			g_hash_table_remove(session->ice_handles, &handle_id);
 			janus_mutex_unlock(&session->mutex);
 			JANUS_LOG(LOG_ERR, "Couldn't attach to plugin '%s', error '%d'\n", plugin_text, error);
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_ATTACH, "Couldn't attach to plugin: error '%d'", error);
 			goto jsondone;
 		}
+		janus_mutex_unlock(&session->mutex);
 		/* Prepare JSON reply */
 		json_t *reply = json_object();
 		json_object_set_new(reply, "janus", json_string("success"));
@@ -868,9 +853,25 @@ int janus_process_incoming_request(janus_request *request) {
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
 			goto jsondone;
 		}
+		janus_mutex_lock(&sessions_mutex);
 		/* Schedule the session for deletion */
+		janus_mutex_lock(&session->mutex);
 		session->destroy = 1;
-		janus_mutex_lock(&sessions_mutex);
+		/* Remove all handles */
+		if(session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
+			GHashTableIter iter;
+			gpointer value;
+			g_hash_table_iter_init(&iter, session->ice_handles);
+			while(g_hash_table_iter_next(&iter, NULL, &value)) {
+				janus_ice_handle *h = value;
+				if(!h || g_atomic_int_get(&stop)) {
+					continue;
+				}
+				janus_ice_handle_destroy(session, h->handle_id);
+				g_hash_table_iter_remove(&iter);
+			}
+		}
+		janus_mutex_unlock(&session->mutex);
 		g_hash_table_remove(sessions, &session->session_id);
 		g_hash_table_insert(old_sessions, janus_uint64_dup(session->session_id), session);
 		GSource *timeout_source = g_timeout_source_new_seconds(3);
@@ -902,8 +903,8 @@ int janus_process_incoming_request(janus_request *request) {
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_DETACH, "No plugin to detach from");
 			goto jsondone;
 		}
-		int error = janus_ice_handle_destroy(session, handle_id);
 		janus_mutex_lock(&session->mutex);
+		int error = janus_ice_handle_destroy(session, handle_id);
 		g_hash_table_remove(session->ice_handles, &handle_id);
 		janus_mutex_unlock(&session->mutex);
 
@@ -2148,7 +2149,9 @@ int janus_process_incoming_admin_request(janus_request *request) {
 	}
 	janus_ice_handle *handle = NULL;
 	if(handle_id > 0) {
+		janus_mutex_lock(&session->mutex);
 		handle = janus_ice_handle_find(session, handle_id);
+		janus_mutex_unlock(&session->mutex);
 		if(!handle) {
 			JANUS_LOG(LOG_ERR, "Couldn't find any handle %"SCNu64" in session %"SCNu64"...\n", handle_id, session_id);
 			ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_HANDLE_NOT_FOUND, "No such handle %"SCNu64" in session %"SCNu64"", handle_id, session_id);
@@ -2165,10 +2168,10 @@ int janus_process_incoming_admin_request(janus_request *request) {
 		}
 		/* List handles */
 		json_t *list = json_array();
+		janus_mutex_lock(&session->mutex);
 		if(session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
 			GHashTableIter iter;
 			gpointer value;
-			janus_mutex_lock(&session->mutex);
 			g_hash_table_iter_init(&iter, session->ice_handles);
 			while (g_hash_table_iter_next(&iter, NULL, &value)) {
 				janus_ice_handle *handle = value;
@@ -2177,8 +2180,8 @@ int janus_process_incoming_admin_request(janus_request *request) {
 				}
 				json_array_append_new(list, json_integer(handle->handle_id));
 			}
-			janus_mutex_unlock(&session->mutex);
 		}
+		janus_mutex_unlock(&session->mutex);
 		/* Prepare JSON reply */
 		json_t *reply = json_object();
 		json_object_set_new(reply, "janus", json_string("success"));
@@ -2733,7 +2736,7 @@ int janus_plugin_push_event(janus_plugin_session *plugin_session, janus_plugin *
 		json_object_set_new(event, "jsep", merged_jsep);
 	/* Send the event */
 	JANUS_LOG(LOG_VERB, "[%"SCNu64"] Sending event to transport...\n", ice_handle->handle_id);
-	janus_session_notify_event(session->session_id, event);
+	janus_session_notify_event(session, event);
 
 	if(jsep != NULL && janus_events_is_enabled()) {
 		/* Notify event handlers as well */
@@ -3146,8 +3149,8 @@ void janus_plugin_end_session(janus_plugin_session *plugin_session) {
 	if(!session)
 		return;
 	/* Destroy the handle */
-	janus_ice_handle_destroy(session, ice_handle->handle_id);
 	janus_mutex_lock(&session->mutex);
+	janus_ice_handle_destroy(session, ice_handle->handle_id);
 	g_hash_table_remove(session->ice_handles, &ice_handle->handle_id);
 	janus_mutex_unlock(&session->mutex);
 }
diff --git a/janus.h b/janus.h
index 20188bc..48b013d 100644
--- a/janus.h
+++ b/janus.h
@@ -73,9 +73,9 @@ janus_session *janus_session_create(guint64 session_id);
  * @returns The created Janus Gateway-Client session if successful, NULL otherwise */
 janus_session *janus_session_find(guint64 session_id);
 /*! \brief Method to add an event to notify to the queue of notifications for this session
- * @param[in] session_id The Janus Gateway-Client session ID
+ * @param[in] session The Janus Gateway-Client session instance to notify
  * @param[in] event The event to notify as a Jansson JSON object */
-void janus_session_notify_event(guint64 session_id, json_t *event);
+void janus_session_notify_event(janus_session *session, json_t *event);
 /*! \brief Method to find an existing Janus Gateway-Client session scheduled to be destroyed from its ID
  * @param[in] session_id The Janus Gateway-Client session ID
  * @returns The created Janus Gateway-Client session if successful, NULL otherwise */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-voip/janus.git



More information about the Pkg-voip-commits mailing list