getsentry / raven-python

Raven is the legacy Python client for Sentry (getsentry.com) — replaced by sentry-python
https://sentry.io
BSD 3-Clause "New" or "Revised" License
1.68k stars 657 forks source link

Conflict with httplib2 #703

Open ben opened 8 years ago

ben commented 8 years ago

Found this because of a strange exception when integrating with Celery and making HTTP requests, but there's a simpler reproduction scenario. Here's my example program:

import httplib2
http = httplib2.Http()

http.request('https://google.com', 'GET')
print(1)
from raven import Client
print(2)
http.request('https://google.com', 'GET')
print(3)

And here's the output:

$ python3 ~/Desktop/test.py
1
2
Traceback (most recent call last):
  File "/Users/ben/Desktop/test.py", line 8, in <module>
    http.request('https://httpbin.org/post', 'GET')
  File "/usr/local/lib/python3.4/site-packages/httplib2/__init__.py", line 1314, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python3.4/site-packages/httplib2/__init__.py", line 1064, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python3.4/site-packages/httplib2/__init__.py", line 1047, in _conn_request
    response = Response(response)
  File "/usr/local/lib/python3.4/site-packages/httplib2/__init__.py", line 1382, in __init__
    for key, value in info.items():
AttributeError: 'HTTPResponse' object has no attribute 'items'

The very act of importing raven.Client causes httplib2 to stop working. In my case, it surfaced as a mysterious error deep inside the Twilio client, which I'll paste below in case someone else googles for it.

This is using Python 3.4.3, raven==5.9.0, and httplib2==0.9.2, all of which appear to be the latest.


That Twilio library exception:

  File "/usr/local/lib/python3.5/site-packages/twilio/rest/resources/messages.py", line 122, in create
    return self.create_instance(kwargs)
  File "/usr/local/lib/python3.5/site-packages/twilio/rest/resources/base.py", line 365, in create_instance
    data=transform_params(body))
  File "/usr/local/lib/python3.5/site-packages/twilio/rest/resources/base.py", line 200, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/twilio/rest/resources/base.py", line 152, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/twilio/rest/resources/base.py", line 117, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/usr/local/lib/python3.5/site-packages/httplib2/__init__.py", line 1196, in request
    self.disable_ssl_certificate_validation)
  File "/usr/local/lib/python3.5/site-packages/httplib2/__init__.py", line 850, in __init__
    check_hostname=True)
  File "/usr/local/lib/python3.5/http/client.py", line 1209, in __init__
    super(HTTPSConnection, self).__init__(host, port, timeout,
TypeError: super(type, obj): obj must be an instance or subtype of type
raylu commented 8 years ago

It turns out this is only an issue with eventlet installed. Importing raven causes all of the transports to get imported, including https://github.com/getsentry/raven-python/blob/356a7c4d83a5289e7b30a07b0f76829e274b7481/raven/transport/eventlet.py#L14 which eventually imports https://github.com/eventlet/eventlet/blob/49773bb12b84e10ccb0a199c301f97fd6528ab68/eventlet/green/httplib.py#L16 which causes the check here to fail https://github.com/jcgregorio/httplib2/blob/cf631a73e2f3f43897b65206127ced82382d35f5/python3/httplib2/__init__.py#L1366 because httplib2 imported http.client before it was replaced by an identical version by eventlet. The simple fix is to import raven before importing httplib2/twilio.

jstasiak commented 8 years ago

Eventlet bug report: https://github.com/eventlet/eventlet/issues/316

jstasiak commented 8 years ago

FYI this is fixed in Eventlet master branch and should be released fairly soon.