Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limitation because subprocess.run with timeout doesnt work for child processes #51

Open
bigbitbus opened this issue Nov 17, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@bigbitbus
Copy link

bigbitbus commented Nov 17, 2021

I am trying to use a timeout to kill a subprocess launched via .run(timeout=); this is not supported unlike the original subprocess.run()

To reproduce:

Python 3.8.12 (default, Oct 27 2021, 12:52:48) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess_tee import run
>>> run('sleep 10', shell=True, timeout=.1, capture_output=True)
CompletedProcess(args='sleep 10', returncode=0, stdout='', stderr='')

Whereas with subprocess.run the exception is correctly thrown:

Python 3.8.12 (default, Oct 27 2021, 12:52:48) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import run
>>> run('sleep 10', shell=True, timeout=.1, capture_output=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/subprocess.py", line 495, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/usr/local/lib/python3.8/subprocess.py", line 1028, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/local/lib/python3.8/subprocess.py", line 1869, in _communicate
    self._check_timeout(endtime, orig_timeout, stdout, stderr)
  File "/usr/local/lib/python3.8/subprocess.py", line 1072, in _check_timeout
    raise TimeoutExpired(
subprocess.TimeoutExpired: Command 'sleep 10' timed out after 0.1 seconds

The output should have been a TimedoutException.

Also tried

>>> run(['sleep','10'], shell=False, timeout=.1, capture_output=False)
CompletedProcess(args=['sleep', '10'], returncode=0, stdout='', stderr='')
@bigbitbus
Copy link
Author

We could add something like the below in

(can send PR if we agree)

            )
        if kwargs.get("timeout", -1):
            await asyncio.wait_for(process.wait(), timeout=kwargs.get("timeout", -1))

@ssbarnea
Copy link
Member

Sounds ok, but please include a test.

@ssbarnea ssbarnea added the bug Something isn't working label Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants