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

Can not run a subprocess in a test: "the child watcher does not have a loop attached" #2058

Closed
cecton opened this issue Jul 6, 2017 · 6 comments
Labels

Comments

@cecton
Copy link
Contributor

cecton commented Jul 6, 2017

Long story short

The loop created for the testing purpose doesn't allow you to spawn subprocesses using asyncio.create_subprocess_*.

Expected behaviour

You can spawn subprocesses without problem like you would do with a default loop.

Actual behaviour

When you try to spawn a subprocess, you automatically get:

self = <asyncio.unix_events.SafeChildWatcher object at 0x7fe80eb15e10>, pid = 9399
callback = <bound method _UnixSelectorEventLoop._child_watcher_callback of <_UnixSelectorEventLoop running=False closed=False debug=False>>
args = (<_UnixSubprocessTransport pid=9399 running>,)

    def add_child_handler(self, pid, callback, *args):
        if self._loop is None:
            raise RuntimeError(
>               "Cannot add child handler, "
                "the child watcher does not have a loop attached")
E           RuntimeError: Cannot add child handler, the child watcher does not have a loop attached

/usr/lib64/python3.6/asyncio/unix_events.py:858: RuntimeError

Steps to reproduce

import asyncio


# tokio event loop does not allow to override attributes
def skip_if_no_dict(loop):
    if not hasattr(loop, '__dict__'):
        pytest.skip("can not override loop attributes")


def stopper(loop):
    def f(*args):
        loop.call_later(0.001, loop.stop)
    return f


async def test_subprocess_co(loop):
    proc = await asyncio.create_subprocess_shell("exit 0", loop=loop)
    await proc.wait()
    assert proc.retcode == 0


def test_subprocess(loop, mocker):
    skip_if_no_dict(loop)
    loop.run_until_complete(test_subprocess_co(loop))

Your environment

Arch Linux, Python 3.6

@cecton
Copy link
Contributor Author

cecton commented Jul 6, 2017

@asvetlov is it possible that it is because the current loop is set to None when running the test?

https://github.com/aio-libs/aiohttp/blob/master/aiohttp/test_utils.py#L442

I have no idea why we put that to None but setting it to loop fixes the issue. Should I make a PR?

@cecton cecton mentioned this issue Jul 6, 2017
5 tasks
@kblin
Copy link

kblin commented Jul 6, 2017

The PR fixes an issue I was seeing trying to use a pytest-aiohttp event loop to test a function using asyncio.subprocess. Thanks!

@cecton
Copy link
Contributor Author

cecton commented Jul 6, 2017

@kblin yw \o/

As I explained there: #1946 (comment) You really don't need asyncio.create_subprocess_* in most cases. If you do not intend to actually use the event loop to run multiple things in parallel, I really don't see why you would introduce such complexity. But this is my opinion.

I'm using ayncio's subprocesses in my application so when I test it it runs under the test loop and that's how I ran into that problem.

@kblin
Copy link

kblin commented Jul 7, 2017

Same here, I'm calling an external tool in the function that I wanted to test.

@cecton
Copy link
Contributor Author

cecton commented Jul 25, 2017

Fixed by #2075

@cecton cecton closed this as completed Jul 25, 2017
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants