Skip to content

asyncio: cancelling a task awaiting subprocess stdin.wait_closed() cancels the shared close future, then pipe_connection_lost() raises InvalidStateError #162217

Open

Description

@AugustusHsu

Bug report

Bug description

For a subprocess created with asyncio.create_subprocess_exec(..., stdin=PIPE), proc.stdin.wait_closed() awaits the protocol's shared _stdin_closed future directly. SubprocessStreamProtocol._get_close_waiter() returns it, and StreamWriter.wait_closed() awaits it without shield(). If the task awaiting wait_closed() is cancelled after proc.stdin.close() but before the pipe's connection_lost callback runs, the cancellation cancels that shared future.

When SubprocessStreamProtocol.pipe_connection_lost(0, None) runs later, it calls self._stdin_closed.set_result(None) on the already-cancelled future. The event loop then logs:

Exception in callback SubprocessStreamProtocol.pipe_connection_lost()
...
asyncio.exceptions.InvalidStateError: invalid state

StreamReaderProtocol guards the equivalent future for sockets (if not self._closed.done(): ...); SubprocessStreamProtocol.pipe_connection_lost() has no such check.

Minimal, deterministic reproducer:

import asyncio
import sys


async def main(mode: str) -> int:
    loop = asyncio.get_running_loop()
    errors = []
    loop.set_exception_handler(lambda _loop, ctx: errors.append(ctx))
    proc = await asyncio.create_subprocess_exec(
        "cat", stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )
    proc.stdin.close()

    async def closer():
        if mode == "shield":
            await asyncio.shield(proc.stdin.wait_closed())
        else:
            await proc.stdin.wait_closed()

    task = asyncio.create_task(closer())
    await asyncio.sleep(0)  # let closer() reach the await on the close future
    task.cancel()           # cancel between close() and pipe_connection_lost()
    try:
        await task
    except asyncio.CancelledError:
        pass
    print("stdin close future:", proc._protocol._stdin_closed)
    await proc.communicate()
    await proc.wait()
    for _ in range(5):
        await asyncio.sleep(0)
    for ctx in errors:
        print("loop exception:", repr(ctx.get("exception")), "|", ctx.get("message"))
    return 1 if errors else 0


if __name__ == "__main__":
    sys.exit(asyncio.run(main(sys.argv[1] if len(sys.argv) > 1 else "plain")))

Output on 3.14.4 (Linux):

$ python3 repro.py
stdin close future: <Future cancelled>
loop exception: InvalidStateError('invalid state') | Exception in callback SubprocessStreamProtocol.pipe_connection_lost()

$ python3 repro.py shield
stdin close future: <Future finished result=None>

The visible impact is an error logged from a loop callback (for example during application shutdown, when a task blocked in wait_closed() gets cancelled). Process I/O is otherwise unaffected. In our case it appeared intermittently in a test suite (about 5 in 2,000 runs under load) until we stopped awaiting wait_closed() on that path.

Possible fixes, either of which avoids the error:

  • In SubprocessStreamProtocol.pipe_connection_lost(), only call set_result() / set_exception() on _stdin_closed when it is not already done, mirroring StreamReaderProtocol.
  • Or have wait_closed() (or _get_close_waiter()) shield the shared future, so cancelling one waiter does not cancel it for everyone.

Possibly related older report: bpo-23140.

CPython versions tested on

3.14

Operating systems tested on

Linux

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions