Skip to content

Fix TaskManager finished tasks memory leak - #266

Merged
ThomasFarstrike merged 1 commit into
MicroPythonOS:mainfrom
fdb:task-manager-completed-task-cleanup
Aug 24, 2026
Merged

ThomasFarstrike merged 1 commit into
MicroPythonOS:mainfrom
fdb:task-manager-completed-task-cleanup

Conversation

@fdb

@fdb fdb commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

This PR fixes a memory leak in TaskManager. TaskManager previously retained every task created through create_task(), which also kept completed coroutine frames and their captured objects alive indefinitely.

We fix this by removing managed tasks from TaskManager.task_list as soon as they finish (using try/finally) so completed coroutine frames and captured objects can be garbage collected. Preserve task results, exceptions, cancellation, and running-task tracking.

We added multiple tests in test_task_manager.py to check the behavior and catch regressions.

This fixes #261.

@fdb fdb changed the title Fix TaskManager completed task retention Fix TaskManager memory leak Aug 21, 2026
@fdb fdb changed the title Fix TaskManager memory leak Fix TaskManager finished tasks memory leak Aug 21, 2026
Remove managed tasks from TaskManager.task_list as soon as they finish so
completed coroutine frames and captured objects can be garbage collected.
Preserve task results, exceptions, cancellation, and running-task tracking.
@ThomasFarstrike
ThomasFarstrike force-pushed the task-manager-completed-task-cleanup branch from 82c7674 to 23b8efc Compare August 24, 2026 11:15
@ThomasFarstrike
ThomasFarstrike merged commit 2364341 into MicroPythonOS:main Aug 24, 2026
10 checks passed
@ThomasFarstrike

Copy link
Copy Markdown
Contributor

Another step in the right direction!

@fdb
fdb deleted the task-manager-completed-task-cleanup branch August 24, 2026 12:46
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.

TaskManager retains completed tasks indefinitely (memory leak)

2 participants