Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempted solution to db pool deadlock issue #140

Merged
merged 2 commits into from
Aug 30, 2022
Merged

Conversation

hardbyte
Copy link
Member

@hardbyte hardbyte commented Aug 30, 2022

tl;dr: Dependencies and path operations defined as functions (def only) are run in an anyio threadpool. db.execute is a blocking call, causing all the workers in the pool to block in the path operation function. This prevents dependency generators from invoking their finally block, thereby preventing SQLAlchemy connections from being released.
fastapi/fastapi#3205 (comment)

The proper solution is likely to use asyncio compatible database - which is coming in Sqlalchemy... but is also a huge rewrite of the code base.

A lovely hack of a solution was suggested in fastapi/full-stack-fastapi-template#104 (comment) which is what this PR adds. Don't merge until we see how it goes...

@hardbyte
Copy link
Member Author

Comparing against the baseline in #139 this hack seems to help. I haven't repeated the experiments but this had no errors up to 85 users @ 35 requests per second. It failed when the 3rd container came up, and exhausted the available db connections.

image
image

Suggest we merge this and establish as a new baseline.

@hardbyte hardbyte requested a review from jlandy-w August 30, 2022 12:02
@hardbyte hardbyte merged commit 43d575d into main Aug 30, 2022
@hardbyte hardbyte deleted the feature/db-hack branch August 30, 2022 21:34
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.

1 participant