Repository: dask/distributed

Issue:
Clicking on "events" on main Bokeh page gives error.
I just tried clicking on "events" on a scheduler which currently has no tasks and no workers. I got back "500: Internal Server Error" and the following error in the logs.

```
HTTPServerRequest(protocol='http', host='35.189.89.148:8787', method='GET', uri='/events', version='HTTP/1.1', remote_ip='86.155.0.172', headers={'Accept-Language': 'en-GB,en;q=0.8,en-US;q=0.6', 'Accept-Encoding': 'gzip, deflate', 'Host': '35.189.89.148:8787', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36', 'Connection': 'keep-alive', 'Referer': 'http://35.189.89.148:8787/', 'Upgrade-Insecure-Requests': '1'})
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1511, in _execute
    result = yield result
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1055, in run
    value = future.result()
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1063, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/server/views/doc_handler.py", line 21, in get
    session = yield self.get_session()
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1055, in run
    value = future.result()
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1063, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/server/views/session_handler.py", line 43, in get_session
    session = yield self.application_context.create_session_if_needed(session_id, self.request)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1055, in run
    value = future.result()
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1069, in run
    yielded = self.gen.send(value)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/server/application_context.py", line 182, in create_session_if_needed
    self._application.initialize_document(doc)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/application/application.py", line 257, in initialize_document
    h.modify_document(doc)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/application/handlers/function.py", line 82, in modify_document
    self._func(doc)
  File "/home/russ_bubley/.local/lib/python2.7/site-packages/distributed/bokeh/scheduler.py", line 935, in events_doc
    events.update()
  File "/home/russ_bubley/.local/lib/python2.7/site-packages/distributed/bokeh/scheduler.py", line 539, in update
    log = [log[-i] for i in range(1, n + 1)]
IndexError: deque index out of range"   
```

It also seemed to result in a bunch of client connections being broken, although I couldn't replicate that when I tried a second time.

Distributed master @dd269f4 (about a week ago)
bokeh==0.12.9


Discussion:
Yeah, there is a reason why we don't list that page on the header.  It's quite buggy.  Even when it works it has issues.



Resolving patch:
diff --git a/distributed/bokeh/scheduler.py b/distributed/bokeh/scheduler.py
--- a/distributed/bokeh/scheduler.py
+++ b/distributed/bokeh/scheduler.py
@@ -536,7 +536,8 @@ def update(self):
         with log_errors():
             log = self.scheduler.events[self.name]
             n = self.scheduler.event_counts[self.name] - self.last
-            log = [log[-i] for i in range(1, n + 1)]
+            if log:
+                log = [log[-i] for i in range(1, n + 1)]
             self.last = self.scheduler.event_counts[self.name]
 
             if log:


Test patch: