Pull_session doesn't work if i don't include 'from bokeh.server.server import Server' statement which i am not using anywhere

Hello experts,

I was getting this error message even though a sample example code shown in an online course was working fine for the instructor:
OSError: Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)

I tried a lot and accidentally found that my code works if i include below additional import statement but interestingly i am not using it anywhere in my code.
from bokeh.server.server import Server

Here is my code:

from flask import Flask, render_template
from bokeh.embed import server_document,server_session
from bokeh.server.server import Server  # If i comment this, I  get above 'Cannot pull session document...' error. 
from bokeh.client import pull_session

app = Flask(__name__)

#create index page function

def index():
    myurl = "http://localhost:5006/mybokeh"
    session = pull_session(url=myurl)
    bokeh_script = server_session(None, url=myurl, session_id=session.id)
    return render_template("index.html",bokeh_script=bokeh_script)

#run the app
if __name__ == "__main__":

Any ideas why this is happening so?

I’m extremely skeptical that there is any real connection between these two events. The only place in the entire codebase that has that error text is here:

Which can only happen when there really is a bona-fide HTTP ERROR as a result of a connection request that failed. So what I actually think is that there was just some coincidence, and that the Bokeh server could not actually be reached, just as the error states (e.g. it wasn’t running, or hadn’t finished starting, had crashed, was on a different port, etc).

Thanks for your response, Bryan. I left the bokeh server untouched (i.e. left continuously running) and tested by only changing the flask code about 8-12 times by commenting and uncommenting that line. Each time the code ran with that line commented it throws pull_session failed error msg and if i uncomment it works perfectly fine with the same running session.

In fact, before we tried that import statement my colleague and I had spent over a day in figuring out why it’s throwing a pull session error (when bokeh serve is running) until he accidentally found out it worked when he inserted that line. I also tested and found this to be happening on my machine too.

As such the code works with that import statement but i am just curious as to why this is happening and how it was working in an online tutorial without that import.

@Gopi_M I don’t have an explanation for you, because it makes no sense to me. However, I can reproduce he problem, on Windows only. The example/embed/server_session example works just fine on OSX and Linux, which is probably why this bug has not been noticed before. In any case, please file a bug report on GitHub

FWIW My speculation is that this necessary codepath is executed from a transitive import of bokeh.server.torndao

This is necessary because Python 3.8 made a breaking change for Tornado on Winwdows. We added that code to make things work by default in the bokeh.server case but evidently did not make sure it was also present for bokeh.client.

@Gopi_M it would be helpful if you can confirm that the issue (only) affects Windows + Python 3.8, and if so then also filing a bug report.

Hi Bryan, I am using Win10 and Python 3.8.3 and it certainly has this behaviour on my machine (Don’t have other OS for me to try, unfortunately). I have raised a bug report as you suggested. Here is the link

@Gopi_M can you confirm if adding this to your script fixes the issue (i.e. that things work without the server import)?

import asyncio

Hi Bryan, Thanks for looking in to this. It works fine if i include the 2 lines you provided. (and with the earlier import statement commented out). Hope this helps.

1 Like