You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
subprocess.check_output() with timeout does not exit if child process does not generate output after timeout #76976
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-02-27.19:32:06.090>created_at=<Date2018-02-08.09:11:07.043>labels= ['type-bug', 'library']
title='subprocess.check_output() with timeout does not exit if child process does not generate output after timeout'updated_at=<Date2021-02-27.19:32:06.090>user='https://bugs.python.org/MikeLewis'
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)
exceptsubprocess.TimeoutExpiredaste:
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)
exceptsubprocess.TimeoutExpiredaste:
now=time.time()
print("Timed out at: {}, {:.0f} seconds".format(now, now-then))
$ ./timeout.py
Subprocess with idle stdout at timeout: start at 1518081130.44
Timed out at: 1518081135.45, 5 seconds
Generating stdout from subprocess: start at 1518081135.45
Timed out at: 1518081136.47, 1 seconds
As you see, the 1 second timeout is respected in the second case where the child process is generating output.
fangyi-zhou commented on Feb 13, 2018
fangyi-zhoumannequin
Mannequin
More actions
I think the issue is that the process is not terminated.
If you increase the sleep duration, you would see that the sleep process is not killed by SIGKILL.
fangyi-zhou commented on Feb 13, 2018
fangyi-zhoumannequin
Mannequin
More actions
As far as I have discovered, the bash process is killed by after the timeout. Presumably the pipe is still open due to the sleep process, so in order to retrieve any output from stdout/stderr, the read waits on the pipe, hence leading to timeout not respected.
We can fix this issue in 2 ways
(1) kill process tree when timeout happens
(2) force closing the pipes after timeout, but this will leave the sub-processes detached and it will be kept running
I don't know which way is better, hopefully someone with more knowledge can engage in the discussion.
fangyi-zhou commented on Feb 14, 2018
fangyi-zhoumannequin
Mannequin
More actions
I choose to implement the first way. I believe this is in favour in prevention of resource leaks and zombie processes.
Activity
MikeLewis commentedon Feb 8, 2018
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:
MikeLewis commentedon Feb 8, 2018
Example of the output:
As you see, the 1 second timeout is respected in the second case where the child process is generating output.
fangyi-zhou commentedon Feb 13, 2018
I think the issue is that the process is not terminated.
If you increase the sleep duration, you would see that the sleep process is not killed by SIGKILL.
fangyi-zhou commentedon Feb 13, 2018
As far as I have discovered, the bash process is killed by after the timeout. Presumably the pipe is still open due to the sleep process, so in order to retrieve any output from stdout/stderr, the read waits on the pipe, hence leading to timeout not respected.
We can fix this issue in 2 ways
(1) kill process tree when timeout happens
(2) force closing the pipes after timeout, but this will leave the sub-processes detached and it will be kept running
I don't know which way is better, hopefully someone with more knowledge can engage in the discussion.
fangyi-zhou commentedon Feb 14, 2018
I choose to implement the first way. I believe this is in favour in prevention of resource leaks and zombie processes.
#5684
eryksun commentedon Feb 14, 2018
Work on related issues extends back 9 years to bpo-5115. Also see bpo-26534.
eryksun commentedon Feb 27, 2021
bpo-37424 fixed this for Python 3.7+ in POSIX. It's still broken in Windows, for which I'm leaving bpo-31447 open.