Skip to content
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

TaskGroup around yield in fixture doesn't terminate with exceptions #1083

Open
sawalls opened this issue Mar 25, 2025 · 1 comment
Open

TaskGroup around yield in fixture doesn't terminate with exceptions #1083

sawalls opened this issue Mar 25, 2025 · 1 comment

Comments

@sawalls
Copy link

sawalls commented Mar 25, 2025

I tried to make a fixture to throw an exception after a certain amount of time to catch deadlocks in my tests, but the exception isn't causing the TaskGroup to error out.

Here is a minimal example fixture that interacts with a literally trivial test
https://github.com/sawalls/pytest-asyncio-example/blob/main/tests/conftest.py

@fixture(scope="function")
async def async_buddy():
    mytasks = set()
    async def forever_loop():
        while True:
            await sleep(1)

    async def exception_task():
        await sleep(5)
        raise Exception("This is an exception")

    async with TaskGroup() as tg:
        mytasks.add(tg.create_task(forever_loop()))
        mytasks.add(tg.create_task(exception_task()))
        yield

---

async def test_example(async_buddy):
    pass

It's inspired by the recipe in the python docs to terminate a TaskGroup:
https://docs.python.org/3/library/asyncio-task.html#terminating-a-task-group

Expected behavior:

  1. Make TaskGroup
  2. Make Tasks
  3. Yield to test, which returns immediately
  4. Enter context manager waiting for tasks
  5. Get Exception from exception task, cancel all tasks, and throw out of the fixture

Observed behavior:

  1. Make TaskGroup
  2. Make Tasks
  3. Yield to test, which returns immediately (so at this point the test has reported success)
  4. Enter context manager waiting for tasks
  5. Hangs indefinitely

Please let me know if there's anything about my intentions I can make more clear or documents I should read to get a better understanding.

Also, if you want to direct me at some code to understand the library's understanding of this, I may be able to try and reason about it.

@sawalls
Copy link
Author

sawalls commented Mar 25, 2025

Wanted to note this is similar to #708 but that had multiple processes involved, which confounded my understanding a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant