pytest-dev / pytest-dev/pytest-asyncio
TaskGroup around yield in fixture doesn't terminate with exceptions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 5h 35m
- Merged PRs (30d)
- 9
Description
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:
- Make TaskGroup
- Make Tasks
- Yield to test, which returns immediately
- Enter context manager waiting for tasks
- Get Exception from exception task, cancel all tasks, and throw out of the fixture
Observed behavior:
- Make TaskGroup
- Make Tasks
- Yield to test, which returns immediately (so at this point the test has reported success)
- Enter context manager waiting for tasks
- 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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal fixture and test in tests/conftest.py from the linked example, then run it to reproduce the hang. Read the asyncio TaskGroup termination recipe and trace how pytest-asyncio handles async-generator fixture teardown. Done means the delayed exception cancels the remaining task and causes the fixture or test to fail instead of hanging indefinitely.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100