# Restart FreeRTOS kernel on POSIX port

**URL:** <https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584>\
**Category:** Libraries\
**Created:** [March 2, 2022, 7:30am UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584 "2022-03-02T07:30:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mappy89](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mappy89](https://forums.freertos.org/u/mappy89)\
**Post date:** [March 2, 2022, 7:30am UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/1 "2022-03-02T07:30:02Z")

</div>

Hello!

I’m using Ceedling to test the code contains FreeRTOS. After each ceedling test I need to end scheduler using vTaskEndScheduler() function to check results.

The problem is - when i restart scheduler with vTaskStartScheduler() function, already created tasks, queues and timers behaves not as I expect.  
How can i free all resources, which were allocated for tasks, timer etc on POSIX port?

---

<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:** [March 2, 2022, 6:52pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/2 "2022-03-02T18:52:00Z")

</div>

> [@mappy89](#):
>
> After each ceedling test I need to end scheduler using vTaskEndScheduler() function to check results.

I have not used seedling but why do you need to end scheduler between tests?

---

<div class="post-metadata">

**Author:** ![cmorganBE](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/cmorganbe/32/3872_2.png) [@cmorganBE](https://forums.freertos.org/u/cmorganBE)\
**Post date:** [October 11, 2023, 12:58pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/3 "2023-10-11T12:58:44Z")

</div>

If the scheduler isn’t stopped, the demo code indicates it will never return, how does one exit cleanly from the posix application in that case? Note that while calling **exit** could be an option, this will result in a ton of leaked memory that will cause trouble with valgrind.

```auto

    /* Start the scheduler itself. */
    vTaskStartScheduler();

    /* Should never get here unless there was not enough heap space to create
     * the idle and other system tasks. */
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![johnboiles](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/johnboiles/32/3927_2.png) [@johnboiles](https://forums.freertos.org/u/johnboiles)\
**Post date:** [November 2, 2023, 4:54pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/4 "2023-11-02T16:54:18Z")

</div>

I investigated this a bit this week. It’s clear to me from the code that FreeRTOS does not like to be started, stopped, and restarted. Here are a few issues I noticed:

- Many of FreeRTOS’s global variables aren’t in a good state after stopping and restarting (e.g.`xSchedulerEnd` is not reset to True)
- The use of `pthread_once` to call `prvSetupSignalsAndSchedulerPolicy` will only happen once per execution (not on a stop/start of FreeRTOS)
- Running tasks are not killed when the scheduler is stopped (ie `vPortCancelThread` is not called on all tasks, only on the timer and idle tasks)
- I believe the signal handling might be messed up on subsequent runs, though I struggled to debug this one.

That said, I’m getting reliable graceful shutdowns when only starting/stopping once. Here’s what I do:

```auto
    // The FreeRTOS tick handler happens via SIGALRM, and it also kicks off preemptive task
    // switching, which is not what we want on the main thread, since it's not a real FreeRTOS task.
    // Just disable SIGALRM on this thread so that it only happens on FreeRTOS tasks.
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, SIGALRM);
    pthread_sigmask(SIG_BLOCK, &mask, NULL);

    // Start the FreeRTOS scheduler
    std::thread schedulerThread([]() {
        xTaskCreate(appMainTask, "app_main", 10000, NULL, 1, NULL);
        vTaskStartScheduler();
        log_i("Scheduler thread done");
    });

    while (mainLoop()) {
        usleep(1000000.0 / 60.0);
    }

    log_i("Shutting down");

    vTaskEndScheduler();
    schedulerThread.join();

```

I had to remove these two lines from `vTaskEndScheduler()` for this to work (not sure how these are supposed to work):

```auto
    xCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
    prvSuspendSelf( xCurrentThread );

```

Maybe a good starting point would be to add code to `vPortEndScheduler` to clean up all the resources, then at least valgrind results would be cleaner, and then that could be expanded into better start/stop/start behavior if someone was sufficiently motivated.

---

<div class="post-metadata">

**Author:** ![cmorganBE](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/cmorganbe/32/3872_2.png) [@cmorganBE](https://forums.freertos.org/u/cmorganBE)\
**Post date:** [November 29, 2023, 11:09pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/5 "2023-11-29T23:09:37Z")

</div>

PR opened here to address signal deadlock issues, shutting down the scheduler cleanly, and being able to restart the scheduler: [Improve POSIX port functionality by cmorganBE · Pull Request #914 · FreeRTOS/FreeRTOS-Kernel · GitHub](https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/914)

@johnboiles for sure the signals were causing issues. The approach of using signals to interrupt execution is neat but also was causing deadlocks.

Example applications used to debug and test are here: [GitHub - cmorganBE/freertos\_posix](https://github.com/cmorganBE/freertos_posix)

---

<div class="post-metadata">

**Author:** ![cmorganBE](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/cmorganbe/32/3872_2.png) [@cmorganBE](https://forums.freertos.org/u/cmorganBE)\
**Post date:** [November 29, 2023, 11:10pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/6 "2023-11-29T23:10:43Z")

</div>

the cleanup in this PR also gets valgrind running cleanly.

---

<div class="post-metadata">

**Author:** ![cmorganBE](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/cmorganbe/32/3872_2.png) [@cmorganBE](https://forums.freertos.org/u/cmorganBE)\
**Post date:** [November 29, 2023, 11:11pm UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/7 "2023-11-29T23:11:21Z")

</div>

@aggarg thoughts on if/who I would reach out to to get review on the PR?

---

<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:** [November 30, 2023, 5:56am UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/8 "2023-11-30T05:56:50Z")

</div>

I will share with someone in my team. Thank you for your change.

---

<div class="post-metadata">

**Author:** ![skptak](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/skptak/32/3981_2.png) [@skptak](https://forums.freertos.org/u/skptak)\
**Post date:** [November 30, 2023, 10:11am UTC](https://forums.freertos.org/t/restart-freertos-kernel-on-posix-port/14584/9 "2023-11-30T10:11:08Z")

</div>

Hey @cmorganBE, thanks for raising your PR! I’ve started reviewing it and have left some comments on the PR itself
