mirror of
https://github.com/python/cpython.git
synced 2025-08-09 11:29:45 +00:00
gh-124958: fix asyncio.TaskGroup and _PyFuture refcycles (#124959)
This commit is contained in:
parent
42b8e52de4
commit
32d457941e
5 changed files with 147 additions and 15 deletions
|
@ -194,8 +194,7 @@ class Future:
|
||||||
the future is done and has an exception set, this exception is raised.
|
the future is done and has an exception set, this exception is raised.
|
||||||
"""
|
"""
|
||||||
if self._state == _CANCELLED:
|
if self._state == _CANCELLED:
|
||||||
exc = self._make_cancelled_error()
|
raise self._make_cancelled_error()
|
||||||
raise exc
|
|
||||||
if self._state != _FINISHED:
|
if self._state != _FINISHED:
|
||||||
raise exceptions.InvalidStateError('Result is not ready.')
|
raise exceptions.InvalidStateError('Result is not ready.')
|
||||||
self.__log_traceback = False
|
self.__log_traceback = False
|
||||||
|
@ -212,8 +211,7 @@ class Future:
|
||||||
InvalidStateError.
|
InvalidStateError.
|
||||||
"""
|
"""
|
||||||
if self._state == _CANCELLED:
|
if self._state == _CANCELLED:
|
||||||
exc = self._make_cancelled_error()
|
raise self._make_cancelled_error()
|
||||||
raise exc
|
|
||||||
if self._state != _FINISHED:
|
if self._state != _FINISHED:
|
||||||
raise exceptions.InvalidStateError('Exception is not set.')
|
raise exceptions.InvalidStateError('Exception is not set.')
|
||||||
self.__log_traceback = False
|
self.__log_traceback = False
|
||||||
|
|
|
@ -66,6 +66,20 @@ class TaskGroup:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def __aexit__(self, et, exc, tb):
|
async def __aexit__(self, et, exc, tb):
|
||||||
|
tb = None
|
||||||
|
try:
|
||||||
|
return await self._aexit(et, exc)
|
||||||
|
finally:
|
||||||
|
# Exceptions are heavy objects that can have object
|
||||||
|
# cycles (bad for GC); let's not keep a reference to
|
||||||
|
# a bunch of them. It would be nicer to use a try/finally
|
||||||
|
# in __aexit__ directly but that introduced some diff noise
|
||||||
|
self._parent_task = None
|
||||||
|
self._errors = None
|
||||||
|
self._base_error = None
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
async def _aexit(self, et, exc):
|
||||||
self._exiting = True
|
self._exiting = True
|
||||||
|
|
||||||
if (exc is not None and
|
if (exc is not None and
|
||||||
|
@ -126,25 +140,34 @@ class TaskGroup:
|
||||||
assert not self._tasks
|
assert not self._tasks
|
||||||
|
|
||||||
if self._base_error is not None:
|
if self._base_error is not None:
|
||||||
|
try:
|
||||||
raise self._base_error
|
raise self._base_error
|
||||||
|
finally:
|
||||||
|
exc = None
|
||||||
|
|
||||||
# Propagate CancelledError if there is one, except if there
|
# Propagate CancelledError if there is one, except if there
|
||||||
# are other errors -- those have priority.
|
# are other errors -- those have priority.
|
||||||
|
try:
|
||||||
if propagate_cancellation_error and not self._errors:
|
if propagate_cancellation_error and not self._errors:
|
||||||
|
try:
|
||||||
raise propagate_cancellation_error
|
raise propagate_cancellation_error
|
||||||
|
finally:
|
||||||
|
exc = None
|
||||||
|
finally:
|
||||||
|
propagate_cancellation_error = None
|
||||||
|
|
||||||
if et is not None and et is not exceptions.CancelledError:
|
if et is not None and et is not exceptions.CancelledError:
|
||||||
self._errors.append(exc)
|
self._errors.append(exc)
|
||||||
|
|
||||||
if self._errors:
|
if self._errors:
|
||||||
# Exceptions are heavy objects that can have object
|
|
||||||
# cycles (bad for GC); let's not keep a reference to
|
|
||||||
# a bunch of them.
|
|
||||||
try:
|
try:
|
||||||
me = BaseExceptionGroup('unhandled errors in a TaskGroup', self._errors)
|
raise BaseExceptionGroup(
|
||||||
raise me from None
|
'unhandled errors in a TaskGroup',
|
||||||
|
self._errors,
|
||||||
|
) from None
|
||||||
finally:
|
finally:
|
||||||
self._errors = None
|
exc = None
|
||||||
|
|
||||||
|
|
||||||
def create_task(self, coro, *, name=None, context=None):
|
def create_task(self, coro, *, name=None, context=None):
|
||||||
"""Create a new task in this group and return it.
|
"""Create a new task in this group and return it.
|
||||||
|
|
|
@ -640,6 +640,28 @@ class BaseFutureTests:
|
||||||
fut = self._new_future(loop=self.loop)
|
fut = self._new_future(loop=self.loop)
|
||||||
fut.set_result(Evil())
|
fut.set_result(Evil())
|
||||||
|
|
||||||
|
def test_future_cancelled_result_refcycles(self):
|
||||||
|
f = self._new_future(loop=self.loop)
|
||||||
|
f.cancel()
|
||||||
|
exc = None
|
||||||
|
try:
|
||||||
|
f.result()
|
||||||
|
except asyncio.CancelledError as e:
|
||||||
|
exc = e
|
||||||
|
self.assertIsNotNone(exc)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
def test_future_cancelled_exception_refcycles(self):
|
||||||
|
f = self._new_future(loop=self.loop)
|
||||||
|
f.cancel()
|
||||||
|
exc = None
|
||||||
|
try:
|
||||||
|
f.exception()
|
||||||
|
except asyncio.CancelledError as e:
|
||||||
|
exc = e
|
||||||
|
self.assertIsNotNone(exc)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(futures, '_CFuture'),
|
@unittest.skipUnless(hasattr(futures, '_CFuture'),
|
||||||
'requires the C _asyncio module')
|
'requires the C _asyncio module')
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# Adapted with permission from the EdgeDB project;
|
# Adapted with permission from the EdgeDB project;
|
||||||
# license: PSFL.
|
# license: PSFL.
|
||||||
|
|
||||||
|
import gc
|
||||||
import asyncio
|
import asyncio
|
||||||
import contextvars
|
import contextvars
|
||||||
import contextlib
|
import contextlib
|
||||||
|
@ -10,7 +10,6 @@ import unittest
|
||||||
|
|
||||||
from test.test_asyncio.utils import await_without_task
|
from test.test_asyncio.utils import await_without_task
|
||||||
|
|
||||||
|
|
||||||
# To prevent a warning "test altered the execution environment"
|
# To prevent a warning "test altered the execution environment"
|
||||||
def tearDownModule():
|
def tearDownModule():
|
||||||
asyncio.set_event_loop_policy(None)
|
asyncio.set_event_loop_policy(None)
|
||||||
|
@ -824,6 +823,95 @@ class TestTaskGroup(unittest.IsolatedAsyncioTestCase):
|
||||||
# We still have to await coro to avoid a warning
|
# We still have to await coro to avoid a warning
|
||||||
await coro
|
await coro
|
||||||
|
|
||||||
|
async def test_exception_refcycles_direct(self):
|
||||||
|
"""Test that TaskGroup doesn't keep a reference to the raised ExceptionGroup"""
|
||||||
|
tg = asyncio.TaskGroup()
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
class _Done(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with tg:
|
||||||
|
raise _Done
|
||||||
|
except ExceptionGroup as e:
|
||||||
|
exc = e
|
||||||
|
|
||||||
|
self.assertIsNotNone(exc)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
|
||||||
|
async def test_exception_refcycles_errors(self):
|
||||||
|
"""Test that TaskGroup deletes self._errors, and __aexit__ args"""
|
||||||
|
tg = asyncio.TaskGroup()
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
class _Done(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with tg:
|
||||||
|
raise _Done
|
||||||
|
except* _Done as excs:
|
||||||
|
exc = excs.exceptions[0]
|
||||||
|
|
||||||
|
self.assertIsInstance(exc, _Done)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
|
||||||
|
async def test_exception_refcycles_parent_task(self):
|
||||||
|
"""Test that TaskGroup deletes self._parent_task"""
|
||||||
|
tg = asyncio.TaskGroup()
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
class _Done(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def coro_fn():
|
||||||
|
async with tg:
|
||||||
|
raise _Done
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with asyncio.TaskGroup() as tg2:
|
||||||
|
tg2.create_task(coro_fn())
|
||||||
|
except* _Done as excs:
|
||||||
|
exc = excs.exceptions[0].exceptions[0]
|
||||||
|
|
||||||
|
self.assertIsInstance(exc, _Done)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
async def test_exception_refcycles_propagate_cancellation_error(self):
|
||||||
|
"""Test that TaskGroup deletes propagate_cancellation_error"""
|
||||||
|
tg = asyncio.TaskGroup()
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(-1):
|
||||||
|
async with tg:
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
except TimeoutError as e:
|
||||||
|
exc = e.__cause__
|
||||||
|
|
||||||
|
self.assertIsInstance(exc, asyncio.CancelledError)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
async def test_exception_refcycles_base_error(self):
|
||||||
|
"""Test that TaskGroup deletes self._base_error"""
|
||||||
|
class MyKeyboardInterrupt(KeyboardInterrupt):
|
||||||
|
pass
|
||||||
|
|
||||||
|
tg = asyncio.TaskGroup()
|
||||||
|
exc = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with tg:
|
||||||
|
raise MyKeyboardInterrupt
|
||||||
|
except MyKeyboardInterrupt as e:
|
||||||
|
exc = e
|
||||||
|
|
||||||
|
self.assertIsNotNone(exc)
|
||||||
|
self.assertListEqual(gc.get_referrers(exc), [])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix refcycles in exceptions raised from :class:`asyncio.TaskGroup` and the python implementation of :class:`asyncio.Future`
|
Loading…
Add table
Add a link
Reference in a new issue