Skip to content

asyncio proc.kill() and proc.wait() are counter intuitive #119710

Description

@piwicode

Bug report

Bug description:

On Linux proc.wait() does not return after proc.kill() when there as sub/sub processes.
This only happens when the standard streams are piped, it looks like wait() blocks until the pipes are closed.

importasyncioimportosimporttimeasyncdefmain():
proc=awaitasyncio.create_subprocess_exec(
"/usr/bin/python3", "-c", "import os;os.system('sleep 20')",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
time.sleep(1)
os.system("ps -f --forest --format pid,ppid,pgid,sid,comm")
print(f">> This python proces pid={os.getpid()} has a `python3` child process, which has a `sleep` child process.")
print(f">> Kill the `python3` child process pid={proc.pid}")
proc.kill()
os.system("ps -f --forest --format pid,ppid,pgid,sid,comm")
print(f">> The `python3` child process was killed, the `sleep` process gets orfaned.")
print(f">> Calling `proc.wait()` or `proc.communicate()` does not return until `sleep` process exits.")
awaitproc.wait()
asyncio.run(main())

I don't know if it's a documentation issue or a bug.

  • It would be more intuitive that proc.wait() after a proc.kill() returned when the process exited, and does not depend on other processes completion.
  • It would be more intuitive to use have the same behavior when standard streams are piped and when they are not.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

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