Skip to content

Commit cb40f88

Browse files
committed
Merge branch 'contrib/github_pr_565' into 'master'
fix(log_router): Do not enter critical section until mutex is taken (GitHub PR) Closes AEGHB-1190 See merge request ae_group/esp-iot-solution!1365
2 parents 4bf90fb + 944a2eb commit cb40f88

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# ChangeLog
22

3+
## v0.1.1 - 2025-08-25
4+
5+
### Bug fix ###
6+
7+
* Do not enter critical section until the mutex is taken.
8+
* Return ESP_ERR_NO_MEM if creating the mutex fails.
9+
*
310
## v0.1.0 - 2025-7-1
411

512
First release version.

components/utilities/log_router/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.0"
1+
version: "0.1.1"
22
description: LogRouter redirects and filters ESP logs to various storage media
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/utilities/log_router
44
repository: https://github.com/espressif/esp-iot-solution.git

components/utilities/log_router/log_router.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ int esp_log_router_flash_vprintf(const char *format, va_list args)
118118
}
119119

120120
// Lock mutex for thread safety
121-
if (g_log_router_mutex) {
122-
xSemaphoreTake(g_log_router_mutex, pdMS_TO_TICKS(100));
123-
}
121+
xSemaphoreTake(g_log_router_mutex, portMAX_DELAY);
124122

125123
// Write to all files that match the log level
126124
esp_log_router_slist_t *item;
@@ -244,9 +242,7 @@ int esp_log_router_flash_vprintf(const char *format, va_list args)
244242
}
245243

246244
// Release mutex
247-
if (g_log_router_mutex) {
248-
xSemaphoreGive(g_log_router_mutex);
249-
}
245+
xSemaphoreGive(g_log_router_mutex);
250246

251247
return ret;
252248
}
@@ -290,6 +286,15 @@ static void esp_log_router_shutdown(void)
290286

291287
esp_err_t esp_log_router_to_file(const char* file_path, const char* tag, esp_log_level_t level)
292288
{
289+
// Create mutex for thread safety, the first time this function is called.
290+
if (g_log_router_mutex == NULL) {
291+
g_log_router_mutex = xSemaphoreCreateMutex();
292+
if (g_log_router_mutex == NULL) {
293+
ESP_LOGE(TAG, "Failed to create mutex for log router");
294+
return ESP_ERR_NO_MEM;
295+
}
296+
}
297+
293298
if (!file_path) {
294299
return ESP_ERR_INVALID_ARG;
295300
}
@@ -376,12 +381,6 @@ esp_err_t esp_log_router_to_file(const char* file_path, const char* tag, esp_log
376381
SLIST_INSERT_HEAD(&g_esp_log_router_slist_head, new_log_router, next);
377382
g_esp_log_router_vprintf = esp_log_set_vprintf(esp_log_router_flash_vprintf);
378383
esp_register_shutdown_handler(esp_log_router_shutdown);
379-
380-
// Create mutex for thread safety when first node is added
381-
g_log_router_mutex = xSemaphoreCreateMutex();
382-
if (g_log_router_mutex == NULL) {
383-
ESP_LOGW(TAG, "Failed to create mutex for log router");
384-
}
385384
} else {
386385
// Find the last node and insert after it
387386
esp_log_router_slist_t *last = SLIST_FIRST(&g_esp_log_router_slist_head);

0 commit comments

Comments
 (0)