GH-95289: Always call uncancel() when parent cancellation is requested (#95602)

Co-authored-by: Guido van Rossum <guido@python.org>
This commit is contained in:
Kumar Aditya 2022-08-04 19:27:44 +05:30 committed by GitHub
parent 42b102bbf9
commit 2fef27589e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 9 deletions

View file

@ -3,7 +3,7 @@
import asyncio
import contextvars
import contextlib
from asyncio import taskgroups
import unittest
@ -741,6 +741,37 @@ class TestTaskGroup(unittest.IsolatedAsyncioTestCase):
self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError})
async def test_taskgroup_context_manager_exit_raises(self):
# See https://github.com/python/cpython/issues/95289
class CustomException(Exception):
pass
async def raise_exc():
raise CustomException
@contextlib.asynccontextmanager
async def database():
try:
yield
finally:
raise CustomException
async def main():
task = asyncio.current_task()
try:
async with taskgroups.TaskGroup() as tg:
async with database():
tg.create_task(raise_exc())
await asyncio.sleep(1)
except* CustomException as err:
self.assertEqual(task.cancelling(), 0)
self.assertEqual(len(err.exceptions), 2)
else:
self.fail('CustomException not raised')
await asyncio.create_task(main())
if __name__ == "__main__":
unittest.main()