# Buggy code
def handle_regular_connect(self, f):
    self.connect_request = True

    try:
        self.set_server((f.request.host, f.request.port))
    except (exceptions.ProtocolException, exceptions.NetlibException) as e:
        # HTTPS tasting means that ordinary errors like resolution
        # and connection errors can happen here.
        self.send_error_response(502, repr(e))
        f.error = flow.Error(str(e))
        self.channel.ask("error", f)
        return False

    if f.response:
        resp = f.response
    else:
        resp = http.make_connect_response(f.request.data.http_version)

    self.send_response(resp)

    if is_ok(resp.status_code):
        layer = self.ctx.next_layer(self)
        layer()

    return False


# Traceback (TimeoutError)
127.0.0.1:59424: clientconnect
127.0.0.1:59424: Set new server address: ('nationalgeographic.com', 443)
127.0.0.1:59424: serverconnect
-> nationalgeographic.com:443
127.0.0.1:59386: Establish TLS with client
127.0.0.1:59386: ALPN for client: b'http/1.1'
127.0.0.1:59386: Server connection to sakura.ne.jp:443 failed: Error connecting to "sakura.ne.jp": [Errno 110] Connection timed out
127.0.0.1:59386: Traceback (most recent call last):
File "/home/ubuntu/mitmproxy/mitmproxy/net/tcp.py", line 763, in connect
connection.connect(self.address())
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/base.py", line 180, in connect
self.server_conn.connect()
File "/home/ubuntu/mitmproxy/mitmproxy/connections.py", line 206, in connect
tcp.TCPClient.connect(self)
File "/home/ubuntu/mitmproxy/mitmproxy/net/tcp.py", line 768, in connect
(self.address.host, err)
mitmproxy.exceptions.TcpException: Error connecting to "sakura.ne.jp": [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/server.py", line 119, in handle
root_layer()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/modes/http_proxy.py", line 9, in __call__
layer()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
layer()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
layer()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/http.py", line 177, in __call__
if not self._process_flow(flow):
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/http.py", line 246, in _process_flow
return self.handle_regular_connect(f)
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/http.py", line 204, in handle_regular_connect
layer()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 376, in __call__
self._establish_tls_with_client_and_server()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 451, in _establish_tls_with_client_and_server
self.ctx.connect()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/tls.py", line 397, in connect
self.ctx.connect()
File "/home/ubuntu/mitmproxy/mitmproxy/proxy/protocol/base.py", line 184, in connect
repr(self.server_conn.address), str(e)
mitmproxy.exceptions.ProtocolException: Server connection to sakura.ne.jp:443 failed: Error connecting to "sakura.ne.jp": [Errno 110] Connection timed out

127.0.0.1:59386: clientdisconnect

# Fixed code
def handle_regular_connect(self, f):
    self.connect_request = True

    try:
        self.set_server((f.request.host, f.request.port))

        if f.response:
            resp = f.response
        else:
            resp = http.make_connect_response(f.request.data.http_version)

        self.send_response(resp)

        if is_ok(resp.status_code):
            layer = self.ctx.next_layer(self)
            layer()
    except (exceptions.ProtocolException, exceptions.NetlibException) as e:
        # HTTPS tasting means that ordinary errors like resolution
        # and connection errors can happen here.
        self.send_error_response(502, repr(e))
        f.error = flow.Error(str(e))
        self.channel.ask("error", f)
        return False

    return False