Comments (3)
Hi @yellowbeeHYF ,
I have looked into this one carefully as we are always concerned about things that look like concurrency problems. Thank you for doing a very thorough report, that made it a lot easier to investigate!
My conclusion is that this is not actually a bug in FreeRTOS, but rather an incorrect usage of the API. I also think that we can definitely be more specific about the limitations here so we will be updating the documentation in the API reference to call this case out specifically for vTaskDelete.
The root cause of the crash you are seeing is that vTaskDelete is called more than once for the same task. If you simply called vTaskDelete twice in a row for the same task you would see the same behavior. The kernel cannot reasonably keep track of old deleted tasks to prevent you from deleting them again as this would cause a significant performance hit and also does not solve the memory re-use case.
Task handles are just pointers to the location of the TCB, and an even worse scenario would be if TaskA were to kill itself, a new task was created using the same memory, resulting in the same TCB pointer, and then task B attempts to delete TaskA, but now it is actually deleting a different task entirely. This is the memory re-use case.
If you think about it this is really a classic race condition which applications always have to guard against. If a task called vPortFree on a block of memory, and a different task called vPortFree on a pointer holding the same address you would of course see a similar situation.
My recommendation would be that if you are going to delete a task from more than one context you need to protect against the deletion using a mutex or semaphore. In this case you could keep the task handle in a shared location which is protected by a mutex, and let the deleting task set the pointer to NULL before delting the task. This way when TaskB decides that it wants to delete TaskA it will find the pointer set to NULL and know that task A has already been deleted, which means it will not call vTaskDelete at all.
An even better solution is to never delete a task from more than one context. This can be achieved by sending a message to the task to delete itself (you could use direct to task notifications or even atomics to achieve this), or alternatively let the task send a message to a coordinating task that does all of the deleting in a single context.
Cobus
from freertos.
Another solution to add to the list of recommendations provided by @cobusve , is to ref-count the deletions using an atomic variable.
This way you will ensure that the last owner of the task will be the only one to actually call delete.
An example can be found here:
https://github.com/aws/amazon-freertos/blob/master/libraries/abstractions/secure_sockets/lwip/iot_secure_sockets.c#L189-L203
Alfred
from freertos.
I am closing this issue as there has been no response about the assessment.
from freertos.
Related Issues (20)
- [BUG] Posix_GCC demo emits stack warnings HOT 5
- [BUG] HOT 4
- [BUG] Posix Gcc Demo Create Task Failed using cmake HOT 2
- [BUG] va_arg process error HOT 4
- [BUG] FULL_DEMO of CORTEX_M3_MPS2_QEMU_GCC doesn't work sometimes HOT 4
- Task suspending itself can remove itself from termination list HOT 6
- [DOC] Missing dependencies on Posix/Linux Simulator Demo for FreeRTOS HOT 2
- [Feature Request] Ability to Query Task Notification Status (pending or not) HOT 7
- FreeRTOS CLI - parameter with spaces HOT 2
- [BUG] Cellular demo doesn't build on windows HOT 7
- [BUG] Incorrect use of memset() in demo code HOT 2
- [Feature Request] Interruptible Blocking Statements HOT 4
- [DOC]
- [DOC] Where are the best practices? HOT 3
- [BUG] *** No rule to make target 'output/tasks.o', needed by 'output/RTOSDemo.out'. Stop. HOT 1
- [DOC] I believe that the TCP/IP stack can use static allocations, contrary to a statement in the tutorial HOT 2
- [BUG] Segment fault error caused by pxTopOfStack HOT 15
- [BUG] When vApplicationTickHook is in use, Cortex R5 UltraScale should save FPU context in tick handler HOT 11
- [BUG] Out of date text file HOT 1
- [BUG] Preprocessor sequence prevents plain text connections
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freertos.