twilio-python icon indicating copy to clipboard operation
twilio-python copied to clipboard

ResponseNotReady on API call

Open emperorcezar opened this issue 12 years ago • 5 comments

This might not have anything to do with the library and may just be httplib's fault. Last night I got the above exception when calling the API. Only happened once, but worried that it might be a bug in the lib.

Relevant parts of the traceback

  File "twilio/rest/resources/sms_messages.py", line 167, in create
    return self.create_instance(kwargs)
  File "twilio/rest/resources/base.py", line 328, in create_instance
    data=transform_params(body))
  File "twilio/rest/resources/base.py", line 180, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "twilio/rest/resources/base.py", line 135, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "twilio/rest/resources/base.py", line 102, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "httplib2/__init__.py", line 1593, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "httplib2/__init__.py", line 1335, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "httplib2/__init__.py", line 1291, in _conn_request
    response = conn.getresponse()
  File "python2.7/httplib.py", line 1018, in getresponse
    raise ResponseNotReady()

emperorcezar avatar May 23 '14 14:05 emperorcezar

This continues to happen occasionally. I have a few hundred messages going out a day and only happens once a week or so.

emperorcezar avatar Aug 13 '14 20:08 emperorcezar

Hey @emperorcezar - it looks like you're trying to create a new Message instance resource through the API here, am I right? Are these requests going out asynchronously?

A quick stackoverflow search shows that some asynchronous requests hit the exact same error as you do.

phalt avatar Aug 13 '14 20:08 phalt

@phalt You're correct that I'm using the API, but I'm just calling the python library like so client.sms.messages.create. If it helps at all, this occurs in a celery task.

emperorcezar avatar Aug 23 '14 03:08 emperorcezar

I had the same problem yesterday. First time in over 10K API calls, but the unexpected exception brought my server down. The twilio-python should catch exceptions that httplib can throw and wrap them in twilio exceptions or document that you might throw these exceptions.

I am using TwilioRestClient.calls.create and TwilioRestClient.messages.create, running synchronously. It was an attempt to call calls.create that failed. I create a new TwilioRestClient instance for every Twilio API call. My server is multi-threaded, but only one thread makes Twilio REST API calls. I have an http server in the process as well, using Paste.

Exception in thread notifications thread:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
  File "ljaf-court-notifications/courtserver.py", line 64, in notificationsLoop
    sendNotifications(db.getConn(), fromNumber, baseUrl, overrideTo, limit)
  File "/home/dierks/court-notify-prod/ljaf-court-notifications/sendnotifications.py", line 33, in sendNotifications
    sendNotification(conn, notification, fromNumber, baseUrl, overrideTo)
  File "/home/dierks/court-notify-prod/ljaf-court-notifications/sendnotifications.py", line 44, in sendNotification
    sendVoiceNotification(conn, notification, fromNumber, baseUrl, overrideTo)
  File "/home/dierks/court-notify-prod/ljaf-court-notifications/sendnotifications.py", line 79, in sendVoiceNotification
    from_= fromNumber)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/twilio/rest/resources/calls.py", line 124, in create
    return self.create_instance(kwargs)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 341, in create_instance
    data=transform_params(body))
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 193, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 148, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 115, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1593, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1346, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers, )
  File "/home/dierks/court-notify-prod/prodenv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1291, in _conn_request
    response = conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1033, in getresponse
    raise ResponseNotReady()
ResponseNotReady

tdierks avatar Sep 25 '14 16:09 tdierks

Based on looking at the code, I'm pretty sure this is an httplib2 bug and that it occurs as a side effect of the fact that the http authentication that Twilio uses entails two requests: the first gets an HTTP 401 response and the second (which now provides the HTTP Basic authentication) gives you the actual result. I believe that in the code in httplib2.Http._request(), when retrying a 401 response, is in some cases running into a race condition where the full body isn't read from the previous request yet, or it's in a bad state, leading to the raise ResponseNotReady() in httplib.HTTPConnection.getresponse().

tdierks avatar Sep 25 '14 16:09 tdierks