Python subprocess.communicate freezes when parent leaves zombies

I am trying to use Popento create a subprocess Aalong with a thread that communicates with it using Popen.communicate. The main process will wait in the thread with the help of Thread.jointhe specified timeout and kill Aafter this timeout expires, which should also lead to the thread dying too.

However, this does not work when it Aitself spawns more subprocesses B, Cand Dwith different groups of processes than those Athat refuse to die. Even after it is Adead and marked as non-existent, and even after the main process takes Awith os.waitpid(), so that it no longer exists, the thread refuses to join the main thread.

Only after all the children, B, C, Dare killed, they will return Popen.communicate.

Is this behavior expected from the module? Recursive waiting can be useful in some cases, but this, of course, is not suitable for the default behavior for Popen.communicate. And if this is the intended behavior, is there a way to override it?

Here is a very simple example:

from subprocess import PIPE, Popen
from threading import Thread
import os
import time
import signal

DEVNULL = open(os.devnull, 'w')

proc = Popen(["/bin/bash"], stdin=PIPE, stdout=PIPE,
             stderr=DEVNULL, start_new_session=True)


def thread_function():
    print("Entering thread")
    return proc.communicate(input=b"nohup sleep 100 &\nexit\n")


thread = Thread(target=thread_function)
thread.start()
time.sleep(1)
proc.kill()
while True:
    thread.join(timeout=5)
    if not thread.is_alive():
        break
    print("Thread still alive")

This is on Linux.

+4
1

, popen.communicate Linux. Proc.communicate(), , stdin, EOF, . .

stdin bash. , bash , popen.communicate EOF stdin, . - :

return proc.communicate(input=b"nohup sleep 100 >/dev/null&\nexit\n")

, , bash ... - , proc.kill, . bash, exit proc.kill. ,

os.killpg(proc.pid,15)

proc.kill(). B, C D, , .

: proc.communicate, :-) . docs :

: stdin. stdout stderr, . , .

2: , .

+1

Source: https://habr.com/ru/post/1693514/


All Articles