[New-bugs-announce] [issue32795] subprocess.check_output() with timeout does not exit if child process does not generate output after timeout

Mike Lewis report at bugs.python.org
Thu Feb 8 04:11:07 EST 2018


New submission from Mike Lewis <mike.lewis at mavenwireless.com>:

When using a timeout with check_output(), the call does not terminate unless the child process generates output after the timeout. Looking at the code, it appears there is a second call to communicate() after the timeout has happened, presumably to retrieve any remaining output. This call appears to hang until the child process generates output.

I have two test cases (for Python 2.7 / subprocess32 and Python 3 / subprocess respectively). They show the same behaviour, the Python 2.7 version has been reproduced on Ubuntu 16.04.3 and Centos 7 and the Python 3 version on Ubuntu 16.043.

Each test case has a first example where bash executes a long sleep before generating output and where the timeout is not respected, and a second example that generates output at intervals and the timeout is respected.

Relevant code also attached below for reference:

then = time.time()
print("Subprocess with idle stdout at timeout: start at {}".format(then))
try:
    output = subprocess.check_output(["bash", "-c",
                                        "echo Subcall; sleep 5; echo Done;"],
                                       stderr=subprocess.STDOUT, timeout=1)
    now = time.time()
    print("Finish at: {}, {:.0f} seconds".format(now, now-then))
    print(output)
except subprocess.TimeoutExpired as te:
    now = time.time()
    print("Timed out at: {}, {:.0f} seconds".format(now, now-then))

then = time.time()
print("Generating stdout from subprocess: start at ", then)
try:
    output = subprocess.check_output(["bash", "-c",
                                        "echo Subcall; for i in 1 2 3 4 5; do sleep 1; echo $i; done; echo Done;"],
                                       stderr=subprocess.STDOUT, timeout=1)
    now = time.time()
    print("Finish at: {}, {:.0f} seconds".format(now, now-then))
    print(output)
except subprocess.TimeoutExpired as te:
    now = time.time()
    print("Timed out at: {}, {:.0f} seconds".format(now, now-then))

----------
components: Library (Lib)
files: timeout_examples.tgz
messages: 311820
nosy: Mike Lewis
priority: normal
severity: normal
status: open
title: subprocess.check_output() with timeout does not exit if child process does not generate output after timeout
type: behavior
versions: Python 2.7, Python 3.5
Added file: https://bugs.python.org/file47429/timeout_examples.tgz

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32795>
_______________________________________


More information about the New-bugs-announce mailing list