test(freertos): Fix race condition in suspend-resume tests

This commit fixes a race condition in suspend-resume tests where in
a test task could escape deletion and result in a memory leak after test
completion.
This commit is contained in:
Sudeep Mohanty
2026-01-05 14:30:51 +01:00
parent 61a63b6c00
commit 05a9bc005a
@@ -223,6 +223,12 @@ TEST_CASE("Test vTaskSuspendAll() and xTaskResumeAll() multicore", "[freertos]")
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
xSemaphoreTake(done_sem, portMAX_DELAY); xSemaphoreTake(done_sem, portMAX_DELAY);
} }
// Suspend the test tasks in case they haven't suspended themselves yet
vTaskSuspend(taskA_hdl);
vTaskSuspend(taskB_hdl);
vTaskDelay(10);
// Cleanup the tasks // Cleanup the tasks
vTaskDelete(taskA_hdl); vTaskDelete(taskA_hdl);
vTaskDelete(taskB_hdl); vTaskDelete(taskB_hdl);
@@ -332,6 +338,8 @@ static void test_unblk_a1_task(void *arg)
// Cleanup A2 and interrupt // Cleanup A2 and interrupt
deregister_intr_cb(); deregister_intr_cb();
vTaskSuspend(a2_task_hdl);
vTaskDelay(10);
vTaskDelete(a2_task_hdl); vTaskDelete(a2_task_hdl);
// Indicate done and wait to be deleted // Indicate done and wait to be deleted
@@ -384,6 +392,11 @@ TEST_CASE("Test vTaskSuspendAll allows scheduling on other cores", "[freertos]")
for (int j = 0; j < 2; j++) { for (int j = 0; j < 2; j++) {
xSemaphoreTake(test_unblk_done_sem, portMAX_DELAY); xSemaphoreTake(test_unblk_done_sem, portMAX_DELAY);
} }
// Suspend the test tasks in case they haven't suspended themselves yet
vTaskSuspend(a1_task_hdl);
vTaskSuspend(b1_task_hdl);
vTaskDelay(10);
// Cleanup tasks // Cleanup tasks
vTaskDelete(a1_task_hdl); vTaskDelete(a1_task_hdl);
vTaskDelete(b1_task_hdl); vTaskDelete(b1_task_hdl);
@@ -505,6 +518,10 @@ TEST_CASE("Test vTaskSuspendAll doesn't block unpinned tasks from being schedule
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
xSemaphoreTake(test_unpinned_sem, portMAX_DELAY); xSemaphoreTake(test_unpinned_sem, portMAX_DELAY);
} }
// Suspend the test tasks in case they haven't suspended themselves yet
vTaskSuspend(pinned_task_hdl);
vTaskSuspend(unpinned_task_hdl);
vTaskDelay(10);
// Cleanup // Cleanup
vTaskDelete(pinned_task_hdl); vTaskDelete(pinned_task_hdl);
@@ -631,6 +648,9 @@ TEST_CASE("Test xTaskResumeAll resumes pended tasks", "[freertos]")
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(test_pended_running_task, "susp", 2048, (void *)xTaskGetCurrentTaskHandle(), UNITY_FREERTOS_PRIORITY + 1, &susp_tsk_hdl, i)); TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(test_pended_running_task, "susp", 2048, (void *)xTaskGetCurrentTaskHandle(), UNITY_FREERTOS_PRIORITY + 1, &susp_tsk_hdl, i));
// Wait for to be notified to test completion // Wait for to be notified to test completion
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
// Suspend the test task in case it hasn't suspended itself yet
vTaskSuspend(susp_tsk_hdl);
vTaskDelay(10);
vTaskDelete(susp_tsk_hdl); vTaskDelete(susp_tsk_hdl);
} }
// Add a short delay to allow the idle task to free any remaining task memory // Add a short delay to allow the idle task to free any remaining task memory
@@ -744,8 +764,12 @@ TEST_CASE("Test xTaskSuspendAll on all cores pends all tasks and xTaskResumeAll
// Cleanup // Cleanup
for (int i = 0; i < TEST_PENDED_NUM_BLOCKED_TASKS; i++) { for (int i = 0; i < TEST_PENDED_NUM_BLOCKED_TASKS; i++) {
vTaskSuspend(blkd_tasks[i]);
vTaskDelay(10);
vTaskDelete(blkd_tasks[i]); vTaskDelete(blkd_tasks[i]);
} }
vTaskSuspend(susp_task);
vTaskDelay(10);
vTaskDelete(susp_task); vTaskDelete(susp_task);
vSemaphoreDelete(done_sem); vSemaphoreDelete(done_sem);
} }