# xStreamBufferReset and Mutex is buggy

**URL:** <https://forums.freertos.org/t/xstreambufferreset-and-mutex-is-buggy/7284>\
**Category:** Kernel\
**Created:** [June 15, 2018, 12:39pm UTC](https://forums.freertos.org/t/xstreambufferreset-and-mutex-is-buggy/7284 "2018-06-15T12:39:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 15, 2018, 12:39pm UTC](https://forums.freertos.org/t/xstreambufferreset-and-mutex-is-buggy/7284/1 "2018-06-15T12:39:37Z")

</div>

**[seagull\_kamome](https://sourceforge.net/u/seagull_kamome/)** wrote on [Friday, June 15, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/e42489af/#f175):

I guess that my previous post got filtered. So, this is second post.

Hi FreeRTOS developer team.

I’m currently testing FreeRTOS kernel for our project. And, found  
something weird.

1. 

xStreamBufferReset doesn’t save a STATICALLY\_ALLOCATED flag in ucFlags. So,  
it will crashes our system at vStreamBufferDelete by calling free().  
2.

xSemaphoreGet with Mutex doesn’t give priority back to that mutex holder  
when timed out

Sorry. I can’t explain well in english so I’ll show the unit test code.

## Task1

static SemaphoreHandle\_t mx1;

static unsigned int volatile test\_counter;

static TaskHandle\_t test\_task;

static void test\_task\_main\_1(void\* args \_\_unused) {

xSemaphoreTake(mx1, 10); // will be fail.

++test\_counter;

ulTaskNotifyTake(0, portMAX\_DELAY);

++test\_counter;

xSemaphoreTake(mx1, portMAX\_DELAY);

++test\_counter;

ulTaskNotifyTake(0, portMAX\_DELAY);

++test\_counter;

xSemaphoreGive(mx1);

vTaskDelete(NULL);

}

## and test task. This task has priority (tskIDLE\_PRIORITY + 2).

should\_eq(xTaskGetCurrentTaskHandle(), xSemaphoreGetMutexHolder(mx1)); //  
precondition check

should\_eq(tskIDLE\_PRIORITY + 2, uxTaskPriorityGet(NULL)); // precondition  
check

test\_counter = 0;

should\_eq(pdTRUE, xTaskCreate(test\_task\_main\_1, “TSK”,  
configMINIMAL\_STACK\_SIZE, NULL, tskIDLE\_PRIORITY + 3, &test\_task));

should\_eq(tskIDLE\_PRIORITY + 3, uxTaskPriorityGet(NULL)); // OK. the  
priority is inherited.

should\_eq(0, test\_counter); // OK. the task still waiting.

vTaskDelay(11);

should\_eq(1, test\_counter); // OK. the task already timed out

// All tests is OK until here.

// BUG? FreeRTOS never descent priority into base when the waiting task  
got timeout.

should\_eq(tskIDLE\_PRIORITY + 2, uxTaskPriorityGet(NULL)); // Fail: actual  
is tskIDLE\_PRIORITY + 3

xTaskNotifyGive(test\_task); // elevate again. the task will preempt control.

taskYIELD(); // but actually not.

should\_eq(2, test\_counter); // OK. the task waiting again.

should\_eq(tskIDLE\_PRIORITY + 3, uxTaskPriorityGet(NULL)); // OK

should\_eq(pdTRUE, xSemaphoreGive(mx1)); // OK

should\_eq(3, test\_counter); // OK

should\_eq(test\_task, xSemaphoreGetMutexHolder(mx1)); // OK

should\_eq(tskIDLE\_PRIORITY + 2, uxTaskPriorityGet(NULL)); // Fail:! I’m  
still in tskIDLE\_PRIORITY + 3!!!

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 18, 2018, 3:55am UTC](https://forums.freertos.org/t/xstreambufferreset-and-mutex-is-buggy/7284/2 "2018-06-18T03:55:29Z")

</div>

**[seagull\_kamome](https://sourceforge.net/u/seagull_kamome/)** wrote on [Monday, June 18, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/e42489af/#56a2):

Finally. I found this comment .

```
            /* Only disinherit if no other mutexes are held. This is a
			simplification in the priority inheritance implementation. If
			the task that holds the mutex is also holding other mutexes then
			the other mutexes may have caused the priority inheritance. */
```
