requests + gevent dns error #1202

Closed
oargon opened this Issue Feb 19, 2013 · 12 comments

Projects

None yet

6 participants

@oargon

I seem to be getting a strange error using requests and gevent with the below code snippet:
running this under gunicorn and hitting this with: ab -n 5 -c 5 http://localhost:8000/
gives this output:

2013-02-19 19:32:24 [40251] [INFO] Starting gunicorn 0.17.2
2013-02-19 19:32:24 [40251] [INFO] Listening at: http://127.0.0.1:8000 (40251)
2013-02-19 19:32:24 [40251] [INFO] Using worker: gevent
2013-02-19 19:32:24 [40256] [INFO] Booting worker with pid: 40256
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
<Response [200]>
from flask import Flask
import requests

app = Flask(__name__)

@app.route('/')
def hello():
        try:
                r = requests.get('http://httpbin.org/delay/5')
                print r
        except Exception as e:
                print e.message
        return 'done'

these are the versions I'm using (all from pip install):

$ pip freeze
Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
argparse==1.2.1
distribute==0.6.24
gevent==0.13.8
greenlet==0.4.0
gunicorn==0.17.2
requests==1.1.0
wsgiref==0.1.2

@Lukasa
Collaborator

This looks like you're failing to resolve the domain name. Assuming you're running this on a box you have control over, can you try running nslookup httpbin.org from the command prompt?

@oargon
Server:     127.0.0.1
Address:    127.0.0.1#53

Non-authoritative answer:
Name:   httpbin.org
Address: 23.21.109.151

The call doesn't always fail with the DNS error.
The more connections I try to open at the same time the more likely it will fail.

@Lukasa
Collaborator

That's bizarre.

The failure seems to be to do with gevent's DNS stack, however, not Requests: we're simply wrapping the exception. To see if you can isolate it, try just calling gevent.dns.resolve_ipv4('httpbin.org') in each of your flask apps, to see if you can trigger the same exceptions.

@oargon

it appears that calling your suggested line just before calling requests makes the problem go away somehow.
so this seem to work every call:

gevent.dns.resolve_ipv4('httpbin.org')
r = requests.get('http://httpbin.org/delay/5')
@Lukasa
Collaborator

Uh...that was not what I expected to happen.

My best guess is that gevent caches the DNS lookup from the first call so that the second one definitely succeeds, but that doesn't really explain very much. Hmm. I'll have to think about this.

@hazzadous

Not really explaining what you're seeing, but do you still get this error with newer gevents? ie. 1.0rc2 (https://github.com/SiteSupport/gevent/downloads)

@oargon

I didn't have the time to check this issue with a newer version of gevent on the same computer but I did run the same test on another computer and I didn't observe the same errors on the other computer.

@Lukasa
Collaborator

Sounds to me like this is somehow gevent's fault. =D Anyone disagree?

@sigmavirus24
Collaborator
@Lukasa
Collaborator

With no other objections, then, I'm closing this issue. =)

@Lukasa Lukasa closed this Feb 26, 2013
@liushuaikobe

Was your gevent grabed by pip?
@oargon
This bug probably was result from the low version of the gevent.
You can refer this: http://jesiah.net/post/59711878434/gevent-problems-gevent-dns-dnserror

@garrettwilkin

I think I'm having the same problem with this. Going to try adding that resolve line to see if it makes it go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment