python-subprocess32 icon indicating copy to clipboard operation
python-subprocess32 copied to clipboard

Timeout doesn't work properly

Open jozo opened this issue 4 years ago • 1 comments

Timeout parameter doesn't really force timeout. It's happening on both Python 3.5 and Python 2.7 + subprocess32. However it works in Python 3.9.5. Not sure if it's worth to fix it, but at least it will be documented here. :)

Tests:

import os
import sys
import unittest
from datetime import datetime, timedelta

if os.name == 'posix' and sys.version_info[0] < 3:
    import subprocess32 as subprocess
else:
    import subprocess


class TestSubprocess(unittest.TestCase):

    def test_long_sleep(self):
        start = datetime.now()
        try:
            subprocess.run(
                "sleep 10",
                timeout=1,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
        except Exception as exc:
            print("We got error:", type(exc))

        delta = datetime.now() - start
        print("It took", delta.seconds, "seconds")

        self.assertLess(delta, timedelta(seconds=2))


if __name__ == '__main__':
    unittest.main()

Results

Python 3.5.10:

We got error: <class 'subprocess.TimeoutExpired'>
It took 10 seconds
F
======================================================================
FAIL: test_long_sleep (__main__.TestSubprocess)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_subprocess.py", line 30, in test_long_sleep
    self.assertLess(delta, timedelta(seconds=2))
AssertionError: datetime.timedelta(0, 10, 2203) not less than datetime.timedelta(0, 2)

----------------------------------------------------------------------
Ran 1 test in 10.003s

FAILED (failures=1)

Python 2.7.18

('We got error:', <class 'subprocess32.TimeoutExpired'>)
('It took', 10, 'seconds')
F
======================================================================
FAIL: test_long_sleep (__main__.TestSubprocess)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_subprocess.py", line 30, in test_long_sleep
    self.assertLess(delta, timedelta(seconds=2))
AssertionError: datetime.timedelta(0, 10, 2108) not less than datetime.timedelta(0, 2)

----------------------------------------------------------------------
Ran 1 test in 10.003s

FAILED (failures=1)

Python 3.9.5

We got error: <class 'subprocess.TimeoutExpired'>
It took 1 seconds
.
----------------------------------------------------------------------
Ran 1 test in 1.002s

OK

jozo avatar May 17 '21 12:05 jozo

Here it's described: https://bugs.python.org/issue37424 It's fixed in Python 3.7.5: https://docs.python.org/release/3.7.5/whatsnew/changelog.html#changelog

jozo avatar May 17 '21 14:05 jozo