# Buggy code
def _get_first_line(rfile):
    try:
        line = rfile.readline()
        if line == b"\r\n" or line == b"\n":
            # Possible leftover from previous message
            line = rfile.readline()
    except exceptions.TcpDisconnect:
        raise exceptions.HttpReadDisconnect("Remote disconnected")
    if not line:
        raise exceptions.HttpReadDisconnect("Remote disconnected")
    return line.strip()


# Traceback (OpenSSL.SSL.SysCallError)
mitmdump -T --host -s analysis.py

192.168.0.103:49825: clientconnect
192.168.0.103:36334: Traceback (most recent call last):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 211, in read
data = self.o.read(rlen)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1304, in recv
self._raise_ssl_error(self._ssl, result)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1166, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (60, 'ETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/server.py", line 119, in handle
root_layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/modes/transparent_proxy.py", line 19, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 176, in __call__
if not self._process_flow(flow):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 227, in _process_flow
request = self.read_request_headers(f)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 14, in read_request_headers
http1.read_request_head(self.client_conn.rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 52, in read_request_head
form, method, scheme, host, port, path, http_version = _read_request_line(rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 240, in _read_request_line
line = _get_first_line(rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 227, in _get_first_line
line = rfile.readline()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 251, in readline
ch = self.read(1)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 233, in read
raise exceptions.TlsException(str(e))
mitmproxy.exceptions.TlsException: (60, 'ETIMEDOUT')

Traceback (most recent call last):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 211, in read
data = self.o.read(rlen)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1304, in recv
self._raise_ssl_error(self._ssl, result)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1166, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (60, 'ETIMEDOUT')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/server.py", line 119, in handle
root_layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/modes/transparent_proxy.py", line 19, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
layer()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 176, in __call__
if not self._process_flow(flow):
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http.py", line 227, in _process_flow
request = self.read_request_headers(f)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/proxy/protocol/http1.py", line 14, in read_request_headers
http1.read_request_head(self.client_conn.rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 52, in read_request_head
form, method, scheme, host, port, path, http_version = _read_request_line(rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 240, in _read_request_line
line = _get_first_line(rfile)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/http/http1/read.py", line 227, in _get_first_line
line = rfile.readline()
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 251, in readline
ch = self.read(1)
File "/Users/mycomputer/development/python/analysis/.venv/lib/python3.5/site-packages/mitmproxy/net/tcp.py", line 233, in read
raise exceptions.TlsException(str(e))
mitmproxy.exceptions.TlsException: (60, 'ETIMEDOUT')

mitmproxy has crashed!
Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy

# Fixed code
def _get_first_line(rfile):
    try:
        line = rfile.readline()
        if line == b"\r\n" or line == b"\n":
            # Possible leftover from previous message
            line = rfile.readline()
    except (exceptions.TcpDisconnect, exceptions.TlsException):
        raise exceptions.HttpReadDisconnect("Remote disconnected")
    if not line:
        raise exceptions.HttpReadDisconnect("Remote disconnected")
    return line.strip()