# Buggy code
def __init__(self, source, path, argv):
    """

    Args:
        source (str) : python source code

        path (str) : a filename to use in any debugging or error output

        argv (list[str]) : a list of string arguments to make available
            as ``sys.argv`` when the code executes

    """
    self._failed = False
    self._error = None
    self._error_detail = None

    import ast

    self._code = None

    try:
        nodes = ast.parse(source, path)
        self._code = compile(nodes, filename=path, mode="exec", dont_inherit=True)
    except SyntaxError as e:
        self._failed = True
        self._error = 'Invalid syntax in "%s" on line %d:\n%s' % (
            os.path.basename(e.filename),
            e.lineno,
            e.text,
        )
        import traceback

        self._error_detail = traceback.format_exc()

    self._path = path
    self._source = source
    self._argv = argv
    self.ran = False


# Traceback (Exception)
(venv) $ bokeh serve myapp.py
2018-06-27 12:59:45,343 Starting Bokeh server version 0.13.0 (running on Tornado 5.0.1)
2018-06-27 12:59:45,345 Bokeh app running at: http://localhost:5006/myapp
2018-06-27 12:59:45,345 Starting Bokeh server with process id: 13979
0.5604607908584933
2018-06-27 13:00:06,274 200 GET /myapp (::1) 96.25ms
2018-06-27 13:00:06,437 101 GET /myapp/ws?bokeh-protocol-version=1.0&amp;bokeh-session-id=OxhSy81ZBM8URWfk6wp7OTH7bTictF12u6pI8o7fkIUs (::1) 0.64ms
2018-06-27 13:00:06,437 WebSocket connection opened
2018-06-27 13:00:06,438 ServerConnection created
0.6452850662545722
0.2762922805351794
2018-06-27 13:00:11,120 Error running application handler <bokeh.application.handlers.script.ScriptHandler object at 0x7feae4861a58>: boom!
File "myapp.py", line 46, in <module>:
raise Exception('boom!') Traceback (most recent call last):
File "/home/matt/Clients/QEye/venv/lib/python3.5/site-packages/bokeh/application/handlers/code_runner.py", line 163, in run
exec(self._code, module.__dict__)
File "/home/matt/Clients/QEye/isolate/myapp.py", line 46, in <module>
raise Exception('boom!')
Exception: boom!

2018-06-27 13:00:11,125 200 GET /myapp (::1) 25.45ms
2018-06-27 13:00:11,132 WebSocket connection closed: code=1001, reason=None
2018-06-27 13:00:11,207 101 GET /myapp/ws?bokeh-protocol-version=1.0&amp;bokeh-session-id=Y5IlTpoIxVs42drth0tKZonyuwGnUNM3zKK6OAG5eg6n (::1) 0.60ms
2018-06-27 13:00:11,208 WebSocket connection opened
2018-06-27 13:00:11,208 ServerConnection created

# Fixed code
def __init__(self, source, path, argv):
    """

    Args:
        source (str) : python source code

        path (str) : a filename to use in any debugging or error output

        argv (list[str]) : a list of string arguments to make available
            as ``sys.argv`` when the code executes

    """
    self._permanent_error = None
    self._permanent_error_detail = None
    self.reset_run_errors()

    import ast

    self._code = None

    try:
        nodes = ast.parse(source, path)
        self._code = compile(nodes, filename=path, mode="exec", dont_inherit=True)
    except SyntaxError as e:
        import traceback

        self._code = None
        self._permanent_error = 'Invalid syntax in "%s" on line %d:\n%s' % (
            os.path.basename(e.filename),
            e.lineno,
            e.text,
        )
        self._permanent_error_detail = traceback.format_exc()

    self._path = path
    self._source = source
    self._argv = argv
    self.ran = False