Add defensive code for clearing locked mutex 12/313112/1 accepted/tizen/7.0/unified/20240624.161734
authorJeongmo Yang <jm80.yang@samsung.com>
Wed, 19 Jun 2024 05:02:00 +0000 (14:02 +0900)
committerJeongmo Yang <jm80.yang@samsung.com>
Wed, 19 Jun 2024 06:30:25 +0000 (15:30 +0900)
- The mutex can be cleared although it's locked while calling client user callback.
- Add defensive code to avoid clearing locked mutex.

[Version] 0.4.109
[Issue Type] Improvement

Change-Id: Ib6abaeba5a05c5cdb7f5667c4719c6a2e89bdfe2
Signed-off-by: Jeongmo Yang <jm80.yang@samsung.com>
packaging/capi-media-camera.spec
src/camera.c

index dac5400..dea874b 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-camera
 Summary:    A Camera API
-Version:    0.4.108
+Version:    0.4.109
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 520de04..9221e19 100644 (file)
@@ -1755,6 +1755,8 @@ static void __camera_client_user_callback(camera_cb_info_s *cb_info, char *recv_
        }
 
        g_mutex_unlock(&cb_info->user_cb_mutex[event]);
+
+       CAM_LOG_DEBUG("done - event[%d]", event);
 }
 
 static gboolean __camera_idle_event_callback(gpointer data)
@@ -2386,8 +2388,17 @@ static void __camera_mutex_cond_clear(camera_cb_info_s *cb_info)
        g_mutex_clear(&cb_info->mp_data_mutex);
        g_mutex_clear(&cb_info->bridge_lock);
 
-       for (i = 0 ; i < MUSE_CAMERA_EVENT_TYPE_NUM ; i++)
+       for (i = 0 ; i < MUSE_CAMERA_EVENT_TYPE_NUM ; i++) {
+               /* The mutex can be locked when the user callback is called in idle callback.
+                  This code will wait until it's unlocked and avoid crash by clearing locked mutex. */
+               if (!g_mutex_trylock(&cb_info->user_cb_mutex[i])) {
+                       CAM_LOG_WARNING("user_cb_mutex[%d] is locked somewhere", i);
+                       g_mutex_lock(&cb_info->user_cb_mutex[i]);
+               }
+
+               g_mutex_unlock(&cb_info->user_cb_mutex[i]);
                g_mutex_clear(&cb_info->user_cb_mutex[i]);
+       }
 
        for (i = 0 ; i < MUSE_CAMERA_API_MAX ; i++) {
                g_mutex_clear(&cb_info->api_mutex[i]);