databases icon indicating copy to clipboard operation
databases copied to clipboard

Getting `DatabaseBackend` is not running error

Open wiseaidev opened this issue 3 years ago • 2 comments
trafficstars

Hey everyone. After deploying my fastapi app on Deta and/or on Vercel, I got an error complaining about DatabaseBackend not running. However, the connect method is being called during the startup event of fastapi instance:

@chat_app.on_event("startup")
async def startup():
    await init_models(SQLALCHEMY_DATABASE_URL)
    await database.connect()

The weird thing is that the app is running as expected on Heroku; you can interract with the endpoints to test things out.

Here are the full logs on vercel:

logs
[ERROR] AssertionError: DatabaseBackend is not running
Traceback (most recent call last):
  File "/var/task/vc__handler__python.py", line 293, in vc_handler
    response = asgi_cycle(__vc_module.app, body)
  File "/var/task/vc__handler__python.py", line 204, in __call__
    loop.run_until_complete(asgi_task)
  File "/var/lang/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/var/task/fastapi/applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)
  File "/var/task/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/var/task/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/var/task/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/var/task/starlette/middleware/base.py", line 68, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/var/task/app/main.py", line 75, in add_process_time_header
    response = await call_next(request)
  File "/var/task/starlette/middleware/base.py", line 46, in call_next
    raise app_exc
  File "/var/task/starlette/middleware/base.py", line 36, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "/var/task/starlette/middleware/cors.py", line 92, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/var/task/starlette/middleware/cors.py", line 147, in simple_response
    await self.app(scope, receive, send)
  File "/var/task/starlette/exceptions.py", line 93, in __call__
    raise exc
  File "/var/task/starlette/exceptions.py", line 82, in __call__
    await self.app(scope, receive, sender)
  File "/var/task/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/var/task/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/var/task/starlette/routing.py", line 670, in __call__
    await route.handle(scope, receive, send)
  File "/var/task/starlette/routing.py", line 266, in handle
    await self.app(scope, receive, send)
  File "/var/task/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/var/task/fastapi/routing.py", line 231, in app
    raw_response = await run_endpoint_function(
  File "/var/task/fastapi/routing.py", line 160, in run_endpoint_function
    return await dependant.call(**values)
  File "/var/task/app/auth/router.py", line 62, in register
    results = await register_user(user)
  File "/var/task/app/auth/crud.py", line 131, in register_user
    fetched_user = await find_existed_user(user.email)
  File "/var/task/app/auth/crud.py", line 64, in find_existed_user
    return await database.fetch_one(query, values=values)
  File "/var/task/databases/core.py", line 147, in fetch_one
    async with self.connection() as connection:
  File "/var/task/databases/core.py", line 230, in __aenter__
    raise e
  File "/var/task/databases/core.py", line 227, in __aenter__
    await self._connection.acquire()
  File "/var/task/databases/backends/mysql.py", line 99, in acquire
    assert self._database._pool is not None, "DatabaseBackend is not running"

Digging a bit deeper, I realized that the query calls were not working even though the database.connect is being called at the startup. It seems like some sort of a race condition. Not sure.

wiseaidev avatar Oct 22 '22 17:10 wiseaidev

Having a similar issue, not sure on what the equivalent to a sessionmaker is over here.

eddyizm avatar Nov 16 '23 04:11 eddyizm

Sup @eddyizm! If you are using sqlalchemy, there is no need to use this dep. Have a look at this commit for more info.

wiseaidev avatar Nov 23 '23 11:11 wiseaidev