Repository: bokeh/bokeh

Issue:
Widgets examples become unresponsive
After about four or five widget interactions, the examples `taylor_server.py`, `population_server.py`, etc become almost completely unresponsive. This happens in both debug and non debug mode. CPU utilization can also take a huge spike I was seeing 100% across three cpus. Note there is a also continuous stream of updates from the `bokeh-server`:

```
2014-06-10 12:43:00,999:INFO:bokeh.server.models.docs:num models: 31
2014-06-10 12:43:01,439:INFO:bokeh.server.start:GET /bokeh/bb/ee2d02b0-10da-48e3-925e-8094bb530caa/ 200 OK 17278 760.5ms
2014-06-10 12:43:03,347:INFO:bokeh.server.models.docs:num models: 31
2014-06-10 12:43:03,751:INFO:bokeh.server.start:GET /bokeh/bb/ee2d02b0-10da-48e3-925e-8094bb530caa/ 200 OK 17278 717.9ms

[snip hundreds of identical logs]

2014-06-10 12:43:39,437:INFO:bokeh.server.models.docs:num models: 31
2014-06-10 12:43:39,848:INFO:bokeh.server.start:GET /bokeh/bb/ee2d02b0-10da-48e3-925e-8094bb530caa/ 200 OK 17278 731.2ms
```

This was happening even when nothing was going on with the widgets or plot. I could imagine some kind of polling but it is constantly exchanging everything? 

@mattpap any thoughts?



Discussion:
Just poking here to say I saw this too (yesterday)... In my case, it made my laptop fully unresponsive, so I have to restart it all...

Isn't this the same as https://github.com/ContinuumIO/bokeh/pull/550#issuecomment-44410402 (last paragraph)?

Yes, but that is merged/closed, we has discussed resolving the remaining issues separately, this issue is just to keep track. 

Yeah, I know, but I wasn't sure if this is the same issue or not.

Oh, yes I think it is, sorry I should have referenced that other comment. 

What's the purpose of [this](https://github.com/ContinuumIO/bokeh/blob/master/bokeh/session.py#L369) line? If removed, at least `taylor_server.py` works fine and doesn't hang after very many interactions. The reason it hangs in `master` is because `PlotContext.children` grows with bogus objects, making JSON large and makes processing time increase significantly (a few megs / over a second after a few interactions).

I don't know what that is for, @hhuuggoo do you recognize it?

This line was added in 2f4fc3c4e2399a244e217d9a69f43787175ee6f1, which was supposed to fix polling (?) after `session-refactor`.

Offhand, I think maybe it was intended to be something like:

```
children = set(plot_context_json['attributes']['children']) 
children.update(set(x.get_ref() for x in doc._plotcontext.children))
plot_context_json['attributes']['children'] = list(children)
```

but I could be wrong.

That might be the case.

@hhuuggoo is currently in a plane and doesn't land for about three more hours. I would suggest trying that fix above and then seeing if Hugo concurs about it in the morning. 

I think the set solution should work - That code is primarily in there because if you already have some plots done locally, and you try to sync up with the server, we're trying to merge what the server has with what we have locally without blowing away the local copy


Resolving patch:
diff --git a/bokeh/session.py b/bokeh/session.py
--- a/bokeh/session.py
+++ b/bokeh/session.py
@@ -366,7 +366,11 @@ def load_document(self, doc):
         plot_contexts = [x for x in json_objs if x['type'] == 'PlotContext']
         other_objects = [x for x in json_objs if x['type'] != 'PlotContext']
         plot_context_json = plot_contexts[0]
-        plot_context_json['attributes']['children'] += [x.get_ref() for x in doc._plotcontext.children]
+        children = set([x['id'] for x in plot_context_json['attributes']['children']])
+        for child in doc._plotcontext.children:
+            ref = child.get_ref()
+            if ref['id'] not in children:
+                plot_context_json['attributes']['children'].append(child.get_ref())
         doc.docid = self.docid
         doc._plotcontext._id = plot_context_json['id']
         doc.load(plot_context_json, *other_objects)


Test patch: