Repository: mitmproxy/mitmproxy

Issue:
Deleting the last flow from within the flowlist crashes mitmproxy
##### Steps to reproduce the problem:

1. `mitmproxy`
2. `n` `enter` `enter` `d`

```
Traceback (most recent call last):
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/master.py", line 216, in run
    self.loop.run()
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 278, in run
    self._run()
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 376, in _run
    self.event_loop.run()                                                                                       [*:8080]
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 682, in run
    self._loop()
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 719, in _loop
    self._watch_files[fd]()
  File "/home/user/venv/lib/python3.5/site-packages/urwid/raw_display.py", line 393, in <lambda>
    event_loop, callback, self.get_available_raw_input())
  File "/home/user/venv/lib/python3.5/site-packages/urwid/raw_display.py", line 493, in parse_input
    callback(processed, processed_codes)
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 403, in _update
    self.process_input(keys)
  File "/home/user/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 503, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/window.py", line 312, in keypress
    k
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/keymap.py", line 130, in handle
    return self.executor(b.command)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/commandexecutor.py", line 17, in __call__
    ret = self.master.commands.call(cmd)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/command.py", line 221, in call
    return self.call_args(parts[0], parts[1:])
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/command.py", line 212, in call_args
    return self.commands[path].call(args)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/command.py", line 101, in call
    ret = self.func(*pargs)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/command.py", line 251, in wrapper
    return function(*args, **kwargs)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/addons/view.py", line 413, in remove
    self.sig_view_remove.send(self, flow=f, index=idx)
  File "/home/user/venv/lib/python3.5/site-packages/blinker/base.py", line 267, in send
    for receiver in self.receivers_for(sender)]
  File "/home/user/venv/lib/python3.5/site-packages/blinker/base.py", line 267, in <listcomp>
    for receiver in self.receivers_for(sender)]
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/addons/view.py", line 566, in _sig_view_remove
    self.flow = None
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/addons/view.py", line 547, in flow
    self.sig_change.send(self)
  File "/home/user/venv/lib/python3.5/site-packages/blinker/base.py", line 267, in send
    for receiver in self.receivers_for(sender)]
  File "/home/user/venv/lib/python3.5/site-packages/blinker/base.py", line 267, in <listcomp>
    for receiver in self.receivers_for(sender)]
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/window.py", line 209, in focus_changed
    i.call("focus_changed")
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/window.py", line 118, in call
    getattr(self.top_window(), name)(*args, **kwargs)
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/flowview.py", line 231, in focus_changed
    self.body.focus_changed()
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/flowview.py", line 58, in focus_changed
    self.show()
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/tabs.py", line 52, in show
    txt = self.tabs[i][0]()
  File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/flowview.py", line 69, in tab_request
    if self.flow.intercepted and not self.flow.response:
AttributeError: 'NoneType' object has no attribute 'intercepted'
```

##### System information

Mitmproxy: 3.0.0.dev1147 (commit b7db304)
Python:    3.5.2
OpenSSL:   OpenSSL 1.1.0g  2 Nov 2017
Platform:  Linux-4.4.0-43-Microsoft-x86_64-with-Ubuntu-16.04-xenial


Discussion:

Resolving patch:
diff --git a/mitmproxy/tools/console/flowview.py b/mitmproxy/tools/console/flowview.py
--- a/mitmproxy/tools/console/flowview.py
+++ b/mitmproxy/tools/console/flowview.py
@@ -55,7 +55,9 @@ def focus_changed(self):
                 (self.tab_response, self.view_response),
                 (self.tab_details, self.view_details),
             ]
-        self.show()
+            self.show()
+        else:
+            self.master.window.pop()
 
     @property
     def view(self):


Test patch: