fix(api): non-blocking asyncio.sleep in lifespan db-retry backoff (#140) - #142
Draft
Jovonni wants to merge 1 commit into
Draft
fix(api): non-blocking asyncio.sleep in lifespan db-retry backoff (#140)#142Jovonni wants to merge 1 commit into
Jovonni wants to merge 1 commit into
Conversation
The startup lifespan retried init_db() with a blocking time.sleep() inside an async function, which stalls the event loop during db reconnection — delaying readiness/health probes that share the loop. Switch to await asyncio.sleep() and add a regression test suite. Closes #140
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
The FastAPI startup
lifespanretriedinit_db()with a blockingtime.sleep(retry_delay)inside anasync def. That blocks the event loop for the full backoff window on every failed attempt, so anything sharing the loop from the first instant (health/readiness probes) can be delayed while the DB is reconnecting.Swaps it for
await asyncio.sleep(retry_delay)(non-blocking), and addsimport asyncioat module top.Why
Flagged in #140 — small, correct one-liner with a real (if narrow) impact on startup responsiveness.
Tests
New regression subsuite
core/tests/test_lifespan_async.py:time.sleepand usesawait asyncio.sleepinit_db()fails once then succeeds; asserts the retry path awaits the non-blocking sleep exactly once (event loop never blocks)Runs under the existing
core/tests/pytest suite in CI.Closes #140