Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess.run isn't timing out, even though timeout is specified

Tags:

python-3.x

I have the following Python code:

strRunOutput = subprocess.run([strFastbootExecutable, "-s", dsn.upper(), "reboot"], timeout=5, stderr=subprocess.PIPE).stderr.decode('utf-8').upper()

Which is essentially doing this:

fastboot -s G070GV1871970FCW reboot

and this is the output:

< waiting for G070GV1871970FCW >

...which is hanging. Why the fastboot command is hanging, I don't know, but what bothers me more is that the subprocess.run command isn't timing out after 5 seconds as I told it to and is causing my program to hang. Any ideas what's going on?

Thanks!

like image 674
Mike Avatar asked May 14 '18 20:05

Mike


People also ask

Does subprocess wait for command to finish?

Most of your interaction with the Python subprocess module will be via the run() function. This blocking function will start a process and wait until the new process exits before moving on. The documentation recommends using run() for all cases that it can handle.

Does Popen have a timeout?

The timeout argument is passed to Popen.communicate() . If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.

What is Popen in subprocess?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

Does subprocess call raise exception?

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.


1 Answers

This is a known issue - there are two tickets on the Python bug tracker relating to it.

It's not timing out because you connected a pipe.

Issue31935: subprocess.run() timeout not working with grandchildren and stdout=PIPE
Issue30154: subprocess.run with stderr connected to a pipe won't timeout when killing a never-ending shell commanad (sic)

like image 108
Alan Avatar answered Oct 14 '22 06:10

Alan