psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.2k stars 9.34k forks source link

requests.exceptions.SSLError: (\"bad handshake: SysCallError(-1, 'Unexpected EOF') #3939

Closed a-sharma11 closed 7 years ago

a-sharma11 commented 7 years ago

i have a flask application that need to get some data from other web endpoints. I am getting this error when trying to make a get call using requests Note - This only happens when running with uWSGI. if i run flask app directly, it works fine.

"SSLError(SSLError(SSLError(\"bad handshake: SysCallError(-1, 'Unexpected EOF')\",),),)", "exception": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py\", line 436, in wrap_socket\n    cnx.do_handshake()\n  File \"/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py\", line 1426, in do_handshake\n    self._raise_ssl_error(self._ssl, result)\n  File \"/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py\", line 1167, in _raise_ssl_error\n    raise SysCallError(-1, \"Unexpected EOF\")\nOpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py\", line 600, in urlopen\n    chunked=chunked)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py\", line 345, in _make_request\n    self._validate_conn(conn)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py\", line 844, in _validate_conn\n    conn.connect()\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py\", line 326, in connect\n    ssl_context=context)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/util/ssl_.py\", line 324, in ssl_wrap_socket\n    return context.wrap_socket(sock, server_hostname=server_hostname)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py\", line 443, in wrap_socket\n    raise ssl.SSLError('bad handshake: %r' % e)\nssl.SSLError: (\"bad handshake: SysCallError(-1, 'Unexpected EOF')\",)\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/usr/local/lib/python3.5/dist-packages/requests/adapters.py\", line 423, in send\n    timeout=timeout\n  File \"/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py\", line 630, in urlopen\n    raise SSLError(e)\nrequests.packages.urllib3.exceptions.SSLError: (\"bad handshake: SysCallError(-1, 'Unexpected EOF')\",)\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"./utils/auth_wrap.py\", line 18, in authenticate\n    user = session.get(token)\n  File \"./utils/session.py\", line 12, in get\n    response = requests.get(self.url, auth=HTTPBasicAuth(token, ''))\n  File \"/usr/local/lib/python3.5/dist-packages/requests/api.py\", line 70, in get\n    return request('get', url, params=params, **kwargs)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/api.py\", line 56, in request\n    return session.request(method=method, url=url, **kwargs)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/sessions.py\", line 488, in request\n    resp = self.send(prep, **send_kwargs)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/sessions.py\", line 609, in send\n    r = adapter.send(request, **kwargs)\n  File \"/usr/local/lib/python3.5/dist-packages/requests/adapters.py\", line 497, in send\n    raise SSLError(e, request=request)\nrequests.exceptions.SSLError: (\"bad handshake: SysCallError(-1, 'Unexpected EOF')\",)"}
[2017-03-24 02:42:02,857] ERROR in app: Exception on /api/etl/v2/config [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 436, in wrap_socket
    cnx.do_handshake()
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1167, in _raise_ssl_error
    raise SysCallError(-1, "Unexpected EOF")
OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')
RUN python3 -m pip freeze
 ---> Running in 5cc74c5987ed
asn1crypto==0.22.0
boto3==1.4.4
botocore==1.5.29
cffi==1.10.0
click==6.7
clickclick==1.2.1
connexion==1.1.5
cryptography==1.8.1
docutils==0.13.1
Flask==0.12
Flask-Cors==3.0.2
hvac==0.2.17
idna==2.5
itsdangerous==0.24
Jinja2==2.9.5
jmespath==0.9.2
jsonschema==2.6.0
MarkupSafe==1.0
ndg-httpsclient==0.4.2
numpy==1.12.1
packaging==16.8
pandas==0.19.2
pyasn1==0.2.3
pycparser==2.17
pycurl==7.43.0
pygobject==3.20.0
pymssql==2.1.3
pyodbc==4.0.15
pyOpenSSL==16.2.0
pyparsing==2.2.0
python-apt==1.1.0b1
python-dateutil==2.6.0
pytz==2016.10
PyYAML==3.12
requests==2.13.0
s3transfer==0.1.10
six==1.10.0
SQLAlchemy==1.1.6
strict-rfc3339==0.7
structlog==16.1.0
swagger-spec-validator==2.1.0
unattended-upgrades==0.1
uWSGI==2.0.14
Werkzeug==0.12.1
a-sharma11 commented 7 years ago

Some more context here: I have two docker containers here. 1st one is running a python flask app and the other one is a nodejs express server. They both talk to each other via docker linking. This worked great until i added uwsgi layer to my flask container.

StewPoll commented 7 years ago

I'm not a major contributor, but some advice for posting to Github in general.

If you add ``` before on the lines before and after your stack trace, it formats it as code and makes it easier to read.

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 436, in wrap_socket
cnx.do_handshake()
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1167, in _raise_ssl_error
raise SysCallError(-1, "Unexpected EOF")
OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')

Verse: Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 436, in wrap_socket cnx.do_handshake() File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshake self._raise_ssl_error(self._ssl, result) File "/usr/local/lib/python3.5/dist-packages/OpenSSL/SSL.py", line 1167, in _raise_ssl_error raise SysCallError(-1, "Unexpected EOF") OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')

Lukasa commented 7 years ago

Hrm. This error has been caused by the remote peer shutting the connection in our face, causing us to see an EOF that we weren't previously expecting. What does your code look like? I suspect that you're encountering a problem with forking.

a-sharma11 commented 7 years ago

Thanks @Lukasa here is the code that failing

def get(self, token):
        response = requests.get(self.url, auth=HTTPBasicAuth(token, ''))
        response.raise_for_status()
        user = response.json()
        logger = get_logger(__name__, user)
        logger.info("User token is good.")
        return user
Lukasa commented 7 years ago

Hrm, there is no global state here. You're confident that self.url is exactly the same each time? Can you add a print statement to your code to print it each time just to confirm it's identical?

a-sharma11 commented 7 years ago

@Lukasa Yes it should be the same in this case(hardcoded shown below). Also I made one simple request using postman which should have only triggered this once and got the same error. self.url = base_url + "/api/platform/v1/session"

and base_url is const and in this case name of the other linked container

a-sharma11 commented 7 years ago

Turned out i was trying to connect using https when the other server only support http. Sorry about creating this confusion. Everything works great now....maybe a better error message would be nice to have.