Skip to main content
Explorer
November 18, 2024
Solved

CubeMX FreeRTOS Task Template Missing osThreadTerminate (vTaskDelete) Safety Guard

  • November 18, 2024
  • 1 reply
  • 905 views

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?

Best answer by mƎALLEm

Hello @baturarslan and welcome to the community,

Will escalate this proposal internally for study. Internal ticket 196456.

Thank you for your suggestion.

1 reply

mƎALLEm
mƎALLEmBest answer
Technical Moderator
November 18, 2024

Hello @baturarslan and welcome to the community,

Will escalate this proposal internally for study. Internal ticket 196456.

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."