Remove expired notify callback handle 27/261027/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Jul 2021 12:07:55 +0000 (21:07 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 8 Jul 2021 12:24:35 +0000 (21:24 +0900)
If the client reconnects and assigns new notify callback, server should update the callback handle.
However, if finalize method was not invoked, server did not update the handle but make new memory
for saving this handle. And this memory could not be searched by logic.

This patch changes the logic for saving callback handle. By this patch, callback handle is always
updated and dangling memory is disappeared.

Change-Id: I02e89970e7a154c33abf38d71733605988051b95
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_tidl.c

index 662d678..06d5012 100644 (file)
@@ -92,17 +92,49 @@ static void __create_client_cb(rpc_port_stub_tts_context_h context, void *user_d
        if (!sender)
                return;
 
+       SLOG(LOG_INFO, tts_tag(), ">>>>> Client connect. appid(%s)", sender);
        free(sender);
 }
 
 static void __destroy_client_cb(rpc_port_stub_tts_context_h context, void *user_data)
 {
-       char *sender = NULL;
+       void* tag = NULL;
+       rpc_port_stub_tts_context_get_tag(context, &tag);
+
+       if (NULL != tag) {
+               SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS FINALIZE");
+               int uid = (intptr_t)tag;
+
+               if (0 != ttsd_server_finalize(uid)) {
+                       return;
+               }
+
+               pthread_mutex_lock(&g_tidl_proxy_infos_mutex);
+               tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
+               if (NULL == info) {
+                       SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to set notify callback");
+                       pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
+                       return;
+               }
+
+               rpc_port_tts_notify_cb_destroy(info->notify_cb_h);
+               info->notify_cb_h = NULL;
+
+               g_tidl_proxy_infos = g_list_remove(g_tidl_proxy_infos, info);
+               free(info);
+
+               SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
+
+               pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
+       }
+       rpc_port_stub_tts_context_set_tag(context, NULL);
 
+       char *sender = NULL;
        rpc_port_stub_tts_context_get_sender(context, &sender);
        if (!sender)
                return;
 
+       SLOG(LOG_INFO, tts_tag(), ">>>>> Client disconnect. appid(%s)", sender);
        free(sender);
 }
 
@@ -128,10 +160,19 @@ static void __register_cb(rpc_port_stub_tts_context_h context, int pid, int uid,
                credential_needed = TTS_CREDENTIAL_NEEDED_ALREADY_INITIALIZED;
        }
 
-       tts_tidl_proxy_info_s* info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
+       intptr_t ptr_uid = uid;
+       rpc_port_stub_tts_context_set_tag(context, (void*)ptr_uid);
+
+       tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
        if (NULL == info) {
-               SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
-               return;
+               info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
+               if (NULL == info) {
+                       SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
+                       return;
+               }
+       } else {
+               rpc_port_tts_notify_cb_destroy(info->notify_cb_h);
+               info->notify_cb_h = NULL;
        }
 
        if (0 != rpc_port_tts_notify_cb_clone(callback, &info->notify_cb_h)) {
@@ -157,10 +198,19 @@ static int __register_cb_sync(rpc_port_stub_tts_context_h context, int pid, int
 {
        SLOG(LOG_DEBUG, tts_tag(), ">>>>> TTS REGISTER CALLBACK synchronously uid(%d)", uid);
 
-       tts_tidl_proxy_info_s* info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
+       intptr_t ptr_uid = uid;
+       rpc_port_stub_tts_context_set_tag(context, (void*)ptr_uid);
+
+       tts_tidl_proxy_info_s* info = __get_tidl_proxy_info_s(uid);
        if (NULL == info) {
-               SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
-               return TTSD_ERROR_OPERATION_FAILED;
+               info = (tts_tidl_proxy_info_s*)calloc(1, sizeof(tts_tidl_proxy_info_s));
+               if (NULL == info) {
+                       SLOG(LOG_ERROR, tts_tag(), "[TIDL ERROR] Fail to allocate memory for tidl proxy");
+                       return TTSD_ERROR_OUT_OF_MEMORY;
+               }
+       } else {
+               rpc_port_tts_notify_cb_destroy(info->notify_cb_h);
+               info->notify_cb_h = NULL;
        }
 
        if (0 != rpc_port_tts_notify_cb_clone(callback, &info->notify_cb_h)) {
@@ -250,6 +300,8 @@ static int __finalize_cb(rpc_port_stub_tts_context_h context, int uid, void *use
        SLOG(LOG_DEBUG, tts_tag(), "<<<<<");
 
        pthread_mutex_unlock(&g_tidl_proxy_infos_mutex);
+
+       rpc_port_stub_tts_context_set_tag(context, NULL);
        return TTSE_ERROR_NONE;
 }