2024-11-18 12:58 AM - last edited on 2024-11-18 01:23 AM by SofLit
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?
Solved! Go to Solution.
2024-11-18 01:14 AM - edited 2024-11-18 01:14 AM
Hello @baturarslan and welcome to the community,
Will escalate this proposal internally for study.
Thank you for your suggestion.
2024-11-18 01:14 AM - edited 2024-11-18 01:14 AM
Hello @baturarslan and welcome to the community,
Will escalate this proposal internally for study.
Thank you for your suggestion.