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

AssertionError while using transactions.in_transaction #1720

Open
DaniilChizhevskii opened this issue Sep 27, 2024 · 3 comments
Open

AssertionError while using transactions.in_transaction #1720

DaniilChizhevskii opened this issue Sep 27, 2024 · 3 comments

Comments

@DaniilChizhevskii
Copy link

Describe the bug
I use Tortoise ORM with FastAPI contrib and MySQL database. There are some places in my code where I use toirtoise.transactions.in_transaction and it causes this error sometimes:

AssertionError: (<aiomysql.connection.Connection object at 0x7ec4f3382120>, {<aiomysql.connection.Connection object at 0x7ec4f3d648f0>, <aiomysql.connection.Connection object at 0x7ec4f3d20530>, <aiomysql.connection.Connection object at 0x7ec4f3367b30>, <aiomysql.connection.Connection object at 0x7ec4f3364770>, <aiomysql.connection.Connection object at 0x7ec4f3d22ba0>})

I use multiple gunicorn workers and all of them having this issue simultaneously. Issue repeats several times an hour.

To Reproduce
Create FastAPI application with registered Tortoise:

from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise
app = FastAPI(title=...)
register_tortoise(app, config=..., generate_schemas=False, add_exception_handlers=False)

Add transaction somewhere in your routes:

async with transactions.in_transaction():
    ...

And MySQL connection:
mysql://root:password@localhost:3306/db

Expected behavior
App works normally, no exceptions got

@henadzit
Copy link
Contributor

Hi @DaniilChizhevskii, could you please tell how do you know that the error happens in in_transaction? Can you provide a stacktrace?

@DaniilChizhevskii
Copy link
Author

Hi @henadzit
I thought this error was related to asyncmy not being installed, but even with it, the error continues to occur.

File "/workspace/backend/app/miniapp/main.py", line 93, in auth_miniapp
[2024-11-14 13:22:50]     user = await get_or_create_user(json.loads(auth_data['user'][0]), auth_data.get('start_param', [None])[0])
[2024-11-14 13:22:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-11-14 13:22:50]   File "/workspace/backend/app/miniapp/main.py", line 63, in get_or_create_user
[2024-11-14 13:22:50]     async with transactions.in_transaction():
[2024-11-14 13:22:50]   File "/workspace/backend/.heroku/python/lib/python3.12/site-packages/tortoise/backends/base/client.py", line 298, in __aexit__
[2024-11-14 13:22:50]     await self.connection._parent._pool.release(self.connection._connection)
[2024-11-14 13:22:50]           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[2024-11-14 13:22:50]   File "asyncmy/pool.pyx", line 180, in asyncmy.pool.Pool.release
[2024-11-14 13:22:50]     self._used.remove(conn)
[2024-11-14 13:22:50] KeyError: <asyncmy.connection.Connection object at 0x7ea05210d8b0>

@henadzit
Copy link
Contributor

I tried to reproduce the issue by severing the connection between a sample app and MySQL but no luck. Please let me know if you have more context.

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

No branches or pull requests

2 participants