Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Timeout doesn't work properly #67

Open
jozo opened this issue May 17, 2021 · 1 comment
Open

Timeout doesn't work properly #67

jozo opened this issue May 17, 2021 · 1 comment

Comments

@jozo
Copy link

jozo commented May 17, 2021

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 added a commit to onecommons/unfurl that referenced this issue May 17, 2021
@jozo
Copy link
Author

jozo commented May 17, 2021

aszs pushed a commit to onecommons/unfurl that referenced this issue May 18, 2021
* #43 - Add tests for dryrun param in shell configurator

* #44 - Add test for timing out

Refactor the file and remove unittest

* #43 - Change name of a function

* #44 - Change timeout test to check for error, not time

* #44 - Fix tests for python2

* #44 - Skip integration test for timeout in shell in case of Python 2

Description of the problem with subprocess: google/python-subprocess32#67

* #44 - Skip integration test for timeout in shell for Python version < 3.7.5

* #44 - Reduce duration of a test

* #43 - Fix return value of canRunTask to make more sense
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant