-
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
Errors with multiple tasks and transactions in postgresql and sqlite #134
Comments
Looks like the root cause of this issue is how If context var is set in outer scope, then all inner tasks have the same value, so new connection is not created in new tasks. If you change code in example to new_context = contextvars.Context()
for i in range(0, 4 * step, step):
fut = new_context.run(asyncio.ensure_future, work(i, i + 5))
futures.append(fut)
await asyncio.wait(futures) then it works as expected. But I don't think that it's a good solution. |
Perhaps my problem is related, but I don't get any errors, in my case workers just get stuck. import asyncio
from databases import Database
def database():
return Database("postgresql://",
host="db",
port=5432,
min_size=1,
max_size=3,
user="tiger",
password="hunter2
database="test")
async def insert_something(db: Database):
async with db.connection() as conn:
async with db.transaction():
await db.execute("insert into person (name) values (:name)", {"name": "testing..."})
async def query_something(db: Database, n):
async with db.connection() as conn:
async with db.transaction():
row = await db.fetch_one("select :foo as foo", {"foo": "bar"})
print(f"{n} done")
async def run():
async with database() as db:
# If this is commented out then the app runs successfully:
await insert_something(db)
tasks = []
for n in range(10):
tasks.append(asyncio.create_task(query_something(db, n)))
print("Waiting...")
await asyncio.gather(*tasks)
print("Done")
if __name__ == "__main__":
asyncio.run(run()) When run I get 4 or 5 worker printing done, and then the app is stuck. If I comment out the The size of the pool does not matter. Also I tried using |
I have the same issue. |
I'm quite certain this is the same underlying issue as #125, #424, #452, and the discussion in #456. It all comes back to how ContextVars have been used in this library, and @reclosedev was totally on the right track all the way back in 2019. I believe I have fixed this in #546 - the gist is that only one parent transaction can run concurrently per connection (but nested transactions are okay). In #546, I'm proposing that databases acquires a new connection from a connection pool for each |
Minimal reproduce script with comments
Error with sqlite database:
Error with postgresql database:
The text was updated successfully, but these errors were encountered: