Skip to content

Migrate FastAPI on_event handlers to lifespan#2369

Open
FawwadAhmed11 wants to merge 1 commit into
roboflow:mainfrom
FawwadAhmed11:fix/1960-fastapi-lifespan-migration
Open

Migrate FastAPI on_event handlers to lifespan#2369
FawwadAhmed11 wants to merge 1 commit into
roboflow:mainfrom
FawwadAhmed11:fix/1960-fastapi-lifespan-migration

Conversation

@FawwadAhmed11
Copy link
Copy Markdown

FastAPI deprecated on_event in 0.109; the upper pin <0.116 was added defensively in case it was removed. It hasn't been removed through 0.128+, but the pin still blocks users who need a newer FastAPI. This PR migrates off on_event and drops the pin.

Closes #1960

What does this PR do?

Replaces the three on_event handlers in http_api.py and parallel_http_api.py with a single asynccontextmanager-based lifespan function. Adds an async startup_hook() extension point on HttpInterface that ParallelHttpInterface overrides for its Redis-dispatch setup. Drops the fastapi<0.116 upper bound from requirements/_requirements.txt.

All existing behavior is preserved: shutdown telemetry flush, preload model thread launch (still gated on PRELOAD_MODELS/PINNED_MODELS), and parallel Redis checker thread setup all work identically — just triggered by lifespan instead of on_event.

Related Issue(s): Fixes #1960

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • I have tested this change locally
  • I have added/updated tests for this change

Test details:

  • python -m py_compile passes on both modified Python files (syntax and closure validity confirmed)
  • make check_code_quality passes clean — black, isort, flake8 all green
  • CI will run the full unit test suite

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in hard-to-understand areas
  • My changes generate no new warnings or errors
  • I have updated the documentation accordingly (if applicable)

Additional Context

The lifespan function is defined inside HttpInterface.__init__ so it can close over local variables (description, should_preload, model_init_state, initialize_models) that are needed at startup/shutdown time. Python resolves these at call time, not definition time, so there is no NameError risk even though some variables are defined later in __init__.

FastAPI deprecated on_event in 0.109; the upper pin <0.116 was
defensive against possible removal. Replace the three on_event
handlers with a single asynccontextmanager-based lifespan and a
startup_hook extension point overridden by ParallelHttpInterface.
Drop the FastAPI upper bound.

Closes roboflow#1960
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Hamza Ahmed seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@socket-security
Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedpypi/​fastapi@​0.115.14 ⏵ 0.136.3100 +1100100100100

View full report

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relax fastapi upper bound (<0.116) — 7 months behind latest (0.128.0)

2 participants