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

urllib3 doesn’t raise ReadTimeoutError when monkey patched. #644

Closed
karanlyons opened this issue Aug 31, 2015 · 4 comments
Closed

urllib3 doesn’t raise ReadTimeoutError when monkey patched. #644

karanlyons opened this issue Aug 31, 2015 · 4 comments
Labels
Type: Bug Identified as a bug; needs a code change to fix

Comments

@karanlyons
Copy link

Gevent v1.1b3 with Python 2.7.6.

nc -l 1337 # Listen on 1337 and keep the connection open.

Without Gevent:

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://localhost:1337/', timeout=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 75, in request
    **urlopen_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 88, in request_encode_url
    return self.urlopen(method, url, **urlopen_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 155, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 557, in urlopen
    body=body, headers=headers)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 414, in _make_request
    self, url, "Read timed out. (read timeout=%s)" % read_timeout)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=1337): Read timed out. (read timeout=1)

With Gevent:

from gevent import monkey
monkey.patch_socket()

import urllib3

http = urllib3.PoolManager()
r = http.request('GET', 'http://localhost:1337/', timeout=1) # Hangs indefinitely.
@karanlyons karanlyons changed the title Sockets don’t raise read timeouts when monkey patched. urllib3 doesn’t raise ReadTimeoutError when monkey patched. Aug 31, 2015
@jamadden
Copy link
Member

Can you simplify the scenario to not use a third-party library? The presence of a third-party library makes things much more complicated.

@karanlyons
Copy link
Author

I’ll try to come up with a bare socket example that isn’t doing anything different. For what it’s worth, you can wrap your request in a gevent.Timeout context to get an ersatz read timeout (it won’t actually be for just the read, but the entire connection), but ideally that wouldn’t be needed.

@karanlyons
Copy link
Author

Here’s the behavior with a bare socket:

nc -l 1337 # Listen on 1337 and keep the connection open.

Without Gevent:

>>> import socket
>>> sock = socket.create_connection(('localhost', 1337), 1)
>>> fp = sock.makefile('rb', 0)
>>> fp.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 355, in read
    data = self._sock.recv(rbufsize)
socket.timeout: timed out

With Gevent:

>>> from gevent import monkey
>>> monkey.patch_socket()
>>> 
>>> import socket
>>> sock = socket.create_connection(('localhost', 1337), 1)
>>> fp = sock.makefile('rb', 0)
>>> fp.read() # Hangs indefinitely.

Note that sock.recv() does the right thing in both cases (raising socket.timeout), but using a file object (as httplib and all its descendants do) causes the read to hang indefinitely when socket is monkey patched.

@jamadden jamadden added Type: Bug Identified as a bug; needs a code change to fix python2 labels Sep 3, 2015
@jamadden
Copy link
Member

jamadden commented Sep 3, 2015

Thanks for the report. I can reproduce this under Python 2, but I can't reproduce it under Python 3 (the expected timeout is raised under Python 3). The expected timeout is also raised for Python 2 with gevent 1.0.2, so this is a regression somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Identified as a bug; needs a code change to fix
Projects
None yet
Development

No branches or pull requests

2 participants