Code Monkey home page Code Monkey logo

Comments (3)

cobusve avatar cobusve commented on June 15, 2024

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.

alfred2g avatar alfred2g commented on June 15, 2024

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.

cobusve avatar cobusve commented on June 15, 2024

I am closing this issue as there has been no response about the assessment.

from freertos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.