Add critical section to avoid thread safety issue
[platform/core/uifw/stt.git] / server / sttd_recorder.c
index 16202d0..b83cc47 100644 (file)
@@ -593,9 +593,14 @@ int sttd_recorder_stop()
        if (STTD_RECORDER_STATE_READY == g_recorder_state)
                return 0;
 
+       // critical section required because this function can be called from stt engine thread context
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
+       pthread_mutex_lock(&sttd_audio_in_handle_mutex);
+
        /* Check engine id is valid */
        if (NULL == g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+               pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
@@ -636,6 +641,8 @@ int sttd_recorder_stop()
 #ifdef BUF_SAVE_MODE
        fclose(g_pFile);
 #endif
+       pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
 
        return 0;
 }