-
Notifications
You must be signed in to change notification settings - Fork 261
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
Assertion error when using transaction decorator #123
Comments
Not sure - could be to do with the interfaction between the two decorators maybe? |
This has crossed my mind and seems the most logical explanation
That seems to me to be the "wrong" way around. But I might try it out.
Not that I could find.
The main fastapi documention has an example to set it up, but doesn't use transactions and is pretty basic overall. While typing this I got another idea of how I could set this up. Using a middleware to attach a transaction to each route, which sounds a lot less error prone. I'm afraid the problem is because of the double decorator. |
This took me a while to figure out. I used locust to create some load tests. This issue only happens when two concurrent requests happen on the same endpoint. Calling database.transaction() creates a Transaction object which is then used to decorate a route function. This instance of the Transaction object's state is then shared between all invocations of the route function. When a route function is called by fastapi, the transaction wrapper function gets a connection and saves it on the object So decorating functions with I've created this decorator to work around this: from functools import wraps
def transaction(func):
@wraps(func)
async def wrapper(*args, **kwargs):
async with database.transaction():
return await func(*args, **kwargs)
return wrapper |
I'm relatively certain should be fixed by #546, there's now plenty of discussion in that PR, and in many of the related issues as well if you're still curious. When that's eventually released please let us know if you still experience this error - thank you for reporting! |
I'm using fastapi in combination with this library but when I use the transaction decorator I'm sporadically getting the following assertion error:
I'm using a pretty basic fastapi setup making use of the on event startup hook to connect the database. The database is mysql, which i'm connecting to with the aiomysql package (and this library).
I've decorated all of my routes in the following manner :
This works most of the time, but once in a while it raises the above exception. If I remove the transaction decorator everything works as expected.
Why do I get this error? Am I using the decorator improperly?
The text was updated successfully, but these errors were encountered: