Skip to content

asyncio.create_subprocess_exec hanging when exception in gathered task #133536

@alexyao2015

Description

@alexyao2015

Bug report

Bug description:

import asyncio


async def task1():
    await asyncio.create_subprocess_shell(
        "ls",
        # Any 2 pipes are required to reproduce
        stdin=asyncio.subprocess.PIPE,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    print("task1 done")


async def task2():
    raise Exception("Task 2 failed")


async def main():
    # Either asyncio.gather, asyncio.TaskGroup, or asyncio.as_completed will cause the hang
    print("running gather")
    await asyncio.gather(task1(), task2())
    print("running tg")
    async with asyncio.TaskGroup() as tg:
        tg.create_task(task1())
        tg.create_task(task2())
    print("running as_completed")
    for result in asyncio.as_completed([task1(), task2()]):
        await result
    print("done")


if __name__ == "__main__":
    asyncio.run(main())

When awaiting create_subprocess_shell with another task that throws an exception, the subprocess await will hang indefinitely waiting for await transp._wait() in

await transp._wait()

This is reproducible with either asyncio.gather, asyncio.TaskGroup, or asyncio.as_completed as shown in the example. Interestingly, this requires at least 2 pipes to be attached to the program before this bug will be triggered.

CPython versions tested on:

3.13, 3.11

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions