-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-128552: fix refcycles in eager task creation #128553
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
Merged
kumaraditya303
merged 13 commits into
python:main
from
graingert:fix-taskgroup-eager-refcycle
Jan 7, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c82f30b
gh-128552: fix refcycles in eager task creation
graingert 8a4eb56
Update Lib/asyncio/base_events.py
graingert 35a38e4
Update Lib/asyncio/taskgroups.py
graingert afd3053
📜🤖 Added by blurb_it.
blurb-it[bot] 9700ebf
Rename 2025-01-06-18-41-08.gh-issue-gh-128552.fV-f8j.rst to 2025-01-0…
graingert fe83add
Update Misc/NEWS.d/next/Library/2025-01-06-18-41-08.gh-issue-128552.f…
graingert a1f76df
Merge branch 'main' into fix-taskgroup-eager-refcycle
graingert 7c966d0
add a version of the test with weakrefs
graingert 89362f5
Merge branch 'main' into fix-taskgroup-eager-refcycle
graingert cf5cfe3
simplify and comment
graingert 18b6ea9
Update Lib/asyncio/base_events.py
graingert ca71935
restore _set_event_loop_policy(None)
graingert cc39717
remove type annotations
graingert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Adapted with permission from the EdgeDB project; | ||
# license: PSFL. | ||
|
||
import weakref | ||
import sys | ||
import gc | ||
import asyncio | ||
|
@@ -38,7 +39,25 @@ def no_other_refs(): | |
return [coro] | ||
|
||
|
||
class TestTaskGroup(unittest.IsolatedAsyncioTestCase): | ||
def set_gc_state(enabled): | ||
was_enabled = gc.isenabled() | ||
if enabled: | ||
gc.enable() | ||
else: | ||
gc.disable() | ||
return was_enabled | ||
|
||
|
||
@contextlib.contextmanager | ||
def disable_gc(): | ||
was_enabled = set_gc_state(enabled=False) | ||
try: | ||
yield | ||
finally: | ||
set_gc_state(enabled=was_enabled) | ||
|
||
|
||
class BaseTestTaskGroup: | ||
|
||
async def test_taskgroup_01(self): | ||
|
||
|
@@ -832,15 +851,15 @@ async def test_taskgroup_without_parent_task(self): | |
with self.assertRaisesRegex(RuntimeError, "has not been entered"): | ||
tg.create_task(coro) | ||
|
||
def test_coro_closed_when_tg_closed(self): | ||
async def test_coro_closed_when_tg_closed(self): | ||
async def run_coro_after_tg_closes(): | ||
async with taskgroups.TaskGroup() as tg: | ||
pass | ||
coro = asyncio.sleep(0) | ||
with self.assertRaisesRegex(RuntimeError, "is finished"): | ||
tg.create_task(coro) | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(run_coro_after_tg_closes()) | ||
|
||
await run_coro_after_tg_closes() | ||
|
||
async def test_cancelling_level_preserved(self): | ||
async def raise_after(t, e): | ||
|
@@ -965,6 +984,30 @@ async def coro_fn(): | |
self.assertIsInstance(exc, _Done) | ||
self.assertListEqual(gc.get_referrers(exc), no_other_refs()) | ||
|
||
|
||
async def test_exception_refcycles_parent_task_wr(self): | ||
"""Test that TaskGroup deletes self._parent_task and create_task() deletes task""" | ||
tg = asyncio.TaskGroup() | ||
exc = None | ||
|
||
class _Done(Exception): | ||
pass | ||
|
||
async def coro_fn(): | ||
async with tg: | ||
raise _Done | ||
|
||
with disable_gc(): | ||
try: | ||
async with asyncio.TaskGroup() as tg2: | ||
task_wr = weakref.ref(tg2.create_task(coro_fn())) | ||
except* _Done as excs: | ||
exc = excs.exceptions[0].exceptions[0] | ||
|
||
self.assertIsNone(task_wr()) | ||
self.assertIsInstance(exc, _Done) | ||
self.assertListEqual(gc.get_referrers(exc), no_other_refs()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it was later changed that coro doesn't hold ref to locals, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah its not merged yet #125640 |
||
|
||
async def test_exception_refcycles_propagate_cancellation_error(self): | ||
"""Test that TaskGroup deletes propagate_cancellation_error""" | ||
tg = asyncio.TaskGroup() | ||
|
@@ -998,5 +1041,16 @@ class MyKeyboardInterrupt(KeyboardInterrupt): | |
self.assertListEqual(gc.get_referrers(exc), no_other_refs()) | ||
|
||
|
||
class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase): | ||
loop_factory = asyncio.EventLoop | ||
|
||
class TestEagerTaskTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase): | ||
@staticmethod | ||
def loop_factory(): | ||
loop = asyncio.EventLoop() | ||
loop.set_task_factory(asyncio.eager_task_factory) | ||
return loop | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-01-06-18-41-08.gh-issue-128552.fV-f8j.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix cyclic garbage introduced by :meth:`asyncio.loop.create_task` and :meth:`asyncio.TaskGroup.create_task` holding a reference to the created task if it is eager. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.