Fix: move AsyncWorker start to app lifespan to resolve FastAPI router… #1178
+48
−24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
… bug
Summary
Moves the AsyncWorker lifecycle management to the FastAPI application lifespan and ensures the background worker maintains its own persistent database client.
Type of Change
Objective
The previous implementation attempted to start the background worker within an APIRouter lifespan. In certain FastAPI configurations, this can lead to race conditions where the worker fails to start. Furthermore, using request-scoped database clients within background tasks often resulted in "connection closed" errors, as the request context frequently concludes before the worker queue finishes processing.
This change:
Migrates worker start/stop to the global FastAPI lifespan in main.py.
Provides the AsyncWorker with its own persistent ZepGraphiti client.
Hardens the worker loop with error handling to ensure continuous operation even if a single job fails.
Testing
Breaking Changes
If this is a breaking change, describe:
Checklist
make lintpasses)Related Issues
Closes #[issue number]