Visitar URL original
Getting `DatabaseBackend` is not running error · Issue #516 · encode/databases · GitHub
Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Getting DatabaseBackend is not running error #516

Description

@wiseaidev

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.

Activity

  1. eddyizm commented on Nov 16, 2023

    @eddyizm

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

  2. wiseaidev commented on Nov 23, 2023

    @wiseaidev
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions