Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(task): Fix memory leak from task::run_on_core_nonblocking #388

Merged
merged 1 commit into from
Feb 28, 2025

Conversation

finger563
Copy link
Contributor

@finger563 finger563 commented Feb 28, 2025

Description

  • Remove call to vTaskDelete(nullptr) from the end of the thread function

Motivation and Context

vTaskDelete(nullptr) was added as part of #353 based on spec. It was erroneously causing the detached thread to leak memory for each call to run_on_core_nonblocking. Using esp heap tracing I was able to determine that it did leak memory, while the updated code does not leak memory. Additionally, I used the TaskMonitor to ensure that the tasks are properly destroyed and are no longer registered within the system.

Note: as mentioned in the esp-docs, there will be some leaks reported which are benign. What is important is that as we vary the number of times that run_on_core_nonblocking is called, that the reported heap trace does not change.

For reference, here is the reported trace when calling run_on_core_nonblocking twice within a loop which runs 10x. This is the same report as if you ran the loop 1x or 5x.

====== Heap Trace Summary ======
Mode: Heap Trace Leaks
144 bytes 'leaked' in trace (5 allocations)
records: 5 (100 capacity, 15 high water mark)
hashmap: 512 capacity (36 hits, 0 misses)
total allocations: 41
total frees: 36
================================

How has this been tested?

  • Build and run task/example on QtPy ESP32s3 and ensure it works still.
  • Build and run modified task/example on QtPy ESP32s3 which as freertos runtime stats (for task monitoring) and heap tracing enabled. Dump the heap trace output to ensure the residual memory allocated is not dependent on the number of times run_on_core_nonblocking was called. Confirmed that when including vTaskDelay(nullptr) the memory allocated (and leaked) is dependent on the number of times it is called.

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

* Remove call to `vTaskDelete(nullptr)` from the end of the thread function

`vTaskDelete(nullptr)` was added as part of #353 based on spec. It was erroneously causing the detached thread to leak memory for each call to `run_on_core_nonblocking`. Using esp heap tracing I was able to determine that it did leak memory, while the updated code does not leak memory. Additionally, I used the `TaskMonitor` to ensure that the tasks are properly destroyed and are no longer registered within the system.

* Build and run `task/example` on QtPy ESP32s3 and ensure it works still.
* Build and run modified `task/example` on QtPy ESP32s3 which as freertos runtime stats (for task monitoring) and heap tracing enabled. Dump the heap trace output to ensure the residual memory allocated is not dependent on the number of times `run_on_core_nonblocking` was called. Confirmed that when including `vTaskDelay(nullptr)` the memory allocated (and leaked) is dependent on the number of times it is called.
@finger563 finger563 self-assigned this Feb 28, 2025
@finger563 finger563 added bug Something isn't working tasks memory leak labels Feb 28, 2025
Copy link

✅Static analysis result - no issues found! ✅

@finger563 finger563 merged commit f43eb87 into main Feb 28, 2025
73 checks passed
@finger563 finger563 deleted the fix/task-run-on-core-nonblocking-memory-leak branch February 28, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working memory leak tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant