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

During cleanup, should pytest-asyncio cancel outstanding tasks, shut down async generators, and shut down the thread pool, like asyncio.run() does? #222

Closed
SyntaxColoring opened this issue Jul 29, 2021 · 4 comments

Comments

@SyntaxColoring
Copy link
Contributor

SyntaxColoring commented Jul 29, 2021

Here's how pytest-asyncio implements the event_loop fixture:

@pytest.fixture
def event_loop(request):
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()

Note that loop.close() is the sole cleanup code.

Compare this with the implementation of asyncio.run():

    loop = events.new_event_loop()
    try:
        events.set_event_loop(loop)
        if debug is not None:
            loop.set_debug(debug)
        return loop.run_until_complete(main)
    finally:
        try:
            _cancel_all_tasks(loop)
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.run_until_complete(loop.shutdown_default_executor())
        finally:
            events.set_event_loop(None)
            loop.close()

Before calling loop.close(), asyncio.run() does this:

  1. It attempts to cancel outstanding tasks with _cancel_all_tasks().
  2. It calls loop.shutdown_asyncgens()
  3. It calls loop.shutdown_default_executor().

(I think (2) and (3) correspond to what the documentation calls "finalizing asynchronous generators and closing the threadpool.")

I only have a tenuous grasp on these asyncio internals, but it seems like the event_loop fixture should do this, too? And so should any other place in pytest-asyncio where the event loop is shut down, if there are more.

@SyntaxColoring SyntaxColoring changed the title During cleanup, should asynchronous generators like asyncio.run? During cleanup, should pytest-asyncio cancel outstanding tasks, shutdown async generators, and shutdown the thread pool, like asyncio.run() does? Jul 29, 2021
@SyntaxColoring SyntaxColoring changed the title During cleanup, should pytest-asyncio cancel outstanding tasks, shutdown async generators, and shutdown the thread pool, like asyncio.run() does? During cleanup, should pytest-asyncio cancel outstanding tasks, shut down async generators, and shut down the thread pool, like asyncio.run() does? Jul 29, 2021
@andyljones
Copy link

Echoing this - I just spent a while figuring out where the heck these 'async generator not closed' warnings were coming from in my test suite, when I couldn't reproduce the behaviour in prod.

@podusowski
Copy link

+1

Got a bunch of "Task was destroyed but it is pending!" because tasks weren't allowed to handle cancellation and finish.

@Tinche
Copy link
Member

Tinche commented Dec 3, 2021

Yeah, this seems like a good change to make. I'll take a PR or implement this myself in the next couple of weeks.

@asvetlov
Copy link
Contributor

asvetlov commented Jan 6, 2022

Can be solved as a side-effect of #235

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

5 participants