Skip to content

Eventlet raises a different exception on SSL timeouts vs standard Python #692

@ShaneHarvey

Description

@ShaneHarvey

With standard Python 3.2+ (not monkey patched) timeouts on ssl connections raise socket.timeout: The read operation timed out. socket.timeout is a standard class that applications can use to identify network timeouts vs other network errors. For example:

# import eventlet
# eventlet.monkey_patch()

import socket
import ssl

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.SOL_TCP)
sock.settimeout(2)  # Set timeout to 2 seconds.
sock.connect(("www.python.org", 443))
print('client socket connected')
client_context = ssl.create_default_context()
client_conn = client_context.wrap_socket(sock, server_hostname="www.python.org")
print('client TLS connected')
client_conn.read()  # This line will timeout.

The above example produces to following output:

$ python3 eventlet_ssl_timeout_bug.py
client socket connected
client TLS connected
Traceback (most recent call last):
  File "eventlet_ssl_timeout_bug.py", line 14, in <module>
    client_conn.read()  # This line will timeout.
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1103, in read
    return self._sslobj.read(len)
socket.timeout: The read operation timed out

However when we uncomment the monkey patching, eventlet raises the more general ssl.SSLError exception:

$ python eventlet_ssl_timeout_bug.py
client socket connected
client TLS connected
Traceback (most recent call last):
  File "eventlet_ssl_timeout_bug.py", line 14, in <module>
    client_conn.read()  # This line will timeout.
  File "/Users/shane/pymongo-pycharm-3.8/lib/python3.8/site-packages/eventlet/green/ssl.py", line 186, in read
    return self._call_trampolining(
  File "/Users/shane/pymongo-pycharm-3.8/lib/python3.8/site-packages/eventlet/green/ssl.py", line 164, in _call_trampolining
    trampoline(self,
  File "/Users/shane/pymongo-pycharm-3.8/lib/python3.8/site-packages/eventlet/hubs/__init__.py", line 159, in trampoline
    return hub.switch()
  File "/Users/shane/pymongo-pycharm-3.8/lib/python3.8/site-packages/eventlet/hubs/hub.py", line 313, in switch
    return self.greenlet.switch()
ssl.SSLError: ('timed out',)

I would consider this a bug because the behavior differs from standard python and code that catches socket.timeout errors will no longer work as expected.

For related issues see:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions