Skip to content

time.sleep() inside async lifespan — intentional or worth switching to await asyncio.sleep()? #140

Description

@Joaquinriosheredia

Hi,

While analyzing the codebase with python-vibe-guard, I noticed that
the FastAPI lifespan function in core/fastapi_app.py uses
time.sleep(retry_delay) inside an async def:

async def lifespan(app):
    for attempt in range(max_retries):
        try:
            await init_db()
            break
        except Exception:
            time.sleep(retry_delay)

time.sleep() blocks the entire event loop during startup retries.
The impact is likely small since traffic is minimal during startup,
but if health-check or readiness probes share the same event loop
from the first instant, they could be delayed during DB reconnection attempts.

Was this intentional, or would await asyncio.sleep(retry_delay)
be a welcome one-line improvement?

Tool used: python-vibe-guard
AST-based runtime anti-pattern scanner validated on 902 real repositories.

Activity

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions