Bug encountered while deleting a task

I wrote a simple test procedure: created a task, delayed, and then deleted the task, but the program got stuck.Here is code:

#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "cpu_hal.h"

void PrintTask3(void const *arg) 
{
    while (1) {
        printf(" >[task] hello printTask! CoreId: %u\n", cpu_ll_get_core_id());
    }
}


int TestTask3(void)
{
    printf("TaskCreate...\n");
    TaskHandle_t taskHandle = NULL;
    if (xTaskCreate(PrintTask3, "test_task", 0x800/sizeof(StackType_t), NULL, 24, &taskHandle) != pdPASS)
    {
        printf("TaskCreate failed\n");
    }
    printf("CoreId:  %u\n", cpu_ll_get_core_id());
    vTaskDelay(100);
    printf("TaskDelete...\n");
    vTaskDelete(taskHandle);
    printf("Delete finished...\n");
    return 0;
}

Here is result:

TaskCreate...
CoreId:  1
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
 >[task] hello printTask! CoreId: 0
TaskDelete...

The program did not execute “printf(“Delete finished…\n”);”. But everything works fine while debugging.

TestTask3() should not return from it’s implementing function. Instead it should either be in an infinite loop, or call vTaskDelete( NULL ) instead of returning. See Writing RTOS tasks in FreeRTOS - implementing tasks as forever loops

Also, how is printf() implemented. It is probably not thread safe. Take a look at the FreeRTOS book for how to print from a task. Free RTOS Book and Reference Manual

1 Like

I think deleting the task while printing might cause problems.
Deleting a task from another task is like asynchronously killing it wherever the task code currently is. Better don’t do that !
The task might currently hold a mutex (I guess your printf implementation uses something like that) when it’s deleted which causes a deadlock because the mutex is not released anymore. Or it’s currently transmitting the text via UART at driver level…
Usually applications create the required set of tasks and keep them alive. Then you know the necessary amount of resources (memory) while developing and avoid stumbling into out-of-memory situations at runtime later on.
If you think you really need deleting tasks you should make it synchronous e.g. tell the task (maybe using a simple volatile flag or a task notification) that it should cleanly stop and delete itself (getting finally cleaned up by the idle task later on).

1 Like

Thanks for your advice. It helped me.