# Buggy code
def handle(self):
    self.log("clientconnect", "info")

    root_layer = self._create_root_layer()
    root_layer = self.channel.ask("clientconnect", root_layer)
    if root_layer == Kill:

        def root_layer():
            raise Kill()

    try:
        root_layer()
    except Kill:
        self.log("Connection killed", "info")
    except ProtocolException as e:
        if isinstance(e, ClientHandshakeException):
            self.log(
                "Client Handshake failed. "
                "The client may not trust the proxy's certificate for {}.".format(
                    e.server
                ),
                "error",
            )
            self.log(repr(e), "debug")
        else:
            self.log(repr(e), "info")

            self.log(traceback.format_exc(), "debug")
        # If an error propagates to the topmost level,
        # we send an HTTP error response, which is both
        # understandable by HTTP clients and humans.
        try:
            error_response = make_error_response(502, repr(e))
            self.client_conn.send(assemble_response(error_response))
        except TcpException:
            pass
    except Exception:
        self.log(traceback.format_exc(), "error")
        print(traceback.format_exc(), file=sys.stderr)
        print("mitmproxy has crashed!", file=sys.stderr)
        print(
            "Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy",
            file=sys.stderr,
        )

    self.log("clientdisconnect", "info")
    self.channel.tell("clientdisconnect", root_layer)
    self.client_conn.finish()


# Traceback (AttributeError)
./mitmdump.run.sh: line 2: 14504 Segmentation fault      mitmdump -p 59873 > network
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/libmproxy/proxy/server.py", line 120, in handle
root_layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/proxy/modes/http_proxy.py", line 10, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/tls.py", line 272, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 130, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 323, in __call__
self.handle_regular_mode_connect(request)
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 409, in handle_regular_mode_connect
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/tls.py", line 272, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 130, in __call__
layer()
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 340, in __call__
self.get_response_from_server(flow)
File "/usr/local/lib/python2.7/site-packages/libmproxy/protocol/http.py", line 471, in get_response_from_server
if flow.response.stream:
AttributeError: 'NoneType' object has no attribute 'response'

# Fixed code
def handle(self):
    self.log("clientconnect", "info")

    root_layer = self._create_root_layer()

    try:
        root_layer = self.channel.ask("clientconnect", root_layer)
        root_layer()
    except Kill:
        self.log("Connection killed", "info")
    except ProtocolException as e:
        if isinstance(e, ClientHandshakeException):
            self.log(
                "Client Handshake failed. "
                "The client may not trust the proxy's certificate for {}.".format(
                    e.server
                ),
                "error",
            )
            self.log(repr(e), "debug")
        else:
            self.log(repr(e), "info")

            self.log(traceback.format_exc(), "debug")
        # If an error propagates to the topmost level,
        # we send an HTTP error response, which is both
        # understandable by HTTP clients and humans.
        try:
            error_response = make_error_response(502, repr(e))
            self.client_conn.send(assemble_response(error_response))
        except TcpException:
            pass
    except Exception:
        self.log(traceback.format_exc(), "error")
        print(traceback.format_exc(), file=sys.stderr)
        print("mitmproxy has crashed!", file=sys.stderr)
        print(
            "Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy",
            file=sys.stderr,
        )

    self.log("clientdisconnect", "info")
    self.channel.tell("clientdisconnect", root_layer)
    self.client_conn.finish()