Author sth
Recipients asvetlov, sth, yselivanov
Date 2018-12-30.22:59:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
`asyncio.create_subprocess_exec()` accepts a `loop` parameter, but doesn't use it to watch the child process. Instead uses `get_event_loop_policy().get_child_watcher()`, which doesn't doesn't know about `loop` but tries to use the current default event loop.

This fails if there is no current event loop or if that loop isn't running:

import asyncio

async def action(loop):
    proc = await asyncio.create_subprocess_exec('echo', loop=loop)
    await proc.wait()

loop = asyncio.new_event_loop()

This crashes because the main event loop never was created:

Traceback (most recent call last):
  File "", line 8, in <module>
  File "/home/sth/devel/cpython.vanilla/Lib/asyncio/", line 589, in run_until_complete
    return future.result()
  File "", line 4, in action
    proc = await asyncio.create_subprocess_exec('echo', loop=loop)
  File "/home/sth/devel/cpython.vanilla/Lib/asyncio/", line 213, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
  File "/home/sth/devel/cpython.vanilla/Lib/asyncio/", line 1542, in subprocess_exec
    transport = await self._make_subprocess_transport(
  File "/home/sth/devel/cpython.vanilla/Lib/asyncio/", line 193, in _make_subprocess_transport
  File "/home/sth/devel/cpython.vanilla/Lib/asyncio/", line 924, in add_child_handler
    raise RuntimeError(
RuntimeError: Cannot add child handler, the child watcher does not have a loop attached

If we do have a current event loop, for example by calling `asyncio.get_event_loop()` before creating out own loop, then we don't get an error, but the program hangs indefinitely since that loop isn't running.

Expected behavior would be that the loop given to create_subprocess_exec() is used to watch the child process.
Date User Action Args
2018-12-30 22:59:42sthsetrecipients: + sth, asvetlov, yselivanov
2018-12-30 22:59:38sthsetmessageid: <>
2018-12-30 22:59:38sthlinkissue35621 messages
2018-12-30 22:59:38sthcreate