diff options
author | Thomas Grainger <tagrain@gmail.com> | 2025-01-07 11:44:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-07 11:44:57 (GMT) |
commit | 61b9811ac6843e22b5896ef96030d421b79cd892 (patch) | |
tree | 3f29fef0b3d767a290bd51c4b1429fb581af3885 /Lib/asyncio | |
parent | 6ea04da27036eaa69d65150148bb8c537d9beacf (diff) | |
download | cpython-61b9811ac6843e22b5896ef96030d421b79cd892.zip cpython-61b9811ac6843e22b5896ef96030d421b79cd892.tar.gz cpython-61b9811ac6843e22b5896ef96030d421b79cd892.tar.bz2 |
gh-128552: fix refcycles in eager task creation (#128553)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/base_events.py | 7 | ||||
-rw-r--r-- | Lib/asyncio/taskgroups.py | 7 |
2 files changed, 12 insertions, 2 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 9e6f6e3..6e6e5aa 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -477,7 +477,12 @@ class BaseEventLoop(events.AbstractEventLoop): task.set_name(name) - return task + try: + return task + finally: + # gh-128552: prevent a refcycle of + # task.exception().__traceback__->BaseEventLoop.create_task->task + del task def set_task_factory(self, factory): """Set a task factory that will be used by loop.create_task(). diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 9fa772c..8af199d 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -205,7 +205,12 @@ class TaskGroup: else: self._tasks.add(task) task.add_done_callback(self._on_task_done) - return task + try: + return task + finally: + # gh-128552: prevent a refcycle of + # task.exception().__traceback__->TaskGroup.create_task->task + del task # Since Python 3.8 Tasks propagate all exceptions correctly, # except for KeyboardInterrupt and SystemExit which are |