# Buggy code
def replay_request(self, f, block=False):
    """
    Returns an http_replay.RequestReplayThred object.
    May raise exceptions.ReplayError.
    """
    if f.live:
        raise exceptions.ReplayError("Can't replay live flow.")
    if f.intercepted:
        raise exceptions.ReplayError("Can't replay intercepted flow.")
    if f.request.raw_content is None:
        raise exceptions.ReplayError("Can't replay flow with missing content.")
    if not f.request:
        raise exceptions.ReplayError("Can't replay flow with missing request.")

    f.backup()
    f.request.is_replay = True

    # TODO: We should be able to remove this.
    if "Content-Length" in f.request.headers:
        f.request.headers["Content-Length"] = str(len(f.request.raw_content))

    f.response = None
    f.error = None
    # FIXME: process through all addons?
    # self.process_new_request(f)
    rt = http_replay.RequestReplayThread(
        self.server.config, f, self.event_queue, self.should_exit
    )
    rt.start()  # pragma: no cover
    if block:
        rt.join()
    return rt


# Traceback (AttributeError)
user@user-ubuntu:~$ mitmproxy
Exception ignored in: <bound method Reply.__del__ of <mitmproxy.controller.Reply object at 0x7f69de8646a0>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 269, in __del__
raise exceptions.ControlException("Un-acked message: %s" % self.obj)
mitmproxy.exceptions.ControlException: Un-acked message: <HTTPFlow
request = Request(GET www.example.com:80/)
server_conn = <ServerConnection: www.example.com:80>>
Exception ignored in: <bound method Reply.__del__ of <mitmproxy.controller.Reply object at 0x7f69de864da0>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 269, in __del__
raise exceptions.ControlException("Un-acked message: %s" % self.obj)
mitmproxy.exceptions.ControlException: Un-acked message: <HTTPFlow
request = Request(GET www.example.com:80/)
response = Response(200 OK, text/html, 1.24k)
server_conn = <ServerConnection: www.example.com:80>>
rTraceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/console/master.py", line 502, in run
self.loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 715, in _loop
alarm_callback()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 164, in cb
callback(self, user_data)
File "/media/sf_git/mitmproxy/mitmproxy/console/master.py", line 448, in ticker
changed = self.tick(timeout=0)
File "/media/sf_git/mitmproxy/mitmproxy/flow/master.py", line 140, in tick
return super(FlowMaster, self).tick(timeout)
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 136, in tick
handle_func(obj)
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 219, in wrapper
master.addons(f.__name__, message)
File "/media/sf_git/mitmproxy/mitmproxy/addons.py", line 64, in __call__
self.invoke(i, name, *args, **kwargs)
File "/media/sf_git/mitmproxy/mitmproxy/addons.py", line 60, in invoke
func(*args, **kwargs)
File "/media/sf_git/mitmproxy/mitmproxy/builtins/setheaders.py", line 39, in response
self.run(flow, flow.response.headers)
AttributeError: 'NoneType' object has no attribute 'headers'

mitmproxy has crashed!
Please lodge a bug report at:
https://github.com/mitmproxy/mitmproxy
Shutting down...
rrrrrrrrrrException ignored in: <bound method Reply.__del__ of <mitmproxy.controller.Reply object at 0x7f69df0c2c18>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 269, in __del__
mitmproxy.exceptions.ControlException: Un-acked message: <HTTPFlow
request = Request(GET www.example.com:80/)
response = Response(200 OK, text/html, 1.24k)
error = Connection killed
server_conn = <ServerConnection: www.example.com:80>>
user@user-ubuntu:~$ mitmproxy
rException ignored in: <bound method Reply.__del__ of <mitmproxy.controller.Reply object at 0x7f17c5270ac8>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 269, in __del__
raise exceptions.ControlException("Un-acked message: %s" % self.obj)
mitmproxy.exceptions.ControlException: Un-acked message: <HTTPFlow
request = Request(GET www.example.com:80/)
response = Response(200 OK, text/html, 1.24k)
server_conn = <ServerConnection: www.example.com:80>>
rException ignored in: <bound method Reply.__del__ of <mitmproxy.controller.Reply object at 0x7f17c5291908>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 269, in __del__
raise exceptions.ControlException("Un-acked message: %s" % self.obj)
mitmproxy.exceptions.ControlException: Un-acked message: <HTTPFlow
request = Request(GET www.example.com:80/)
response = Response(200 OK, text/html, 1.24k)
server_conn = <ServerConnection: www.example.com:80>>
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/console/master.py", line 502, in run
self.loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 715, in _loop
alarm_callback()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 164, in cb
callback(self, user_data)
File "/media/sf_git/mitmproxy/mitmproxy/console/master.py", line 448, in ticker
changed = self.tick(timeout=0)
File "/media/sf_git/mitmproxy/mitmproxy/flow/master.py", line 140, in tick
return super(FlowMaster, self).tick(timeout)
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 136, in tick
handle_func(obj)
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 219, in wrapper
master.addons(f.__name__, message)
File "/media/sf_git/mitmproxy/mitmproxy/addons.py", line 64, in __call__
self.invoke(i, name, *args, **kwargs)
File "/media/sf_git/mitmproxy/mitmproxy/addons.py", line 60, in invoke
func(*args, **kwargs)
File "/media/sf_git/mitmproxy/mitmproxy/builtins/setheaders.py", line 39, in response
self.run(flow, flow.response.headers)
AttributeError: 'NoneType' object has no attribute 'headers'

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

# Fixed code
def replay_request(self, f, block=False):
    """
    Replay a HTTP request to receive a new response from the server.

    Args:
        f: The flow to replay.
        block: If True, this function will wait for the replay to finish.
            This causes a deadlock if activated in the main thread.

    Returns:
        The thread object doing the replay.

    Raises:
        exceptions.ReplayException, if the flow is in a state
        where it is ineligible for replay.
    """

    if f.live:
        raise exceptions.ReplayException("Can't replay live flow.")
    if f.intercepted:
        raise exceptions.ReplayException("Can't replay intercepted flow.")
    if f.request.raw_content is None:
        raise exceptions.ReplayException("Can't replay flow with missing content.")
    if not f.request:
        raise exceptions.ReplayException("Can't replay flow with missing request.")

    f.backup()
    f.request.is_replay = True

    f.response = None
    f.error = None

    rt = http_replay.RequestReplayThread(
        self.server.config, f, self.event_queue, self.should_exit
    )
    rt.start()  # pragma: no cover
    if block:
        rt.join()
    return rt