Thanks to visit codestin.com
Credit goes to github.com

Skip to content

"cannot reuse already awaited coroutine" when yielding from TaskGroup() #34

@sean-anderson-seco

Description

@sean-anderson-seco

The following program

import asyncio
from taskgroup import TaskGroup

async def bar():
    async with TaskGroup():
        yield

gen_obj = bar()
async def setup():
    return await gen_obj.__anext__()

asyncio.run(setup())

Fails with the following error(s):

an error occurred during closing of asynchronous generator <async_generator object bar at 0x7fb3dadccee0>
asyncgen: <async_generator object bar at 0x7fb3dadccee0>
  + Exception Group Traceback (most recent call last):
  |   File "/path/to/taskgroup/taskgroup/install.py", line 125, in install_uncancel
  |     yield
  |   File "/usr/lib/python3.8/contextlib.py", line 662, in __aexit__
  |     cb_suppress = await cb(*exc_details)
  |   File "/path/to/taskgroup/taskgroup/taskgroups.py", line 120, in __aexit__
  |     return await self._aexit(et, exc)
  |   File "/path/to/taskgroup/taskgroup/taskgroups.py", line 218, in _aexit
  |     raise BaseExceptionGroup(
  | exceptiongroup.BaseExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "tests/loop_test.py", line 6, in bar
    |     yield
    | GeneratorExit
    +------------------------------------

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tests/loop_test.py", line 6, in bar
    yield
  File "/path/to/taskgroup/taskgroup/taskgroups.py", line 381, in __aexit__
    return await self.__stack.__aexit__(exc_type, exc_val, exc_tb)
  File "/usr/lib/python3.8/contextlib.py", line 679, in __aexit__
    raise exc_details[1]
  File "/usr/lib/python3.8/contextlib.py", line 662, in __aexit__
    cb_suppress = await cb(*exc_details)
  File "/usr/lib/python3.8/contextlib.py", line 189, in __aexit__
    await self.gen.athrow(typ, value, traceback)
  File "/path/to/taskgroup/taskgroup/install.py", line 128, in install_uncancel
    await _async_yield(UNCANCEL_DONE)
  File "/path/to/taskgroup/taskgroup/install.py", line 46, in _async_yield
    return (yield v)
RuntimeError: Task got bad yield: <object object at 0x7fb3daeba2c0>
an error occurred during closing of asynchronous generator <async_generator object install_uncancel at 0x7fb3dad161f0>
asyncgen: <async_generator object install_uncancel at 0x7fb3dad161f0>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
RuntimeError: cannot reuse already awaited coroutine

During handling of the above exception, another exception occurred:

RuntimeError: aclose(): asynchronous generator is already running
Traceback (most recent call last):
  File "tests/loop_test.py", line 12, in <module>
    asyncio.run(setup())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
RuntimeError: cannot reuse already awaited coroutine

This error does not occur when using asyncio's TaskGroup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions