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

requests.get() timeout times out after twice the given value #5760

Closed
LefterisJP opened this issue Feb 23, 2021 · 4 comments · Fixed by #6600
Closed

requests.get() timeout times out after twice the given value #5760

LefterisJP opened this issue Feb 23, 2021 · 4 comments · Fixed by #6600

Comments

@LefterisJP
Copy link

I made a simple example script to showcase what I experienced

import requests
import time

print('About to do a request that will timeout')
start_time = int(time.time())
try:
    response = requests.get('http://www.google.com:81/', timeout=4)
except requests.exceptions.RequestException as e:
    print(f'Exception: {str(e)}')
elapsed_secs = int(time.time()) - start_time
print(f'After request that should timeout. Elapsed seconds: {elapsed_secs}')

Expected Result

I expected this to timeout after 4 seconds and per the given timeout.

Actual Result

What happens instead is that the timeout exception is raised after 2x the given timeout argument.

About to do a request that will timeout
Exception: HTTPConnectionPool(host='www.google.com', port=81): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f204f680550>, 'Connection to www.google.com timed out. (connect timeout=4)'))
After request that should timeout. Elapsed seconds: 8

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "4.0.0"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.7.9"
  },
  "platform": {
    "release": "5.10.16-arch1-1",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.25.1"
  },
  "system_ssl": {
    "version": "101010af"
  },
  "urllib3": {
    "version": "1.26.3"
  },
  "using_pyopenssl": false
}

@sigmavirus24
Copy link
Contributor

In the future, please search closed and open issues before creating new ones that are duplicates.

@LefterisJP
Copy link
Author

Hello @sigmavirus24 thank you for your response.

I did do that but could not find one. I can of course only judge by the issue's title and what github search can afford to show me.

Can you please at least link the issue for which you are closing this one as a duplicate? Thanks in advance.

@MestreLion
Copy link
Contributor

One such issue I could find is #5450, the issue was closed but not solved.

I'm also experiencing this problem, and requests is consistently timing out after approximately twice the specified timeout. This is not a precision problem, this is an actual bug!

Please reopen either this or the other issue!

Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time, requests
>>> requests.__version__
'2.25.1'
>>> requests.urllib3.__version__
'1.26.3'
>>> def test_timeout(timeout=1, url='http://google.com:81'):
...     start = time.time()
...     try:
...             requests.get(url, timeout=timeout)
...     except requests.ConnectTimeout:
...             print(time.time()-start)
... 
>>> for i in range(1, 11):
...     test_timeout(i)
... 
2.0313971042633057
4.0080249309539795
6.010318040847778
8.012223482131958
10.014029264450073
12.012367725372314
14.017669200897217
16.01678204536438
18.01997184753418
20.043574571609497
>>> 

This is a ConnectTimeout, no Read Timeout involved. Look at the timings, it can't be more consistent than this!

@sigmavirus24
Copy link
Contributor

#4276 #3099

You're asking for a wall clock timeout and we can not provide that. Further we have clearly documented how timeouts work in our docs

@psf psf locked and limited conversation to collaborators Mar 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants