fix: ensure SQL storage initializes in Vercel serverless runtime#314
Open
FaiChou wants to merge 1 commit intochenyme:mainfrom
Open
fix: ensure SQL storage initializes in Vercel serverless runtime#314FaiChou wants to merge 1 commit intochenyme:mainfrom
FaiChou wants to merge 1 commit intochenyme:mainfrom
Conversation
Vercel serverless deployments could start serving requests without reliably executing FastAPI lifespan startup, so config.load() was not always called during cold start. Because SQL schema creation is triggered through storage-backed config/tokens access, PostgreSQL/MySQL tables (for example app_config and tokens) were never initialized even when SERVER_STORAGE_TYPE and SERVER_STORAGE_URL were configured. Add a minimal lazy-init guard in Config with ensure_loaded(), protected by an asyncio lock and a success flag to keep initialization idempotent under concurrent first requests. Wire this guard into an HTTP middleware so initialization is guaranteed before handling requests, and reuse ensure_loaded() in lifespan to preserve existing startup behavior. This keeps the fix small, safe, and backward-compatible while restoring deterministic DB bootstrapping on Vercel.
Author
|
this bug: #243 (comment) |
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.
Summary
Fixes DB schema not initializing on Vercel serverless deployments even when
SERVER_STORAGE_TYPEandSERVER_STORAGE_URLare set.Root cause
Schema bootstrap depends on storage-backed config loading, which was only triggered in FastAPI lifespan startup. In serverless runtime, lifespan may not run reliably before requests.
Changes
config.ensure_loaded()with:_loadedflagasyncio.Lockfor concurrency-safe first loadawait config.ensure_loaded()before handling requestsawait config.load()withawait config.ensure_loaded()Result
Initialization now works in both traditional and serverless runtimes, with minimal and backward-compatible changes.