# Pthread\_cond\_timedwait problem

**URL:** <https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225>\
**Category:** Kernel\
**Created:** [August 12, 2021, 2:31pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225 "2021-08-12T14:31:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoaa](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@brunoaa](https://forums.freertos.org/u/brunoaa)\
**Post date:** [August 12, 2021, 2:31pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/1 "2021-08-12T14:31:43Z")

</div>

Hi, we are trying to find a cause of a HardFault in a STM32F427 application. The FreeRTOS is version 10.4.3 and we are using mixed POSIX and non-POSIX tasks/libraries.

We have tried a number of approaches, but we could not find the cause yet.

There is one thing that could lead to the cause: there is a task that use pthread\_cond\_timedwait with a timeout of 10 ticks, normally the function exits with ETIMEDOUT or a task giving the pthread\_cond\_signal and thats fine.

But sometimes the function exits with zero error, like if some task has given the cond\_signal, but no other task did it, as far as I could check. I did this by checking in xQueueGenericSend if the semaphore in the conditional variable was accessed just before the problem occur.

I don’t know yet if this is directly related to the cause of the HardFault, but it seems like something is corrupting the memory somewhere, and this could lead to the cause.

Is this a good approach? Or there would be a better way to find why the pthread\_cond\_timedwait is exiting with zero without other task intentionally giving the cond\_signal?

Thank you in advance.  
Regards,

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 12, 2021, 5:28pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/2 "2021-08-12T17:28:07Z")

</div>

`pthread_cond_signal` is not something we provide and therefore, it is a bit hard to guess the cause of the error. Where did you get that implementation?

In terms of getting the details about hard fault, can you find out the instruction causing the hard fault by following these instructions - [Debugging and diagnosing hard faults on ARM Cortex-M CPUs](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

This is another good document from ARM about analyzing hard faults: [https://www.keil.com/appnotes/files/apnt209.pdf](https://www.keil.com/appnotes/files/apnt209.pdf)

Thanks.

---

<div class="post-metadata">

**Author:** ![brunoaa](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@brunoaa](https://forums.freertos.org/u/brunoaa)\
**Post date:** [August 12, 2021, 6:12pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/3 "2021-08-12T18:12:10Z")

</div>

Hi Gaurav,

Thanks for the response.

The implementation is from FreeRTOS+Labs

Following the documentation from here:  
 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/3/3838020e7a77fa720e4d60321ab98c2ca1020a17.png)

We have already followed the first link you mentioned, it was actually the first thing to do. But we were not able to find the cause. At the moment of the HardFault, at least the LR and PC are corrupted.

---

<div class="post-metadata">

**Author:** ![gedeonag](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/gedeonag/32/479_2.png) [@gedeonag](https://forums.freertos.org/u/gedeonag)\
**Post date:** [August 12, 2021, 9:48pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/4 "2021-08-12T21:48:19Z")

</div>

Hi,  
Per the pthread\_cond\_wait manual it is allowed to return zero for no obvious reason to the caller (spurious wakeup) , and the suggested use case should be as follows:

```auto
while (! mypredicate(&t) && rc == 0) 
{          
        rc = pthread_cond_timedwait(&t.cond, &t.mn, &ts);
}

```

That being said I am not sure the implementation of FreeRTOS+LAB follows this spec closely or the internal condition for such behavior is even present, but lets be on the safe side and do it the standard way, if you haven’t already done so.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [August 12, 2021, 10:25pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/5 "2021-08-12T22:25:10Z")

</div>

> [@brunoaa](#):
>
> At the moment of the HardFault, at least the LR and PC are corrupted.

That sounds like a memory corruption and fault is most probably the result of a corruption happened earlier. Do you have configASERT, stack overflow check and malloc failed check enabled?

Thanks.

---

<div class="post-metadata">

**Author:** ![brunoaa](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@brunoaa](https://forums.freertos.org/u/brunoaa)\
**Post date:** [August 13, 2021, 1:43pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/6 "2021-08-13T13:43:01Z")

</div>

I’ll try to do this check, thanks!

---

<div class="post-metadata">

**Author:** ![brunoaa](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@brunoaa](https://forums.freertos.org/u/brunoaa)\
**Post date:** [August 13, 2021, 1:45pm UTC](https://forums.freertos.org/t/pthread-cond-timedwait-problem/13225/7 "2021-08-13T13:45:11Z")

</div>

configASSERT and stack overflow are enabled, malloc not. I’ll enable and see if something changes… Thanks!
