# Buggy code
def startServer(self):
    if gevent_present:
        web.app.logger.info("Starting Gevent server")
        # leave subprocess out to allow forking for fetchers and processors
        self.start_gevent()
    else:
        try:
            web.app.logger.info("Starting Tornado server")
            if (
                web.ub.config.get_config_certfile()
                and web.ub.config.get_config_keyfile()
            ):
                ssl = {
                    "certfile": web.ub.config.get_config_certfile(),
                    "keyfile": web.ub.config.get_config_keyfile(),
                }
            else:
                ssl = None
            # Max Buffersize set to 200MB
            http_server = HTTPServer(
                WSGIContainer(web.app), max_buffer_size=209700000, ssl_options=ssl
            )
            http_server.listen(web.ub.config.config_port)
            self.wsgiserver = IOLoop.instance()
            self.wsgiserver.start()
            # wait for stop signal
            self.wsgiserver.close(True)
        except SocketError as e:
            web.app.logger.info("Error starting server: %s" % e.strerror)
            print("Error starting server: %s" % e.strerror)
            web.helper.global_WorkerThread.stop()
            sys.exit(1)

    if self.restart == True:
        web.app.logger.info("Performing restart of Calibre-Web")
        web.helper.global_WorkerThread.stop()
        if os.name == "nt":
            arguments = ['"' + sys.executable + '"']
            for e in sys.argv:
                arguments.append('"' + e + '"')
            os.execv(sys.executable, arguments)
        else:
            os.execl(sys.executable, sys.executable, *sys.argv)
    else:
        web.app.logger.info("Performing shutdown of Calibre-Web")
        web.helper.global_WorkerThread.stop()
    sys.exit(0)


# Traceback (OperationalError)
ERROR:cps.web:Exception on /book/125 [GET]
Traceback (most recent call last):
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/jgillman/Code/calibre-web/vendor/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/jgillman/Code/calibre-web/vendor/flask_login.py", line 717, in decorated_view
return func(*args, **kwargs)
File "/Users/jgillman/Code/calibre-web/cps/web.py", line 381, in show_book
return render_template('detail.html', entry=entries,  cc=cc, title=entries.title, books_shelfs=book_in_shelfs)
File "/Users/jgillman/Code/calibre-web/vendor/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/Users/jgillman/Code/calibre-web/vendor/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/jgillman/Code/calibre-web/cps/templates/detail.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/Users/jgillman/Code/calibre-web/cps/templates/layout.html", line 136, in top-level template code
{% block body %}{% endblock %}
File "/Users/jgillman/Code/calibre-web/cps/templates/detail.html", line 66, in block "body"
{% if entry['custom_column_' ~ c.id]|length > 0 %}
File "/Users/jgillman/Code/calibre-web/vendor/jinja2/environment.py", line 387, in getitem
return getattr(obj, attr)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/attributes.py", line 316, in __get__
return self.impl.get(instance_state(instance), dict_)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/attributes.py", line 613, in get
value = self.callable_(state, passive)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/strategies.py", line 524, in _load_for_state
return self._emit_lazyload(session, state, ident_key, passive)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/strategies.py", line 585, in _emit_lazyload
result = q.all()
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2241, in all
return list(self)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2353, in __iter__
return self._execute_and_instances(context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/orm/query.py", line 2368, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 662, in execute
params)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 874, in _execute_context
context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
exc_info
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/util/compat.py", line 196, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/base.py", line 867, in _execute_context
context)
File "/Users/jgillman/Code/calibre-web/vendor/sqlalchemy/engine/default.py", line 324, in do_execute
cursor.execute(statement, parameters)
OperationalError: (OperationalError) no such table: books_custom_column_1_link u'SELECT custom_column_1.id AS custom_column_1_id, custom_column_1.value AS custom_column_1_value \
nFROM custom_column_1, books_custom_column_1_link \nWHERE ? = books_custom_column_1_link.book AND custom_column_1.id = books_custom_column_1_link.value' (125,)
ERROR:tornado.access:500 GET /book/125 (::1) 89.60ms

# Fixed code
def startServer(self):
    if gevent_present:
        web.app.logger.info("Starting Gevent server")
        # leave subprocess out to allow forking for fetchers and processors
        self.start_gevent()
    else:
        try:
            ssl = None
            web.app.logger.info("Starting Tornado server")
            certfile_path = web.ub.config.get_config_certfile()
            keyfile_path = web.ub.config.get_config_keyfile()
            if certfile_path and keyfile_path:
                if os.path.isfile(certfile_path) and os.path.isfile(keyfile_path):
                    ssl = {"certfile": certfile_path, "keyfile": keyfile_path}
                else:
                    web.app.logger.info(
                        "The specified paths for the ssl certificate file and/or key file seem to be broken. Ignoring ssl. Cert path: %s | Key path: %s"
                        % (certfile_path, keyfile_path)
                    )

            # Max Buffersize set to 200MB
            http_server = HTTPServer(
                WSGIContainer(web.app), max_buffer_size=209700000, ssl_options=ssl
            )
            http_server.listen(web.ub.config.config_port)
            self.wsgiserver = IOLoop.instance()
            self.wsgiserver.start()
            # wait for stop signal
            self.wsgiserver.close(True)
        except SocketError as e:
            web.app.logger.info("Error starting server: %s" % e.strerror)
            print("Error starting server: %s" % e.strerror)
            web.helper.global_WorkerThread.stop()
            sys.exit(1)

    # ToDo: Somehow caused by circular import under python3 refactor
    if sys.version_info > (3, 0):
        self.restart = web.py3_restart_Typ
    if self.restart == True:
        web.app.logger.info("Performing restart of Calibre-Web")
        web.helper.global_WorkerThread.stop()
        if os.name == "nt":
            arguments = ['"' + sys.executable + '"']
            for e in sys.argv:
                arguments.append('"' + e + '"')
            os.execv(sys.executable, arguments)
        else:
            os.execl(sys.executable, sys.executable, *sys.argv)
    else:
        web.app.logger.info("Performing shutdown of Calibre-Web")
        web.helper.global_WorkerThread.stop()
    sys.exit(0)