# Buggy code
def _handle_concurrent_reply(fn, o, *args, **kwargs):
    # Make first call to o.reply a no op and start the script thread.
    # We must not start the script thread before, as this may lead to a nasty race condition
    # where the script thread replies a different response before the normal reply, which then gets swallowed.

    def run():
        fn(*args, **kwargs)
        # If the script did not call .reply(), we have to do it now.
        reply_proxy()

    script_thread = ScriptThread(target=run)

    reply_proxy = ReplyProxy(o.reply, script_thread)
    o.reply = reply_proxy


# Traceback (ValueError)
Error: Connection killed
Traceback (most recent call last):
File "/home/christofferqa/mitmproxy/venv/bin/mitmdump", line 9, in <module>
load_entry_point('mitmproxy', 'console_scripts', 'mitmdump')()
File "/home/christofferqa/mitmproxy/mitmproxy/main.py", line 102, in mitmdump
master.run()
File "/home/christofferqa/mitmproxy/mitmproxy/dump.py", line 354, in run
return flow.FlowMaster.run(self)
File "/home/christofferqa/mitmproxy/mitmproxy/controller.py", line 128, in run
self.tick(self.masterq, 0.1)
File "/home/christofferqa/mitmproxy/mitmproxy/flow.py", line 828, in tick
return super(FlowMaster, self).tick(q, timeout)
File "/home/christofferqa/mitmproxy/mitmproxy/controller.py", line 113, in tick
self.handle(*msg)
File "/home/christofferqa/mitmproxy/mitmproxy/controller.py", line 135, in handle
m(obj)
File "/home/christofferqa/mitmproxy/mitmproxy/dump.py", line 337, in handle_response
self._process_flow(f)
File "/home/christofferqa/mitmproxy/mitmproxy/dump.py", line 321, in _process_flow
self.state.delete_flow(f)
File "/home/christofferqa/mitmproxy/mitmproxy/flow.py", line 584, in delete_flow
self.flows._remove(f)
File "/home/christofferqa/mitmproxy/mitmproxy/flow.py", line 496, in _remove
self._list.remove(f)
ValueError: list.remove(x): x not in list
Traceback (most recent call last):
File "/home/christofferqa/mitmproxy/mitmproxy/proxy/server.py", line 121, in handle
root_layer()
TypeError: 'NoneType' object is not callable

mitmproxy has crashed!
Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy
----------------------------------------
Error in processing of request from 127.0.0.1:41084
Traceback (most recent call last):
File "/home/christofferqa/mitmproxy/netlib/tcp.py", line 854, in connection_thread
self.handle_client_connection(connection, client_address)
File "/home/christofferqa/mitmproxy/mitmproxy/proxy/server.py", line 64, in handle_client_connection
h.handle()
File "/home/christofferqa/mitmproxy/mitmproxy/proxy/server.py", line 152, in handle
self.channel.tell("clientdisconnect", root_layer)
File "/home/christofferqa/mitmproxy/mitmproxy/controller.py", line 68, in tell
m.reply = DummyReply()
AttributeError: 'NoneType' object has no attribute 'reply'

# Fixed code
def _handle_concurrent_reply(fn, o, *args, **kwargs):
    # Make first call to o.reply a no op and start the script thread.
    # We must not start the script thread before, as this may lead to a nasty race condition
    # where the script thread replies a different response before the normal reply, which then gets swallowed.

    def run():
        fn(*args, **kwargs)
        # If the script did not call .reply(), we have to do it now.
        reply_proxy.done()

    script_thread = ScriptThread(target=run)

    reply_proxy = ReplyProxy(o.reply, script_thread)
    o.reply = reply_proxy