Fix main thread deadlock issue 60/300660/2 accepted/tizen/unified/20231109.053256
authorWootak Jung <wootak.jung@samsung.com>
Tue, 24 Oct 2023 22:45:27 +0000 (07:45 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Wed, 1 Nov 2023 02:04:35 +0000 (11:04 +0900)
GSource should be removed by calling g_source_destroy() not g_source_remove().
Using g_source_remove(), wrong GSources in the main context can be removed.

48492 [pid 23543] writev(4, [{iov_base="\3", iov_len=1}, {iov_base="CAPI_NETWORK_DNSSD\0", iov_len=19},
 {iov_base="dns-sd.c: __remove_service_resolve_socket(1369) > Remove DNSServiceResolve socket\0", iov_len=82}], 3 <unfinished ...>
48517 [pid 23543] write(2, "\n(process:23543): GLib-CRITICAL **: 13:35:03.291: Source ID 1 was not found
 when attempting to remove it\n", 105 <unfinished ...>

Change-Id: I2e2f8accba980073e39a70e9a39ef11b32086314
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
src/dns-sd/dns-sd.c

index 2c96b78a96b67aaf0cc3506bb220179f6c9c414b..62ddd0280d66bb02f9914fa6276bf6bf9eb6b165 100644 (file)
@@ -1117,7 +1117,9 @@ EXPORT_API int dnssd_deregister_local_service(dnssd_service_h local_service)
        }
 
        if (local_handle->watch_id > 0) {
-               g_source_remove(local_handle->watch_id);
+               GMainContext *context = g_main_context_get_thread_default();
+               GSource *source = g_main_context_find_source_by_id(context, local_handle->watch_id);
+               g_source_destroy(source);
                local_handle->watch_id = 0;
        }
 
@@ -1135,8 +1137,11 @@ static gboolean __remove_service_getaddrinfo_socket(gpointer user_data)
        DNSSD_LOCK;
        DNSSD_LOGD("Remove DNSServiceGetAddrInfo socket");
        dnssd_handle_s *local_handle = user_data;
-       if (local_handle->watch_id > 0)
-               g_source_remove(local_handle->watch_id);
+       if (local_handle->watch_id > 0) {
+               GMainContext *context = g_main_context_get_thread_default();
+               GSource *source = g_main_context_find_source_by_id(context, local_handle->watch_id);
+               g_source_destroy(source);
+       }
        DNSServiceRefDeallocate(local_handle->sd_ref);
        local_handle->watch_id = 0;
        local_handle->sd_ref = NULL;
@@ -1368,8 +1373,11 @@ static gboolean __remove_service_resolve_socket(gpointer user_data)
        DNSSD_LOCK;
        DNSSD_LOGD("Remove DNSServiceResolve socket");
        resolve_reply_data *resolve_data = user_data;
-       if (resolve_data->watch_id > 0)
-               g_source_remove(resolve_data->watch_id);
+       if (resolve_data->watch_id > 0) {
+               GMainContext *context = g_main_context_get_thread_default();
+               GSource *source = g_main_context_find_source_by_id(context, resolve_data->watch_id);
+               g_source_destroy(source);
+       }
        DNSServiceRefDeallocate(resolve_data->sd_ref);
        resolve_data->watch_id = 0;
        resolve_data->sd_ref = NULL;
@@ -1398,8 +1406,7 @@ static void __dnssd_resolve_reply_cb(DNSServiceRef sd_ref, unsigned int flags,
                        host_name, resolve_data->service_name, fullname,
                        (const char *) txt_record, txt_len, port);
 
-       g_idle_add_full(G_PRIORITY_HIGH, __remove_service_resolve_socket,
-                                       resolve_data, NULL);
+       __remove_service_resolve_socket(resolve_data);
        DNSSD_UNLOCK;
 }
 
@@ -1692,8 +1699,11 @@ static int __get_valid_browsing_handle(dnssd_service_h dnssd_service, dnssd_hand
 
 void __destroy_browsing_handle(dnssd_handle_s *browsing_handle)
 {
-       if (browsing_handle->watch_id > 0)
-               g_source_remove(browsing_handle->watch_id);
+       if (browsing_handle->watch_id > 0) {
+               GMainContext *context = g_main_context_get_thread_default();
+               GSource *source = g_main_context_find_source_by_id(context, browsing_handle->watch_id);
+               g_source_destroy(source);
+       }
 
        DNSServiceRefDeallocate(browsing_handle->sd_ref);
        browsing_handle->sd_ref = NULL;