Repository navigation
asyncio: cancelling a task that performs insert operations in parallel can raise unexpected errors #11761
Replies: 6 comments 10 replies
|
sorry what behavior do you expect? are you indicating that as the program continues after these exception raises that the connection pool is corrupted? cancelling a task is going to be fairly chaotic from any perspective I can think of |
|
for example I dont see the code trying to catch any |
|
it is expected that the connection will be closed on a cancellation also, since the state of such a connection can't be guaranteed. this is a discussion and at best some extra notes in the docs |
|
The function The main issue is that the connection that the task used was somehow corrupted (or something) and is returned to the pool and when another tasks get this connection from the pool it raises an error when using this connection because it is corrupted. At least that is what I think that happens. The reason why I think SQLAlchemy should handle cancellations correctly is that python 3.11 introduced TaskGroups and uncancel which can be used to build clean code that unfortunately relies a lot on cancellation and therefore proper cleanup. Also I am not aware if this only happens when inserting or also when performing different operations. |
|
This fix caused the issue reported at #12727 The proposed fix will change the behavior of the provided script so that instead of continuing it will terminate with a variation of |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
When using asyncio to insert entries in parallel, cancelling the insert task can lead to unexpected errors that are related to connection issues in that special case.
I suppose this is a bug because I expect it to perform cleanup that leaves the connections in a consistent state without raising an error and I couldn't find something at
https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.htmlthat documents this behavior as expected.Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected
No response
SQLAlchemy Version in Use
2.0.32
DBAPI (i.e. the database driver)
asyncpg
Database Vendor and Major Version
PostgreSQL 16
Python Version
3.12
Operating system
Linux
To Reproduce
Error
Additional context
This also happens when using sqlite with aiosqlite.
SQLAlchemy==2.0.32
asyncpg==0.29.0
aiosqlite==0.20.0
All reactions