Skip to content

[Bug] app.on_event('startup') is deprecated — migrate to lifespan context manager #306

Description

@arpit2006

What happened

backend/app/main.py uses the @app.on_event("startup") decorator (line 115), which was deprecated in FastAPI 0.93.0 in favour of the lifespan async context manager pattern. Running the server emits the following deprecation warning:

DeprecationWarning: on_event is deprecated, use lifespan event handlers instead.

This is a maintenance issue — the on_event API will be removed in a future FastAPI release, and the warning clutters server logs.

Steps to reproduce

  1. Install the backend dependencies.
  2. Start the server: uvicorn app.main:app --reload --port 8000
  3. Observe the deprecation warning in the console output.

Expected behaviour

No deprecation warning. The startup logic (await init_db()) runs correctly when the application starts.

Actual behaviour

Relevant code — backend/app/main.py, lines 115–117:

@app.on_event("startup")
async def startup():
    await init_db()

FastAPI emits a DeprecationWarning at import time because on_event is scheduled for removal.

Environment

Field Value
OS Any
Python version 3.10+
FastAPI version 0.93+
PatchPilot version / commit main

Logs

DeprecationWarning: on_event is deprecated, use lifespan event handlers instead.

Additional context

Fix — replace the decorator with a lifespan context manager and pass it to the FastAPI constructor:

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    await init_db()
    yield
    # Shutdown (add any cleanup here in the future)

app = FastAPI(title="PatchPilot API", version="0.1.0", lifespan=lifespan)

The @app.on_event("startup") block is then deleted entirely. No other code needs to change.

This also has the benefit of making shutdown cleanup trivial to add in the future (after the yield).

Reference: FastAPI lifespan events docs

Acceptance criteria:

  • @app.on_event("startup") is removed.
  • A lifespan async context manager handles await init_db() on startup.
  • FastAPI(lifespan=lifespan) is used when constructing app.
  • No deprecation warning appears when the server starts.
  • All existing tests pass without modification.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions