requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)

Here is the first issue.
https://github.com/kennethreitz/requests/issues/2906

Python 3.5.1 (https://www.python.org/downloads/) Virtualenv 14.0.5 Mac OS X 10.11.3

First, I created a virtualenv and `pip install requests[security]`

Then I got

```
>>> from cryptography.hazmat.backends.openssl.backend import backend
>>> print(backend.openssl_version_text())
OpenSSL 1.0.2f  28 Jan 2016
```

which was what I expected.

Everything worked great for about an hour.

Then, some of my own scripts crashed which was normal. After that, when I try to run my script again, I got following exceptions

```
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)

requests.packages.urllib3.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)

requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)
```

So I opened another Python console and

```
>>> requests.get("https://www.google.com")
<Response [200]>
>>> requests.get("https://www.telegram.org")
Traceback (most recent call last):
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
    body=body, headers=headers)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 784, in _validate_conn
    conn.connect()
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/connection.py", line 252, in connect
    ssl_version=resolved_ssl_version)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/util/ssl_.py", line 305, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket
    _context=self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 983, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "VirtualenvPath/lib/python3.5/site-packages/requests/adapters.py", line 376, in send
    timeout=timeout
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 588, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "VirtualenvPath/lib/python3.5/site-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "VirtualenvPath/lib/python3.5/site-packages/requests/adapters.py", line 447, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)
>>> 
```

So I rebooted, uninstalled all these libs and pip install them again. Everything worked again.

But after 1 hour or so, same exception again.


> Then, some of my own scripts crashed which was normal.

What does a crash like that look like?

> After that, when I try to run my script again, I got following exceptions

Are you completely unable to run them at all after that?


@sigmavirus24 It's just some bug in my own code without anything to do with networking.
I'm not sure whether this is the real cause or just coincidence.

But one thing is for sure, after some point, I'm completely unable to make request to https://www.telegram.org, which I can do right after install request.

Just FYI: #2906


So I'd like to point out that you're installing `requests[security]` which means we should be using pyOpenSSL but your stacktrace shows that we aren't. That's intriguing.


@sigmavirus24 So this there anything I can do to help you?


Any idea? @Lukasa 


So what matters most here is: why does your code stop using PyOpenSSL? When you encounter your crash, can you open a python console in your virtual environment and then run `import urllib3.contrib.pyopenssl`, to see if that works?


Couldn't reproduce it now. I will close this first, and if I encounter the problem again, I will paste the result and reopen it.


@Lukasa I think since requests is shipped with its own urllib3, I could not import urllib3 alone. And the following result confirms it.

```
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib3.contrib.pyopenssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'urllib3'
>>> ^D




pip list
cffi (1.5.0)
cryptography (1.2.2)
idna (2.0)
ndg-httpsclient (0.4.0)
pip (8.0.2)
pyasn1 (0.1.9)
pycparser (2.14)
pyOpenSSL (0.15.1)
requests (2.9.1)
setuptools (20.0)
six (1.10.0)
wheel (0.26.0)
```


I'm sorry, try importing `requests.packages.urllib3.contrib.pyopenssl`


@Lukasa 

```
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests.packages.urllib3.contrib.pyopenssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "VirtualenvPath/lib/python3.5/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 57, in <module>
    from socket import _fileobject, timeout, error as SocketError
ImportError: cannot import name '_fileobject'
>>> 
```


I bet that is a bug on 3.5 with pyOpenSSL that we didn't know about @lukasa (with respect to `_fileobject` not existing. 


Nope, I know about it, and have proposed a fix upstream in urllib3 when we started testing PyOpenSSL. The reality is that `requests[security]` does not (and has never) worked on Python 3. That'll be fixed in an upcoming version of Requests.

So that doesn't solve our puzzle: why did this work for a bit and then stop?


tangent comment based on original report, the built in ssl.py in python has an option to suppress ragged EOFs as there are plenty of sites that will uncleanly shutdown SSL connections. sometimes unreliably as if it were a matter of timing or coincidence. the following is an extract from ssl.py

``` python
class SSLSocket(socket):
    [...]
    def read(self, len=0, buffer=None):
        """Read up to LEN bytes and return them.
        Return zero-length string on EOF."""

        self._checkClosed()
        if not self._sslobj:
            raise ValueError("Read on closed or unwrapped SSL socket.")
        try:
            return self._sslobj.read(len, buffer)
        except SSLError as x:
            if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                if buffer is not None:
                    return 0
                else:
                    return b''
            else:
                raise
```


https://github.com/shazow/urllib3/pull/795
https://github.com/shazow/urllib3/issues/791


Having the same exception on Python 2.7.11/OSX when pounding a server with  `requests` under `gevent`. Could this be related?


@the-efi could you be more specific about which exception you're seeing?


```
    r = requests.post(self.MY_URL, data=parameters)
  File "/Users/me/Envs/my_env/lib/python2.7/site-packages/requests/api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Users/me/Envs/my_env/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/me/Envs/my_env/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/me/Envs/my_env/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/Users/me/Envs/my_env/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: EOF occurred in violation of protocol (_ssl.c:590)
```


@the-efi Do you have `pyopenssl`, `pyasn1`, and `ndg-httpsclient` installed?


pyopenssl: negative
pyasn1: pyasn1==0.1.8 (transient, I guess)
ndg-httpsclient: negative


Ok. You're also running Python 2.7, so you and the poster seem to be having different problems.

Do you know if you hit your problem during connection setup, or on a long-running connection?


I would imagine this is during connection setup, but if `requests` reuses previously opened connections with `Connection: keep-alive` I might be wrong.


Requests does indeed re-use previously opened connections where possible, which is why I asked the question. ;)

It would be very useful if we could get a packet capture of this problem in your case, though that may be tricky given that it occurs under heavy load.


No problem, I will see what I can do about it after the weekend. Would you like me to submit that as a new issue?


Yes please. =)


@Lukasa Any progress with urllib3?


@caizixian We're getting there, but we have some problems with our CI testing because Travis CI has a fairly old PyPy image that doesn't behave well with PyOpenSSL at the moment. I'll see if I can get this to work sometime this weekend.


@Lukasa @shazow urllib3 always has a home at http://ci.kennethreitz.org, if desired!


Any update on this one? It happens quite a lot when working with aws.


@mindw As best as I know we don't have a good understanding of exactly where and when it's happening. In the backtrace above it's happening during the handshake, which is usually a problem with negotitation: when are you encountering this error?


OS X, Python 3.5.1, requests 2.10.0 + security.
I will try and provide any additional info upom request :)

```
Traceback (most recent call last):
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 351, in _make_request
    self._validate_conn(conn)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 814, in _validate_conn
    conn.connect()
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/connection.py", line 289, in connect
    ssl_version=resolved_ssl_version)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/util/ssl_.py", line 308, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket
    _context=self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 983, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 604, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gabdav01/.virtualenvs/splatt/bin/splatt", line 9, in <module>
    load_entry_point('splatt', 'console_scripts', 'splatt')()
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/core.py", line 1060, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/gabdav01/work/paas/splatt/splatt/cmd.py", line 108, in create
    url, json=json, headers=headers)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/sessions.py", line 518, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/Users/gabdav01/.virtualenvs/splatt/lib/python3.5/site-packages/requests/adapters.py", line 477, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:645)
```