Skip to content

asyncio subprocess spawn does not record the awaiting task #157259

Description

@deadlovelll

Bug report

Bug description:

The internal subprocess pipe-connect task registers no awaited_by edge

Repro:

import asyncio, sys

async def main():
    spawn = asyncio.create_task(
        asyncio.create_subprocess_exec(sys.executable, '-c', 'pass',
                                       stdin=asyncio.subprocess.PIPE),
        name='spawn')

    await asyncio.sleep(0)
    await asyncio.sleep(0)

    for t in asyncio.all_tasks() - {asyncio.current_task()}:
        asyncio.print_call_graph(t)

    proc = await spawn
    await proc.wait()

asyncio.run(main())

Actual:

* Task(name='spawn', id=0x103e00b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()
* Task(name='Task-2', id=0x103b7d0a0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1753, in async BaseEventLoop.connect_write_pipe()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_subprocess.py', line 188, in async BaseSubprocessTransport._connect_pipes()

Expected:

* Task(name='spawn', id=0x105620b90)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()
* Task(name='Task-2', id=0x10539d0a0)
  + Call stack:
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1753, in async BaseEventLoop.connect_write_pipe()
  |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_subprocess.py', line 194, in async BaseSubprocessTransport._connect_pipes()
  + Awaited by:
    * Task(name='spawn', id=0x105620b90)
      + Call stack:
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/unix_events.py', line 211, in async _UnixSelectorEventLoop._make_subprocess_transport()
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py', line 1839, in async BaseEventLoop.subprocess_exec()
      |   File '/Users/timofeiivankov/cpython/Lib/asyncio/subprocess.py', line 249, in async create_subprocess_exec()

Proposed fix: register the edge in BaseSubprocessTransport.__init__ and discard it in doe callback

I have a fix ready for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions