FreeRTOS hangs after vTaskEndScheduler in Posix_GCC port

I have problems with Posix GCC port.

I’m using FreeRTOS on x86-64 to run some tests and at the end call vTaskEndScheduler().

I’ve created MRE based on blinky example app:

diff --git a/FreeRTOS/Demo/Posix_GCC/main.c b/FreeRTOS/Demo/Posix_GCC/main.c
index e89da3b9e..9a2fc0d3d 100644
--- a/FreeRTOS/Demo/Posix_GCC/main.c
+++ b/FreeRTOS/Demo/Posix_GCC/main.c
@@ -82,11 +82,13 @@
 #endif
 
 /* Demo type is passed as an argument */
-#ifdef USER_DEMO
-    #define     mainSELECTED_APPLICATION    USER_DEMO
-#else /* Default Setting */
-    #define    mainSELECTED_APPLICATION     FULL_DEMO
-#endif
+// #ifdef USER_DEMO
+//     #define     mainSELECTED_APPLICATION    USER_DEMO
+// #else /* Default Setting */
+//     #define    mainSELECTED_APPLICATION     FULL_DEMO
+// #endif
+
+#define     mainSELECTED_APPLICATION    USER_DEMO
 
 /* This demo uses heap_3.c (the libc provided malloc() and free()). */
 
diff --git a/FreeRTOS/Demo/Posix_GCC/main_blinky.c b/FreeRTOS/Demo/Posix_GCC/main_blinky.c
index c599c933a..d451d2841 100644
--- a/FreeRTOS/Demo/Posix_GCC/main_blinky.c
+++ b/FreeRTOS/Demo/Posix_GCC/main_blinky.c
@@ -174,9 +174,9 @@ void main_blinky( void )
      * there was insufficient FreeRTOS heap memory available for the idle and/or
      * timer tasks	to be created.  See the memory management section on the
      * FreeRTOS web site for more details. */
-    for( ; ; )
-    {
-    }
+    // for( ; ; )
+    // {
+    // }
 }
 /*-----------------------------------------------------------*/
 
@@ -205,6 +205,10 @@ static void prvQueueSendTask( void * pvParameters )
          * will not block - it shouldn't need to block as the queue should always
          * have at least one space at this point in the code. */
         xQueueSend( xQueue, &ulValueToSend, 0U );
+
+        if (xNextWakeTime > 400) {
+            vTaskEndScheduler();
+        }
     }
 }
 /*-----------------------------------------------------------*/

I’ve compiled as cmake .; make.

Running in loop with until ! nice -n 20 ./posix_demo; do date; done.

After some time (10-100 loops) posix_demo hangs.

Attaching to gdb shows following:

# sudo gdb -p 35553                                                                                                                                                                                                                                                   
GNU gdb (Ubuntu 15.1-1ubuntu2) 15.1
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 35553
[New LWP 35559]
[New LWP 35557]
[New LWP 35556]
[New LWP 35555]
[New LWP 35554]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x0000719b4b845ed8 in __GI___sigtimedwait (set=set@entry=0x7fff1df3ff70, info=info@entry=0x7fff1df3fea0, timeout=timeout@entry=0x0) at ../sysdeps/unix/sysv/linux/sigtimedwait.c:31

warning: 31     ../sysdeps/unix/sysv/linux/sigtimedwait.c: Nie ma takiego pliku ani katalogu
(gdb) info thr
  Id   Target Id                                           Frame
* 1    Thread 0x719b4ba7e740 (LWP 35553) "Scheduler"       0x0000719b4b845ed8 in __GI___sigtimedwait (set=set@entry=0x7fff1df3ff70, info=info@entry=0x7fff1df3fea0, timeout=timeout@entry=0x0) at ../sysdeps/unix/sysv/linux/sigtimedwait.c:31
  2    Thread 0x719b484006c0 (LWP 35559) "Scheduler timer" 0x0000719b4b8f38e3 in __GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x719b483ffe70, rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
  3    Thread 0x719b498006c0 (LWP 35557) "IDLE"            0x0000719b4b89dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x6218c9f76e60, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
  4    Thread 0x719b4a2006c0 (LWP 35556) "TX"              futex_wait (futex_word=0x6218c9f76fa0, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
  5    Thread 0x719b4ac006c0 (LWP 35555) "Rx"              0x0000719b4b89dffd in __futex_abstimed_wait_common64 (private=29083, futex_word=0x6218c9f76a24, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
  6    Thread 0x719b4b6006c0 (LWP 35554) "TzCtrl"          0x0000719b4b89dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x6218c9f562f4, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
(gdb) thr 4
[Switching to thread 4 (Thread 0x719b4a2006c0 (LWP 35556))]
#0  futex_wait (futex_word=0x6218c9f76fa0, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
warning: 146    ../sysdeps/nptl/futex-internal.h: Nie ma takiego pliku ani katalogu
(gdb) bt
#0  futex_wait (futex_word=0x6218c9f76fa0, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
#1  __GI___lll_lock_wait (futex=futex@entry=0x6218c9f76fa0, private=0) at ./nptl/lowlevellock.c:49
#2  0x0000719b4b8a5541 in lll_mutex_lock_optimized (mutex=0x6218c9f76fa0) at ./nptl/pthread_mutex_lock.c:48
#3  ___pthread_mutex_lock (mutex=0x6218c9f76fa0) at ./nptl/pthread_mutex_lock.c:93
#4  0x00006218a6eb583e in event_signal (ev=0x6218c9f76fa0) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:104
#5  0x00006218a6eb536c in vPortCancelThread (pxTaskToDelete=0x6218a6f1f320 <xTimerTaskTCB.0>) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:445
#6  0x00006218a6eb13d7 in prvDeleteTCB (pxTCB=0x6218a6f1f320 <xTimerTaskTCB.0>) at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:6432
#7  0x00006218a6eade64 in vTaskDelete (xTaskToDelete=0x6218a6f1f320 <xTimerTaskTCB.0>) at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:2319
#8  0x00006218a6eaf0aa in vTaskEndScheduler () at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:3778
#9  0x00006218a6e90abc in prvQueueSendTask (pvParameters=0x0) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main_blinky.c:210
#10 0x00006218a6eb53f1 in prvWaitForStart (pvParams=0x719b4ba7cfe8) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:465
#11 0x0000719b4b8a1e2e in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#12 0x0000719b4b933a4c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
(gdb) fr 3
#3  ___pthread_mutex_lock (mutex=0x6218c9f76fa0) at ./nptl/pthread_mutex_lock.c:93
warning: 93     ./nptl/pthread_mutex_lock.c: Nie ma takiego pliku ani katalogu
(gdb) p mutex.__data
$1 = {__lock = 2, __count = 0, __owner = 35558, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}}
(gdb)

To summarize: task TX waits on mutex. Its owner is task 35558 which no longer exists. It was Tmr Svc task. To add - Tmr Svc task is first task deleted after calling vTaskEndScheduler(). With vTaskSuspendAll() before vTaskEndScheduler() app still hangs.

Problem occurs on current master (2dcc47ecb) and 202406.01 LTS.

I wonder it I’m using vTaskEndScheduler incorrectly or there some issue with that port.

There is a recent PR to address a hang in the POSIX port’s vTaskEndScheduler - POSIX Simulator: Handle `pthread`s not created by FreeRTOS differently by johnboiles · Pull Request #1223 · FreeRTOS/FreeRTOS-Kernel · GitHub. Would you please try that and see if it address your issue?

I’ve tested and nothing changed. The issue remains the same - TX task waits on mutex with owner Tmr Svc which is deleted.

I’m not sure that’s my situation after reading PR description - vTaskEndScheduler is called from FreeRTOS task and all tasks in example are FreeRTOS tasks.

I tried running your sample code for a long time and I am unable to repro this. This indicates that it is likely a race condition. I am trying to read the code and your call stack to determine the cause of the hang. Would you please test the following patch: posix_port.patch (614 Bytes).

Without PR 1223 and with patch I immediately get following crash (multiple times):

gdb ./posix_demo                        ─╯
GNU gdb (Ubuntu 15.1-1ubuntu2) 15.1
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./posix_demo...
(gdb) break configASSERT
Function "configASSERT" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) break vAssertCalled
Breakpoint 1 at 0x376d: file /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main.c, line 328.
(gdb) r
Starting program: /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/posix_demo 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7a006c0 (LWP 48280)]

Trace started.
The trace will be dumped to disk if a call to configASSERT() fails.
Starting echo blinky demo
[New Thread 0x7ffff70006c0 (LWP 48281)]
[New Thread 0x7ffff66006c0 (LWP 48282)]
[New Thread 0x7ffff5c006c0 (LWP 48283)]
[New Thread 0x7ffff52006c0 (LWP 48284)]
[New Thread 0x7ffff48006c0 (LWP 48285)]
Message received from task
Message received from task
[Thread 0x7ffff52006c0 (LWP 48284) exited]
[Switching to Thread 0x7ffff66006c0 (LWP 48282)]

Thread 4 "TX" hit Breakpoint 1, vAssertCalled (pcFileName=0x55555557e870 "/data/code/FreeRTOS/FreeRTOS/Source/tasks.c", ulLine=5132) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main.c:328
328         volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;
(gdb) bt
#0  vAssertCalled (pcFileName=0x55555557e870 "/data/code/FreeRTOS/FreeRTOS/Source/tasks.c", ulLine=5132) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main.c:328
#1  0x000055555557733e in vTaskSwitchContext () at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:5132
#2  0x000055555557c0e2 in prvPortYieldFromISR () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:309
#3  0x000055555557c11b in vPortYield () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:321
#4  0x0000555555574ea2 in vTaskDelete (xTaskToDelete=0x5555555c6260 <xIdleTaskTCB.2>) at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:2331
#5  0x00005555555760d3 in vTaskEndScheduler () at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:3785
#6  0x0000555555557abc in prvQueueSendTask (pvParameters=0x0) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main_blinky.c:210
#7  0x000055555557c3f1 in prvWaitForStart (pvParams=0x7ffff7fa1fe8) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:465
#8  0x00007ffff7ca1e2e in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#9  0x00007ffff7d33a4c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

I have no hard evidence but feel like running demo with lower priority and some benchmark (e.g. `openssl speed -multi 8) increases likelihood of hang. But hangs also occurs without load.

Crash with your patch was without extra load.

It looks like same issue as 1217 in FreeRTOS-Kernel (cannot post link).

I was able to repro it once when I used Ubuntu 24.04. Would you please try the following patch -
posix_port.patch (1.1 KB)?

I raised this PR to address this issue - Mark mutex as robust to prevent deadlocks by aggarg · Pull Request #1233 · FreeRTOS/FreeRTOS-Kernel · GitHub.

@jtymejczyk would you be to try and see if this fixes your issue?

After 15 minutes both programs (minimal example and original test where I first noticed problem) are running without hangs. I’ll leave them running until Monday.

For extra information: problems was observed on multiple machines, but all run Ubuntu in different versions (from 20.04 to 24.10; native system, dockerized and in VM)

Thank you for testing. Let us know whatever you find!

After ~2 hours minimal example didn’t hang after calling vTaskEndScheduler.
But it hanged before.

Output:

[...]
Trace started.
The trace will be dumped to disk if a call to configASSERT() fails.
Starting echo blinky demo
Message received from task
Message received from task
Message received from task
sob, 25 sty 2025, 19:09:25 CET

Trace started.
The trace will be dumped to disk if a call to configASSERT() fails.
Starting echo blinky demo
Message received from task
Message received from task
sob, 25 sty 2025, 19:09:26 CET

Trace started.
The trace will be dumped to disk if a call to configASSERT() fails.
Starting echo blinky demo
//end of output
(gdb) info thr
  Id   Target Id                                            Frame
* 1    Thread 0x7aa632465740 (LWP 101329) "Scheduler"       0x00007aa632245ed8 in __GI___sigtimedwait (set=set@entry=0x7ffcb272d850,
    info=info@entry=0x7ffcb272d780, timeout=timeout@entry=0x0) at ../sysdeps/unix/sysv/linux/sigtimedwait.c:31
  2    Thread 0x7aa62ee006c0 (LWP 101335) "Scheduler timer" 0x00007aa6322f38e3 in __GI___clock_nanosleep (clock_id=clock_id@entry=0,
    flags=flags@entry=0, req=req@entry=0x7aa62edffe70, rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
  3    Thread 0x7aa62f8006c0 (LWP 101334) "Tmr Svc"         0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0,
    futex_word=0x573989344ff8, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
  4    Thread 0x7aa6302006c0 (LWP 101333) "IDLE"            0x00007aa6322f38e3 in __GI___clock_nanosleep (clock_id=clock_id@entry=0,
    flags=flags@entry=0, req=req@entry=0x7aa6301ffe40, rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
  5    Thread 0x7aa630c006c0 (LWP 101332) "TX"              0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0,
    futex_word=0x573989344c78, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
  6    Thread 0x7aa6316006c0 (LWP 101331) "Rx"              0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0,
    futex_word=0x573989344a2c, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
  7    Thread 0x7aa6320006c0 (LWP 101330) "TzCtrl"          0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0,
    futex_word=0x5739893242fc, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
(gdb) bt
#0  0x00007aa632245ed8 in __GI___sigtimedwait (set=set@entry=0x7ffcb272d850, info=info@entry=0x7ffcb272d780, timeout=timeout@entry=0x0)
    at ../sysdeps/unix/sysv/linux/sigtimedwait.c:31
#1  0x00007aa6322455ac in __GI___sigwait (set=0x7ffcb272d850, sig=0x7ffcb272d84c) at ../sysdeps/unix/sysv/linux/sigwait.c:28
#2  0x000057396092b210 in xPortStartScheduler () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:293
#3  0x00005739609251ab in vTaskStartScheduler () at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:3739
#4  0x0000573960906b41 in main_blinky () at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main_blinky.c:169
#5  0x00005739609066fe in main () at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main.c:176
(gdb) thr 5
[Switching to thread 5 (Thread 0x7aa630c006c0 (LWP 101332))]
#0  0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x573989344c78, expected=0, op=393, abstime=0x0,
    cancel=true) at ./nptl/futex-internal.c:57
warning: 57     ./nptl/futex-internal.c: Nie ma takiego pliku ani katalogu
(gdb) bt
#0  0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x573989344c78, expected=0, op=393, abstime=0x0,
    cancel=true) at ./nptl/futex-internal.c:57
#1  __futex_abstimed_wait_common (futex_word=0x573989344c78, expected=0, clockid=0, abstime=0x0, private=0, cancel=true)
    at ./nptl/futex-internal.c:87
#2  __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x573989344c78, expected=expected@entry=0,
    clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=0) at ./nptl/futex-internal.c:139
#3  0x00007aa6322a0b6d in __pthread_cond_wait_common (cond=0x573989344c50, mutex=0x573989344c20, clockid=0, abstime=0x0)
    at ./nptl/pthread_cond_wait.c:503
#4  ___pthread_cond_wait (cond=0x573989344c50, mutex=0x573989344c20) at ./nptl/pthread_cond_wait.c:627
#5  0x000057396092b9e1 in event_wait (ev=0x573989344c20)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:84
#6  0x000057396092b764 in prvSuspendSelf (thread=0x7aa632463fe8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:604
#7  0x000057396092b736 in prvSwitchThread (pxThreadToResume=0x5739609b5418 <stackTzCtrl+131032>, pxThreadToSuspend=0x7aa632463fe8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:582
#8  0x000057396092b387 in prvPortYieldFromISR () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:377
#9  0x000057396092b39c in vPortYield () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:385
#10 0x0000573960924184 in xTaskDelayUntil (pxPreviousWakeTime=0x7aa630bffe78, xTimeIncrement=200)
    at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:2425
#11 0x0000573960906b8e in prvQueueSendTask (pvParameters=0x0) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main_blinky.c:201
#12 0x000057396092b6c2 in prvWaitForStart (pvParams=0x7aa632463fe8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:546
#13 0x00007aa6322a1e2e in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#14 0x00007aa632333a4c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
(gdb) thr 6
[Switching to thread 6 (Thread 0x7aa6316006c0 (LWP 101331))]
#0  0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x573989344a2c, expected=0, op=393, abstime=0x0,
    cancel=true) at ./nptl/futex-internal.c:57
57      in ./nptl/futex-internal.c
(gdb) bt
#0  0x00007aa63229dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x573989344a2c, expected=0, op=393, abstime=0x0,
    cancel=true) at ./nptl/futex-internal.c:57
#1  __futex_abstimed_wait_common (futex_word=0x573989344a2c, expected=0, clockid=0, abstime=0x0, private=0, cancel=true)
    at ./nptl/futex-internal.c:87
#2  __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x573989344a2c, expected=expected@entry=0,
    clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=0) at ./nptl/futex-internal.c:139
#3  0x00007aa6322a0b6d in __pthread_cond_wait_common (cond=0x573989344a00, mutex=0x5739893449d0, clockid=0, abstime=0x0)
    at ./nptl/pthread_cond_wait.c:503
#4  ___pthread_cond_wait (cond=0x573989344a00, mutex=0x5739893449d0) at ./nptl/pthread_cond_wait.c:627
#5  0x000057396092b9e1 in event_wait (ev=0x5739893449d0)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:84
#6  0x000057396092b764 in prvSuspendSelf (thread=0x5739893448d8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:604
#7  0x000057396092b736 in prvSwitchThread (pxThreadToResume=0x5739609b5418 <stackTzCtrl+131032>, pxThreadToSuspend=0x5739893448d8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:582
#8  0x000057396092b387 in prvPortYieldFromISR () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:377
#9  0x000057396092b39c in vPortYield () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:385
#10 0x000057396091f3dc in xQueueReceive (xQueue=0x573989324840, pvBuffer=0x7aa6315ffe84, xTicksToWait=18446744073709551615)
    at /data/code/FreeRTOS/FreeRTOS/Source/queue.c:1614
#11 0x0000573960906c54 in prvQueueReceiveTask (pvParameters=0x0) at /data/code/FreeRTOS/FreeRTOS/Demo/Posix_GCC/main_blinky.c:248
#12 0x000057396092b6c2 in prvWaitForStart (pvParams=0x5739893448d8)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:546
#13 0x00007aa6322a1e2e in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#14 0x00007aa632333a4c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
(gdb)

I’m attaching core dump and binary.
coredump.7z (373.5 KB

While using this PR or while not using this PR?

With FreeRTOS-Kernel from PR 1233.

I meant “hanged before calling vTaskEndScheduler”.

Hi! I spent quite some time with the posix port for Linux a year ago with Kernel version ‘V10.5.1’. Now I will never change the posix code unless absolutely necessary. I had two problems: memory leakage (found by Valgrind) and the other was crashes. I’m not sure if this has anything to do with your problems though.

One problem was that removing all tasks caused the scheduler to crash. So I put the xSchedulerRunning = pdFALSE; first in the vTaskEndScheduler(). Then I had to check the xSchedulerRunning in two places in ‘tasks.c’.

void vTaskSwitchContext( void )
{
    traceENTER_vTaskSwitchContext();

    if (xSchedulerRunning == pdFALSE)
    {
        return;
    }

And in function portTASK_FUNCTION():

for( ; configCONTROL_INFINITE_LOOP(); )
{
    /* See if any tasks have deleted themselves - if so then the idle task
     * is responsible for freeing the deleted task's TCB and stack. */
    prvCheckTasksWaitingTermination();

    if (!xSchedulerRunning)
    {
         break;
    }

I also added a delete function in ‘timers.c’ to delete the timer queue:

void vTimerDeleteTimerTask( void )
{
    // Delete the timer task queue.
    vQueueDelete(xTimerQueue);
}

Which is called from the end of the vTaskEndScheduler(). Then I get the nice result from Valgrind :slight_smile:

==58226== 
==58226== HEAP SUMMARY:
==58226==     in use at exit: 0 bytes in 0 blocks
==58226==   total heap usage: 28,089 allocs, 28,089 frees, 10,596,362 bytes allocated
==58226== 
==58226== All heap blocks were freed -- no leaks are possible

I made some other changes too, to the ‘port.c’ I think…
/Martin

1 Like

I am not able to load the binary and coredump but analyzing the stack that you shared, it seems that both Tx and Rx tasks are waiting legitimately. Did you change mainTASK_SEND_FREQUENCY_MS? Do I need to change anything to repro this?

I didn’t changed mainTASK_SEND_FREQUENCY_MS. I’ve used same app as described in first post + FreeRTOS-Kernel from Pull Request.

Which OS are you running it on?

I’m using Ubuntu 24.10.

I have again this situation: hang after printing Starting echo blinky demo, no Message received from task prints.

(gdb) info thr
  Id   Target Id                                            Frame
  1    Thread 0x7a136e87b740 (LWP 187131) "Scheduler"       0x00007a136e645ed8 in __GI___sigtimedwait (set=set@entry=0x7ffd16425670, info=info@entry=0x7ffd164255a0, timeout=timeout@entry=0x0)
    at ../sysdeps/unix/sysv/linux/sigtimedwait.c:31
* 2    Thread 0x7a136b2006c0 (LWP 187138) "Scheduler timer" 0x00007a136e6f38e3 in __GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7a136b1ffe70,
    rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
  3    Thread 0x7a136bc006c0 (LWP 187137) "Tmr Svc"         0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeeca01ff8, expected=0, op=393, abstime=0x0, cancel=true)
    at ./nptl/futex-internal.c:57
  4    Thread 0x7a136c6006c0 (LWP 187136) "IDLE"            0x00007a136e6f38e3 in __GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7a136c5ffe40,
    rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:48
  5    Thread 0x7a136d0006c0 (LWP 187135) "TX"              0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeeca01c7c, expected=0, op=393, abstime=0x0, cancel=true)
    at ./nptl/futex-internal.c:57
  6    Thread 0x7a136da006c0 (LWP 187134) "Rx"              0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeeca01a2c, expected=0, op=393, abstime=0x0, cancel=true)
    at ./nptl/futex-internal.c:57
  7    Thread 0x7a136e4006c0 (LWP 187133) "TzCtrl"          0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeec9e12f8, expected=0, op=393, abstime=0x0, cancel=true)
    at ./nptl/futex-internal.c:57
(gdb) p xTickCount
$3 = 2
(gdb)

Can I check anything in this state?

Or create better core dump that you can open?

(gdb) thr 7
[Switching to thread 7 (Thread 0x7a136e4006c0 (LWP 187133))]
#0  0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeec9e12f8, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
warning: 57     ./nptl/futex-internal.c: Nie ma takiego pliku ani katalogu
(gdb) bt
#0  0x00007a136e69dffd in __futex_abstimed_wait_common64 (private=0, futex_word=0x59eeec9e12f8, expected=0, op=393, abstime=0x0, cancel=true) at ./nptl/futex-internal.c:57
#1  __futex_abstimed_wait_common (futex_word=0x59eeec9e12f8, expected=0, clockid=0, abstime=0x0, private=0, cancel=true) at ./nptl/futex-internal.c:87
#2  __GI___futex_abstimed_wait_cancelable64 (futex_word=futex_word@entry=0x59eeec9e12f8, expected=expected@entry=0, clockid=clockid@entry=0, abstime=abstime@entry=0x0, private=private@entry=0)
    at ./nptl/futex-internal.c:139
#3  0x00007a136e6a0b6d in __pthread_cond_wait_common (cond=0x59eeec9e12d0, mutex=0x59eeec9e12a0, clockid=0, abstime=0x0) at ./nptl/pthread_cond_wait.c:503
#4  ___pthread_cond_wait (cond=0x59eeec9e12d0, mutex=0x59eeec9e12a0) at ./nptl/pthread_cond_wait.c:627
#5  0x000059eec6dcc9e1 in event_wait (ev=0x59eeec9e12a0) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/utils/wait_for_event.c:84
#6  0x000059eec6dcc764 in prvSuspendSelf (thread=0x59eec6e56418 <stackTzCtrl+131032>) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:604
#7  0x000059eec6dcc736 in prvSwitchThread (pxThreadToResume=0x59eec6e362f8 <uxIdleTaskStack.1+131032>, pxThreadToSuspend=0x59eec6e56418 <stackTzCtrl+131032>)
    at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:582
#8  0x000059eec6dcc387 in prvPortYieldFromISR () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:377
#9  0x000059eec6dcc39c in vPortYield () at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:385
#10 0x000059eec6dc5271 in vTaskDelay (xTicksToDelay=2) at /data/code/FreeRTOS/FreeRTOS/Source/tasks.c:2477
#11 0x000059eec6da980e in TzCtrl (pvParameters=0x0) at /data/code/FreeRTOS/FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/kernelports/FreeRTOS/trcKernelPort.c:548
#12 0x000059eec6dcc6c2 in prvWaitForStart (pvParams=0x59eec6e56418 <stackTzCtrl+131032>) at /data/code/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c:546
#13 0x00007a136e6a1e2e in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#14 0x00007a136e733a4c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78
(gdb) fr 3
#3  0x00007a136e6a0b6d in __pthread_cond_wait_common (cond=0x59eeec9e12d0, mutex=0x59eeec9e12a0, clockid=0, abstime=0x0) at ./nptl/pthread_cond_wait.c:503
warning: 503    ./nptl/pthread_cond_wait.c: Nie ma takiego pliku ani katalogu
(gdb) p mutex.__
__align  __data   __size
(gdb) p mutex.__data
$4 = {__lock = 0, __count = 1, __owner = 0, __nusers = 1, __kind = 144, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}}
(gdb)