Mike
Mike

Reputation: 544

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

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!

Upvotes: 13

Views: 5838

Answers (2)

Alwin
Alwin

Reputation: 1057

The problem is because of the pipes. However, the following worked for me even with pipes:

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
try:
  return proc.communicate(timeout=1.0)
except TimeoutExpired:
  proc.kill()
  return proc.communicate()

Source: https://github.com/python/cpython/issues/81605

Upvotes: 0

Alan
Alan

Reputation: 3042

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)

Upvotes: 9

Related Questions