[Errno 2] _ssl.c:504: The operation did not complete (read)

I'm using [rauth](https://github.com/litl/rauth) in my Django application and encoutering this error. rauth is based on requests, and looking at the traceback you can see that the error indeed comes from requests:

```
File "/www/srv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "./proj/apps/core/views.py" in end_pipeline
  62.                                             make_absolute(reverse('home'))})
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_auth_session
  533.         return self.get_session(self.get_access_token(method, **kwargs))
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_access_token
  518.         r = self.get_raw_access_token(method, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_raw_access_token
  496.         return session.request(method, self.access_token_url, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/session.py" in request
  360.         return super(OAuth2Session, self).request(method, url, **req_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in request
  335.         resp = self.send(prep, **send_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in send
  438.         r = adapter.send(request, **kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/adapters.py" in send
  331.                 raise SSLError(e)

Exception Type: SSLError at /accounts/end-pipeline/
Exception Value: [Errno 2] _ssl.c:504: The operation did not complete (read)
```

This seems to be caused by payloads larger than usual.
The strange thing is that in the development environment it works perfectly, but not in production. And this is blocking the deployment. Please help me to solve this as soon as possible if you can.

I found only another issue with the same problem here: pika/pika#59. That was solved. There is an issue in Python tracker here:
http://bugs.python.org/issue12343


Could you remove the except clause at `adapters.py:330` so we can see where exactly this exception comes from?
(Handshake or normal `read`)


Thanks for the quick reply! There you go:

```
File "/www/srv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "./proj/apps/core/views.py" in end_pipeline
  62.                                             make_absolute(reverse('home'))})
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_auth_session
  533.         return self.get_session(self.get_access_token(method, **kwargs))
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_access_token
  518.         r = self.get_raw_access_token(method, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_raw_access_token
  496.         return session.request(method, self.access_token_url, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/session.py" in request
  360.         return super(OAuth2Session, self).request(method, url, **req_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in request
  335.         resp = self.send(prep, **send_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in send
  438.         r = adapter.send(request, **kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/adapters.py" in send
  292.                     timeout=timeout
File "/www/srv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py" in urlopen
  461.             raise SSLError(e)
```

Although I'm afraid it's not enough. There is another except:

```
            # Timed out by socket
            raise TimeoutError(self, url,
                               "Request timed out. (timeout=%s)" %
                               timeout)
        except BaseSSLError as e:
            # SSL certificate error
            raise SSLError(e) ...
        except CertificateError as e:
            # Name mismatch
            raise SSLError(e)    # <---- This is line 461
        except (HTTPException, SocketError) as e:
```

I'll drop this except as well, so that we can see clearly.


Damn, after removing that one it's a mess. The error is now `HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /login/oauth/access_token (Caused by <class 'ssl.SSLError'>: [Errno 2] _ssl.c:504: The operation did not complete (read))`. And the traceback:

```
File "/www/srv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "./proj/apps/core/views.py" in end_pipeline
  62.                                             make_absolute(reverse('home'))})
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_auth_session
  533.         return self.get_session(self.get_access_token(method, **kwargs))
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_access_token
  518.         r = self.get_raw_access_token(method, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/service.py" in get_raw_access_token
  496.         return session.request(method, self.access_token_url, **kwargs)
File "/www/srv/lib/python2.7/site-packages/rauth/session.py" in request
  360.         return super(OAuth2Session, self).request(method, url, **req_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in request
  335.         resp = self.send(prep, **send_kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/sessions.py" in send
  438.         r = adapter.send(request, **kwargs)
File "/www/srv/lib/python2.7/site-packages/requests/adapters.py" in send
  292.                     timeout=timeout
File "/www/srv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py" in urlopen
  474.                 raise MaxRetryError(self, url, e)
```


Oh no, we got real problems here. With Django's inbuilt development server everything works (even with `DEBUG=False`), whereas with Circus + Chaussette it raises the error from above! oO
Any tips? I'm getting mad...


I'm on my phone currently, so I can't dig really deep into this. Can you look at the value of the timeout parameter? According to the big you linked the socket is in nonblocking mode which raises this exception, if the previous operation has not yet competed. If I'm not mistaken, the socket should be in blocking mode.


I get this error with boto and requests, so it's a problem with an underlying library, maybe urllib3?

I don't get the error in my scripts running in my current shell, either django dev server or from a normal shell script.  However, when I run inside foreman or honcho (uses subprocess) it happens.  I'm reproducing this behavior in Ubuntu Precise and Mac OSX 10.8 by running a breakout script as `python breakout.py` successfully while `foreman run python breakout.py` fails.  I reset my virtualenv to old versions of these packages to test and it didn't change the outcome, so it could be something in Python or something in the path that is wrong?


It's worth confirming whether you can reproduce this with a totally clean virtualenv, installing all the packages directly from PyPI. A lot of these problems appear from incorrect installations.


@Lukasa I said in my last message that's what I did.

Just to clarify - I did an `rm -fr venv && virtualenv venv`


Still trying to track it down, I added `print traceback.format_exc(e)` to requests/adapters.py:331 and this is what I get:

```
$ foreman run python tasks.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): x.s3.amazonaws.com
Traceback (most recent call last):
  File "/x/venv/lib/python2.7/site-packages/requests/adapters.py", line 292, in send
    timeout=timeout
  File "/x/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 461, in urlopen
    raise SSLError(e)
SSLError: [Errno 2] _ssl.c:503: The operation did not complete (read)
```


Moving the print statement to that line in connectionpool.py:

```
$ foreman run python tasks.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): x.s3.amazonaws.com
Traceback (most recent call last):
  File "/x/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 428, in urlopen
    body=body, headers=headers)
  File "/x/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 280, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 955, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 989, in _send_request
    self.endheaders(body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 951, in endheaders
    self._send_output(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 811, in _send_output
    self.send(msg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 773, in send
    self.connect()
  File "/x/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 107, in connect
    ssl_version=resolved_ssl_version)
  File "/x/venv/lib/python2.7/site-packages/requests/packages/urllib3/util.py", line 378, in ssl_wrap_socket
    ssl_version=ssl_version)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 372, in wrap_socket
    ciphers=ciphers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 134, in __init__
    self.do_handshake()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 296, in do_handshake
    self._sslobj.do_handshake()
SSLError: [Errno 2] _ssl.c:503: The operation did not complete (read)

Traceback (most recent call last):
  File "tasks.py", line 12, in <module>
    print requests.get('https://x.s3.amazonaws.com')
  File "/x/venv/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/x/venv/lib/python2.7/site-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/requests/adapters.py", line 331, in send
    raise SSLError(e)
requests.exceptions.SSLError: [Errno 2] _ssl.c:503: The operation did not complete (read)
```


These tracebacks make it look like you have an SSL problem. I shall now invoke Requests' resident SSL specialist by saying his name three times: @t-8ch @t-8ch @t-8ch 


Related? http://bugs.python.org/issue12343


@jsullivanlive I can't reproduce this on my end.
Could you provide a minimal code sample?
If I'm looking at your example about `tasks.py` I don't see any timeouts involved. On the other hand this behaviour should only occur if non-blocking mode is active which isn't the default.

A shot in the dark would be to go to `requests/packages/urllib3/util.py` and modify the `ssl_wrap_socket` function to look like this:

``` py
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
                    ca_certs=None, server_hostname=None,
                    ssl_version=None):
    s = wrap_socket(sock, keyfile=keyfile, certfile=certfile,
                          ca_certs=ca_certs, cert_reqs=cert_reqs,
                          ssl_version=ssl_version)
    s.do_handshake()
    return s
```


Is it possible foreman is making it run in non blocking mode?  I will try
with your modification and get back to you.

## 

Sent from my iPhone


Thanks for the help, but this was my mistake - I had a local import in my breakout which had the line `from gevent import monkey; monkey.patch_socket()` embedded in it.  It apparently wasn't enough of a breakout!  When switching patch_socket to patch_all the gevent error goes away.  Maybe @rubik 's server is using patch_socket instead, I found that in examples of auto-reload servers.


I tried tracking it down just like you did, with no success. It put print_exc everywhere, in requests and urllib3. More importantly, this problem only showed up in production stack (circus + Chaussette) and not with Django's development server.


@jsullivanlive: thanks for the cue, I'll look for that!


Thanks for posting your solution @jsullivanlive. :+1:


Unfortunately I'm using Chaussette with meinheld backend, not Gevent. And meinheld does not use gevent, I checked that...


http://meinheld.org says async in the first paragraph...

This may have something to do with it:
https://github.com/mopemope/meinheld/blob/master/meinheld/patch.py


There is also mopemope/meinheld#27


Wow thanks a lot guys. That was big help. I'm investigating further!


Just to say, you are giving me good clues for problem I am having now four years after the main discussion.
Thx and have a good sleep

Had an issue with ssl and large messages (experienced several SSLWantWriteErrors) - fixed it by using [gevent's monkey patching](http://www.gevent.org/gevent.monkey.html). Thx for pointing me in this direction :)