2025-01-30 10:25 AM - last edited on 2026-02-25 4:28 AM by mƎALLEm
I have come across an issue which I believe is a bug in the implementation of the osThreadFlagsWait function.
Scenario
A thread is waiting on a flag (e.g. 0x2u) for a timeout above 0, before this timeout should occur, there are multiple other flags set on the thread (e.g. 0x1u) using osThreadFlagsSet, this results in the osThreadFlagsWait function returning with a value of osErrorTimeout sooner than the set timeout.
Cause
The xTaskNotifyWait function which is called by osThreadFlagsWait returns each time the 0x1u flag is set, resulting in this section of code being run:
Note that t0 is set to the return value of xTaskGetTickCount() at the start of the wait loop, so is the start time, tout is a timeout value passed to xTaskNotifyWait, which is originally the set timeout, but is adjusted each time the xTaskNotifyWait function returns.
The issue with the code above is that the value of td is the total time that the thread has been waiting, this is subtracting from tout each time a flag is set that is not waited on e.g.
for a set timeout of 100, if the flag that is not waited on happens every 10 ticks, the value of td and tout will be as follows for each time xTaskNotifyWait returns:
This gives a timeout of 40 ticks in this case instead of the desired 100.
Suggested Fix
To fix this issue, I would suggest removing the cumulative subtraction from tout, instead replacing it with a subtraction of td from the set timeout e.g.
Solved! Go to Solution.
2026-02-25 4:41 AM
Hello @Graham1
According to an internal feedback FreeRTOS version has been updated to v10.6.2 in STM32WB release v1.24.0. Does it solve the problem reported in this post?
2025-01-30 4:02 PM - edited 2025-01-30 4:02 PM
The code on GitHub already has this change.
What exact chip and library are you using? Are you using the latest version?
If you have an IOC file that generates the faulty code please attach it.
2025-01-31 2:09 AM - edited 2025-01-31 7:43 AM
I am using an STM32WB55, using the FREERTOS middleware from STM32CubeIDEs Pinout & configuration tool with repository version STM32Cube_FW_WB_V1.21.0 which I believe is the latest.
2025-03-04 8:28 AM
This is still not brought over to the STM32CubeWB FW repository for the latest release as of yesterday.
2025-03-05 1:07 AM
Thank you so much for bringing this on the ST Community. I've escalated for review on the Future (under internal ticket number (204574).
Best Regards.
STTwo-32
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.
2026-02-19 6:09 AM - edited 2026-02-19 8:04 AM
Hi everyone,
my apologies for reviving this, but yesterday I discovered another annoying problem with this code.
I was measuring the current consumption to check if it was sufficiently low in case the system goes into stop2.
To my surprise, I discovered that it wasn't (draining 6mA instead of 30uA).
After a long search, I discovered that the high-consuming task state wasn't "suspended", but "blocked"!
A little later, I realized this would happen when I called osThreadFlagsWait.
I assumed the timout value could be osWaitForever, so the task would sleep until a signal arrives and thus be suspended.
However, in a similar scenario, tout -= td becomes the result; what's done essentially amounts to timeout -= td.
Therefore, timeout != osWaitForever, and xTaskNotifyWait will notice this and set a very long timeout.
This causes the behavior to be different (the task is blocked, not suspended).
I've currently tested this as a solution:
if (timeout != osWaitForever) {
if (td > tout) {
tout = 0;
}
else {
tout -= td;
}
}
Is this a correct sollution?
I am also using stm32wb55 and STM32CubeIDE (Version: 1.19.0 Build: 25607_20250703_0907 (UTC)) as tool to configure the hw and generate code.
It generated the FreeRTOS code (FreeRTOS Kernel V10.3.1.h states: FreeRTOS Kernel V10.3.1)
2026-02-25 4:21 AM - edited 2026-02-25 4:21 AM
@fvv I would recommend the following solution:
if (timeout != osWaitForever)
{
if (td >= timeout)
{
tout = 0;
}
else
{
tout = (timeout - td);
}
}This should address the issue I observed, and the issue you describe as well.
2026-02-25 4:41 AM
Hello @Graham1
According to an internal feedback FreeRTOS version has been updated to v10.6.2 in STM32WB release v1.24.0. Does it solve the problem reported in this post?
2026-02-25 6:42 AM - edited 2026-02-25 6:42 AM
2026-02-25 11:29 PM
As the first issue has been solved this thread needs to be closed.
@fvv needs to post the new question in a new thread.
Thank you for the confirmation.