From 8ca45f34fa467fe7dd4a267d891def7a9e2e63e3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 14 Mar 2025 08:57:03 +0100 Subject: [PATCH] fix(mdns): Fix potential task delete race Need to wait for the task to be deleted before destroy its stack --- common_components/linux_compat/freertos/freertos_linux.c | 2 ++ components/mdns/mdns.c | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/common_components/linux_compat/freertos/freertos_linux.c b/common_components/linux_compat/freertos/freertos_linux.c index ced90522..ce58d0eb 100644 --- a/common_components/linux_compat/freertos/freertos_linux.c +++ b/common_components/linux_compat/freertos/freertos_linux.c @@ -166,6 +166,8 @@ void vTaskDelete(TaskHandle_t *task) if (task == NULL) { pthread_exit(0); + } else { + pthread_cancel((pthread_t)task); } void *thread_rval = NULL; pthread_join((pthread_t)task, &thread_rval); diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 5a4d5191..388fc959 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -5427,7 +5427,7 @@ static void _mdns_service_task(void *pvParameters) } } _mdns_service_task_handle = NULL; - vTaskDelete(NULL); + vTaskDelay(portMAX_DELAY); } static void _mdns_timer_cb(void *arg) @@ -5532,16 +5532,17 @@ static esp_err_t _mdns_service_task_stop(void) { _mdns_stop_timer(); if (_mdns_service_task_handle) { + TaskHandle_t task_handle = _mdns_service_task_handle; mdns_action_t action; mdns_action_t *a = &action; action.type = ACTION_TASK_STOP; if (xQueueSend(_mdns_server->action_queue, &a, (TickType_t)0) != pdPASS) { - vTaskDelete(_mdns_service_task_handle); _mdns_service_task_handle = NULL; } while (_mdns_service_task_handle) { vTaskDelay(10 / portTICK_PERIOD_MS); } + vTaskDelete(task_handle); } vSemaphoreDelete(_mdns_service_semaphore); _mdns_service_semaphore = NULL;