Fix event loop memory leak from cached root task (#1203)#1229
Closed
uttam12331 wants to merge 2 commits into
Closed
Fix event loop memory leak from cached root task (#1203)#1229uttam12331 wants to merge 2 commits into
uttam12331 wants to merge 2 commits into
Conversation
find_root_task() cached the root asyncio.Task with a strong reference in the per-loop _run_vars dict. Since _run_vars is a WeakKeyDictionary keyed by the event loop, and the cached task references that same loop, the loop was kept alive by its own dict value and never garbage collected. Every anyio.run() that used to_thread.run_sync() (which calls find_root_task) therefore leaked its event loop, root task and result, growing memory without bound. Cache a weakref to the root task instead, so it no longer pins the loop. Fixes agronholm#1203
for more information, see https://pre-commit.ci
Collaborator
|
Looks like there is already a PR, @uttam12331 did you take a look at #1211? |
Owner
|
The bot also deleted the PR template which is grounds for dismissal anyway. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1203
Problem
find_root_task()(asyncio backend) caches the root task via_root_task.set(task). That RunVar lives in the per-loop dict of_run_vars, which is aWeakKeyDictionarykeyed by the event loop.Because the cached
Taskholds a strong reference back to its loop, the loopis kept alive by a value stored under its own weak key — so the weak key never
clears. Every
anyio.run()that goes throughfind_root_task()(e.g. anythingusing
to_thread.run_sync()) permanently leaks its event loop, root task andthe coroutine's result. In a loop this grows memory without bound and
gc.collect()cannot reclaim it.Reproduction:
Fix
Cache a
weakrefto the root task instead of a strong reference, so thecached value no longer pins the loop. While the loop is running the root task
is alive, so the weakref resolves; once the loop is done, the weakref clears
and everything is collected.
Verification
test_anyio_run_does_not_leak_event_loop(usesclear_cache-free gccheck), which fails on
masterand passes with this change.unrelated Windows/
psutilenv issues that also fail on an unmodified tree).ruffclean; changelog updated.