Skip to content

event_loop teardown prevents cleanup tasks from finishing #91

Closed
@amit-traiana

Description

@amit-traiana

Hello everyone!

I'm not sure if that's even a pytest-asyncio bug, it might go down up to asyncio, but Im sharing the details in case it is. I'm trying to use pytest-asyncio with pyppeteer - a package the wrap Google's Puppetter API and interacts with Chrome. Here's and example test:

from pyppeteer import launch
import pytest

@pytest.mark.asyncio
async def test_some_asyncio_code():
    browser = await launch()
    page = await browser.newPage()
    raise Exception('Unexpected Exception')
    await browser.close()

The following makes pytest freeze, or to be exact - the process is sleeping forever while the C method epoll_wait is waiting for any interrupt.

The following code seems to work-around it:

from pyppeteer import launch
import pytest

@pytest.yield_fixture
def loop():
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     yield loop

def test_some_asyncio_code(loop):
    async def inner():
        browser = await launch()
        page = await browser.newPage()
        raise Exception('Unexpected Exception')
        await browser.close()

    loop.run_until_complete(inner())

The difference here is that I do not close the loop, and Python does it for me when the run ends. Adding 'loop.close()to the fixture, prints an error thatThe event loop is already closed`, which might be a hint of what's happening here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions