# Details of process.wait() deadlock

**URL:** <https://discuss.python.org/t/details-of-process-wait-deadlock/69481>\
**Category:** Async-SIG\
**Tags:** help\
**Created:** [October 28, 2024, 3:05pm UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481 "2024-10-28T15:05:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xitop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xitop/32/13242_2.png) [@xitop](https://discuss.python.org/u/xitop)\
**Post date:** [October 28, 2024, 3:05pm UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481/1 "2024-10-28T15:05:37Z")

</div>

Inspired by a recent SO question, I’d like to ask for more details in explanation of a possible deadlock in `asyncio.subprocess.Process.wait()`. This function is defined the usual way _“Wait for the child process to terminate.”_:

Documentation quote:

> **Note**
> 
> This method can deadlock when using `stdout=PIPE` or `stderr=PIPE` and the child process generates so much output that it blocks waiting for the OS pipe buffer to accept more data. Use the [`communicate()`](https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.subprocess.Process.communicate) method when using pipes to avoid this condition.

My understanding was: When the executed process is blocked by a full pipe, it cannot finish its work and then to exit. In other words: the fact the process does not terminate makes the `process.wait()` block. The `wait()` does the right thing and not reading the pipe (i.e. not communicating properly) is the primary cause.

However, the `process.wait` will block even when the process in such state receives a signal and terminates. A full buffer in the `process.stdout` stream buffer blocks the `process.wait`. Draining the buffer unblocks the `wait`. I think that in this case `wait()` does not behave correctly. It should have returned when the process had exited - regardless of buffer full condition.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [October 28, 2024, 7:49pm UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481/2 "2024-10-28T19:49:55Z")

</div>

My untested assumption is that if you kill the process then wait() will return as wait() is not interested in the stdin/stdout pipes.

As you describe the issue is the process writes to stdout and after that completes will exit. But if the pipe is not read the write never completes in the process and you wait for ever.

The `subprocess.Popen.communicate` function exists in the sync world to fix this issue. And is used by subprocess.run I assume.

I would assume that you can setup the async process to have its stdout/stderr read as well as waiting for process exit. That would  
fix the problem as well.

---

<div class="post-metadata">

**Author:** ![xitop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xitop/32/13242_2.png) [@xitop](https://discuss.python.org/u/xitop)\
**Post date:** [October 28, 2024, 9:03pm UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481/3 "2024-10-28T21:03:25Z")

</div>

> [@barry-scott](#):
>
> My untested assumption is that if you kill the process then wait() will return as wait() is not interested in the stdin/stdout pipes.

My assumption was the same. But in reality it behaves in the opposite way. `wait` does not return when the process is killed (and the buffer is still full) which means it cares about the buffer being full even after the process is gone. This was the reason I posted here.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [October 28, 2024, 10:16pm UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481/4 "2024-10-28T22:16:33Z")

</div>

Do you have a small test case to show the problem?

Are you doing this on linux? Of so there are lots of tools to find out what is happening. I would try strace on the python parent for example.

---

<div class="post-metadata">

**Author:** ![xitop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xitop/32/13242_2.png) [@xitop](https://discuss.python.org/u/xitop)\
**Post date:** [October 29, 2024, 7:52am UTC](https://discuss.python.org/t/details-of-process-wait-deadlock/69481/5 "2024-10-29T07:52:23Z")

</div>

Please find below a small Linux test program.

Preparation: create a 512kB file `./datafile`, e.g.

```
 dd if=/dev/zero of=datafile bs=64k count=8

```

Code:

```python
import asyncio

async def killproc(proc):
    await asyncio.sleep(1)
    print(f"{proc.returncode=}")
    proc.terminate()
    print("signal sent")
    await asyncio.sleep(1)
    print(f"{proc.returncode=}")
    await asyncio.sleep(2)
    print("reading pipe buffer")
    await proc.stdout.read()

async def main():
    proc = await asyncio.create_subprocess_exec(
        "/usr/bin/cat", "./datafile",
        stdout=asyncio.subprocess.PIPE)
    print("process started")
    asyncio.create_task(killproc(proc))
    print("wait() start")
    await proc.wait()
    print("wait() stop")

asyncio.run(main())

```

The output + blank lines where are delays + my comments:

```
-----
process started
wait() start

proc.returncode=None # process 'cat' exists
signal sent

proc.returncode=-15 # process 'cat' terminated by signal 15
                      # (and the Python is aware of that)

reading pipe buffer # <-- without this the 'wait' blocks
wait() stop
-----
```
