# Buggy code
def keypress(self, xxx_todo_changeme, key):
    (maxcol,) = xxx_todo_changeme
    key = common.shortcuts(key)
    if key == "a":
        self.flow.accept_intercept(self.master)
        signals.flowlist_change.send(self)
    elif key == "d":
        self.flow.kill(self.master)
        self.state.delete_flow(self.flow)
        signals.flowlist_change.send(self)
    elif key == "D":
        f = self.master.duplicate_flow(self.flow)
        self.master.state.set_focus_flow(f)
        signals.flowlist_change.send(self)
    elif key == "m":
        if self.state.flow_marked(self.flow):
            self.state.set_flow_marked(self.flow, False)
        else:
            self.state.set_flow_marked(self.flow, True)
        signals.flowlist_change.send(self)
    elif key == "M":
        if self.state.mark_filter:
            self.state.disable_marked_filter()
        else:
            self.state.enable_marked_filter()
        signals.flowlist_change.send(self)
    elif key == "r":
        r = self.master.replay_request(self.flow)
        if r:
            signals.status_message.send(message=r)
        signals.flowlist_change.send(self)
    elif key == "S":
        if not self.master.server_playback:
            signals.status_prompt_onekey.send(
                prompt="Server Replay",
                keys=(
                    ("all flows", "a"),
                    ("this flow", "t"),
                    ("file", "f"),
                ),
                callback=self.server_replay_prompt,
            )
        else:
            signals.status_prompt_onekey.send(
                prompt="Stop current server replay?",
                keys=(
                    ("yes", "y"),
                    ("no", "n"),
                ),
                callback=self.stop_server_playback_prompt,
            )
    elif key == "U":
        for f in self.state.flows:
            self.state.set_flow_marked(f, False)
        signals.flowlist_change.send(self)
    elif key == "V":
        if not self.flow.modified():
            signals.status_message.send(message="Flow not modified.")
            return
        self.state.revert(self.flow)
        signals.flowlist_change.send(self)
        signals.status_message.send(message="Reverted.")
    elif key == "w":
        signals.status_prompt_onekey.send(
            self,
            prompt="Save",
            keys=(
                ("all flows", "a"),
                ("this flow", "t"),
                ("marked flows", "m"),
            ),
            callback=self.save_flows_prompt,
        )
    elif key == "X":
        self.flow.kill(self.master)
    elif key == "enter":
        if self.flow.request:
            self.master.view_flow(self.flow)
    elif key == "|":
        signals.status_prompt_path.send(
            prompt="Send flow to script",
            callback=self.master.run_script_once,
            args=(self.flow,),
        )
    elif key == "E":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to file",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.ask_save_path),
        )
    elif key == "C":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to clipboard",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.copy_to_clipboard_or_prompt),
        )
    elif key == "b":
        common.ask_save_body(None, self.flow)
    else:
        return key


# Traceback (mitmproxy.exceptions.ControlException)
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 719, in _loop
self._watch_files[fd]()
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 393, in <lambda>
event_loop, callback, self.get_available_raw_input())
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 493, in parse_input
callback(processed, processed_codes)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 403, in _update
self.process_input(keys)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 503, in process_input
k = self._topmost_widget.keypress(self.screen_size, k)
File "/media/sf_git/mitmproxy/mitmproxy/console/window.py", line 42, in keypress
k = super(self.__class__, self).keypress(size, k)
File "/usr/local/lib/python3.5/dist-packages/urwid/container.py", line 1128, in keypress
return self.body.keypress( (maxcol, remaining), key )
File "/media/sf_git/mitmproxy/mitmproxy/console/flowlist.py", line 408, in keypress
return urwid.ListBox.keypress(self, size, key)
File "/usr/local/lib/python3.5/dist-packages/urwid/listbox.py", line 985, in keypress
key = focus_widget.keypress((maxcol,),key)
File "/media/sf_git/mitmproxy/mitmproxy/console/flowlist.py", line 258, in keypress
self.flow.kill(self.master)
File "/media/sf_git/mitmproxy/mitmproxy/models/flow.py", line 154, in kill
self.reply.kill()
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 255, in kill
self.send(exceptions.Kill)
File "/media/sf_git/mitmproxy/mitmproxy/controller.py", line 262, in send
raise exceptions.ControlException("Message already acked.")
mitmproxy.exceptions.ControlException: Message already acked.

# Fixed code
def keypress(self, xxx_todo_changeme, key):
    (maxcol,) = xxx_todo_changeme
    key = common.shortcuts(key)
    if key == "a":
        self.flow.accept_intercept(self.master)
        signals.flowlist_change.send(self)
    elif key == "d":
        if not self.flow.reply.acked:
            self.flow.kill(self.master)
        self.state.delete_flow(self.flow)
        signals.flowlist_change.send(self)
    elif key == "D":
        f = self.master.duplicate_flow(self.flow)
        self.master.state.set_focus_flow(f)
        signals.flowlist_change.send(self)
    elif key == "m":
        if self.state.flow_marked(self.flow):
            self.state.set_flow_marked(self.flow, False)
        else:
            self.state.set_flow_marked(self.flow, True)
        signals.flowlist_change.send(self)
    elif key == "M":
        if self.state.mark_filter:
            self.state.disable_marked_filter()
        else:
            self.state.enable_marked_filter()
        signals.flowlist_change.send(self)
    elif key == "r":
        r = self.master.replay_request(self.flow)
        if r:
            signals.status_message.send(message=r)
        signals.flowlist_change.send(self)
    elif key == "S":
        if not self.master.server_playback:
            signals.status_prompt_onekey.send(
                prompt="Server Replay",
                keys=(
                    ("all flows", "a"),
                    ("this flow", "t"),
                    ("file", "f"),
                ),
                callback=self.server_replay_prompt,
            )
        else:
            signals.status_prompt_onekey.send(
                prompt="Stop current server replay?",
                keys=(
                    ("yes", "y"),
                    ("no", "n"),
                ),
                callback=self.stop_server_playback_prompt,
            )
    elif key == "U":
        for f in self.state.flows:
            self.state.set_flow_marked(f, False)
        signals.flowlist_change.send(self)
    elif key == "V":
        if not self.flow.modified():
            signals.status_message.send(message="Flow not modified.")
            return
        self.state.revert(self.flow)
        signals.flowlist_change.send(self)
        signals.status_message.send(message="Reverted.")
    elif key == "w":
        signals.status_prompt_onekey.send(
            self,
            prompt="Save",
            keys=(
                ("all flows", "a"),
                ("this flow", "t"),
                ("marked flows", "m"),
            ),
            callback=self.save_flows_prompt,
        )
    elif key == "X":
        if not self.flow.reply.acked:
            self.flow.kill(self.master)
    elif key == "enter":
        if self.flow.request:
            self.master.view_flow(self.flow)
    elif key == "|":
        signals.status_prompt_path.send(
            prompt="Send flow to script",
            callback=self.master.run_script_once,
            args=(self.flow,),
        )
    elif key == "E":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to file",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.ask_save_path),
        )
    elif key == "C":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to clipboard",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.copy_to_clipboard_or_prompt),
        )
    elif key == "b":
        common.ask_save_body(None, self.flow)
    else:
        return key