Skip to content

Bugfix/workqueue crash#62

Closed
usman-pak1991 wants to merge 19 commits into
michaelbecker:masterfrom
Selectronic-AU:bugfix/workqueue_crash
Closed

Bugfix/workqueue crash#62
usman-pak1991 wants to merge 19 commits into
michaelbecker:masterfrom
Selectronic-AU:bugfix/workqueue_crash

Conversation

@usman-pak1991
Copy link
Copy Markdown

@usman-pak1991 usman-pak1991 commented May 12, 2026

This PR address the issue of Hardfault due to Thread exit of WorkQueue.

Exiting the thread via break causes the task function to return, which triggers:

  1. prvTaskExitError() (see port.c line 228-249) - FreeRTOS task functions must never return
  2. The Idle task then attempts cleanup via prvCheckTasksWaitingTermination() (see tasks.c line 6074-6145)
    which tries to free the TCB while it may still be scheduled or in use
  3. Results in hardfault due to race condition in resource cleanup

In C++, the situation is more complex than plain C:

  • The task is owned by a C++ object (cpp_freertos::Thread wrapper)
  • Task deletion (vTaskDelete) and object destruction (~Thread) are separate events
  • The ~Thread() destructor calls vTaskDelete(handle) (see cthread.cpp line 326-362)
  • If the task returns before destruction, vTaskDelete() may operate on an invalid/zombie task
  • Even calling vTaskDelete(NULL) from within Run() causes the task to be deleted while
    the destructor still holds a reference to it, leading to double-free or use-after-free

Therefore, the safest approach for a C++ wrapper task is to keep the thread alive but allowing the destructor to properly clean up when the object is destroyed.

iridley-selectronic and others added 19 commits August 1, 2024 21:37
Updates the EventGroup class to use FreeRTOS StaticEventGroup data
structures internally when static allocation support is enabled.
Updates the MemoryPool class to accept a preallocated memory buffer when
static allocation support is enabled.
Updates the MutexStandard & MutexRecursive classes to use FreeRTOS
StaticSemaphore_t internally when static allocation support is
configured.
Updates the Queue, Deque and BinaryQueue classes to use FreeRTOS
StaticQueue_t internally when static allocation support is configured.
Updates the Semaphore, BinarySemaphore & CountingSemaphore classes to
use FreeRTOS StaticSemaphore_t internally when static allocation support
is configured.
Updates the ReadWriteLock class to use FreeRTOS StaticSemaphore_t
internally when static allocation support is configured.
Updates the Timer class to use FreeRTOS StaticTimer_t when static
allocation support is configured.
Updates the Thread class to use FreeRTOS xTaskCreateStatic thread
creation with a preallocated static buffer when static allocation
support is configured.
Updates the Tasklet class to use FreeRTOS StaticSemaphore_t internally
when static allocation support is configured.
Updates the Workqueue class to use of static thread, semaphore and queue
variants when static allocation support is configured.
feature/static_allocation

# Teamwork Ticket
- [X] [Teamwork Ticket](https://selectspace.teamwork.com/app/tasks/31944281)

# Design Notes
- [X] See [freertos-addons wiki page](https://github.com/Selectronic-AU/wiki/blob/master/3rd-party/FreeRTOS/freertos-addons.md) for details of approach to supporting static allocation.

# Testing
- [X] *TODO*: repurpose demo applications (if possible using googletest) as unit tests and port to target (STMH7).

# Related PRs
- [X] Refer to [PR #1](#1) for previous experimental work.
* add started guard before deletion [wip]

* SW-115  port battery driver to comms

* fixing tabs to spaces

* fix for preventing deletion of the calling thread. and removing code smell and properly fixing the inclusion of winapi in freertos mingw port

---------

Co-authored-by: Ghufran Khan <gkhan@selectronic.com.au>
Co-authored-by: Ghufran Khan <153471261+gkhan-selectronic@users.noreply.github.com>
…ub.com:Selectronic-AU/freertos-addons and open bug/WorkQueue-Crash-Fix
@usman-pak1991
Copy link
Copy Markdown
Author

Minore Updates Required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants