# Buggy code
def setup_events(self):
    ## hack - what we want to do is execute the update callback once
    ## and only if some properties in the graph have changed
    ## so we set should_update to be True in setup_events, and
    ## set it to be false as soon as the callback is done
    if not self.name:
        return
    for k in self.__dict__.keys():
        if k.startswith("_func"):
            self.__dict__.pop(k)
    counter = 0
    if not self.update_registry.get(self.name):
        name = "_func%d" % counter
        func = self.create_registry[self.name]
        setattr(self, name, self.callback(func))
        for widget_name in self.widget_list:
            obj = self.objects.get(widget_name)
            if obj:
                for attr in obj.class_properties():
                    obj.on_change(attr, self, name)
        return
    for selectors, func in self.update_registry[self.name]:
        # hack because we lookup callbacks by func name
        name = "_func%d" % counter
        counter += 1
        setattr(self, name, self.callback(func))
        for selector in selectors:
            if isinstance(selector, string_types):
                self.widget_dict[selector].on_change("value", self, name)
                continue
            elif isinstance(selector, tuple):
                selector, attrs = selector
            else:
                attrs = None
            for obj in self.select(selector):
                if obj == self:
                    continue
                if attrs:
                    toiter = attrs
                else:
                    toiter = obj.class_properties()
                for attr in toiter:
                    obj.on_change(attr, self, name)
    self.set_debounce()


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/views/backbone.py", line 208, in _handle_specific_model_put
return _handle_specific_model(docid, typename, id, request.method)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/crossdomain.py", line 34, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/views/backbone.py", line 169, in _handle_specific_model
return update(docid, typename, id)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/views/bbauth.py", line 21, in wrapper
return func(*args, **kwargs)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/views/backbone.py", line 269, in update
t.load()
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/server/serverbb.py", line 376, in load
clientdoc.load(*temporary_json, events='none', dirty=False)
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/document.py", line 197, in load
m.setup_events()
File "/home/till/programming/miniconda3/lib/python3.4/site-packages/bokeh/models/widgets/layouts.py", line 143, in setup_events
for k in self.__dict__.keys():
RuntimeError: dictionary changed size during iteration

# Fixed code
def setup_events(self):
    ## hack - what we want to do is execute the update callback once
    ## and only if some properties in the graph have changed
    ## so we set should_update to be True in setup_events, and
    ## set it to be false as soon as the callback is done
    if not self.name:
        return
    to_delete = []
    for k in self.__dict__.keys():
        if k.startswith("_func"):
            to_delete.append(k)
    for k in to_delete:
        self.__dict__.pop(k)
    counter = 0
    if not self.update_registry.get(self.name):
        name = "_func%d" % counter
        func = self.create_registry[self.name]
        setattr(self, name, self.callback(func))
        for widget_name in self.widget_list:
            obj = self.objects.get(widget_name)
            if obj:
                for attr in obj.class_properties():
                    obj.on_change(attr, self, name)
        return
    for selectors, func in self.update_registry[self.name]:
        # hack because we lookup callbacks by func name
        name = "_func%d" % counter
        counter += 1
        setattr(self, name, self.callback(func))
        for selector in selectors:
            if isinstance(selector, string_types):
                self.widget_dict[selector].on_change("value", self, name)
                continue
            elif isinstance(selector, tuple):
                selector, attrs = selector
            else:
                attrs = None
            for obj in self.select(selector):
                if obj == self:
                    continue
                if attrs:
                    toiter = attrs
                else:
                    toiter = obj.class_properties()
                for attr in toiter:
                    obj.on_change(attr, self, name)
    self.set_debounce()