cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX FreeRTOS Task Template Missing osThreadTerminate (vTaskDelete) Safety Guard

baturarslan
Associate

I noticed that the FreeRTOS task function template generated by CubeMX lacks a defensive programming practice recommended in FreeRTOS examples and documentation.

Current Generated Code:

 

void TaskFunction(void const * argument)
{
    for(;;)
    {
        // Task code
    }
}

 

Recommended Pattern (from FreeRTOS examples):

 

void TaskFunction(void const * argument)
{
   for(;;)
   {
       // Task code
   }
   osThreadTerminate(taskTaskHandle); // Safety guard if loop is ever broken
}

 

Rationale:
1. While the infinite loop means the code after it should never be reached, having osThreadTerminate(threadID) (vTaskDelete()) acts as a safety guard
2. If the loop is unexpectedly broken (due to a bug or other condition), the current implementation could lead to undefined behavior
3. This pattern is shown and recommended in official FreeRTOS examples and documentation
4. Adding this guard aligns with defensive programming practices

Impact:
- Low: This is not a functional bug as the code works correctly under normal conditions
- Medium priority as it's a best practice improvement for safety

Request:
Could the task function template be updated to include this safety guard in future CubeMX releases?

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @baturarslan and welcome to the community,

Will escalate this proposal internally for study.

Thank you for your suggestion.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
SofLit
ST Employee

Hello @baturarslan and welcome to the community,

Will escalate this proposal internally for study.

Thank you for your suggestion.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.