in_transaction() leaks a pool connection when begin() is cancelled
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start in TransactionContextPooled.__aenter__, where the connection is acquired from the pool before begin() runs outside any cleanup. Read __aexit__ to see how the release is handled, and check how the asyncpg pool's release is shielded from cancellation. Done means a cancelled begin() returns the connection, which the issue's reproduction script can confirm. Open PR #2293 edits the same lines, so check it before starting.
Written by the indexing model from the issue text.
Description
Describe the bug
TransactionContextPooled.__aenter__ takes a connection from the pool, sets the context variable, then awaits begin() with nothing around it. If the task is cancelled during begin(), for example by a request deadline, __aenter__ never finishes, so __aexit__ never runs and the connection is never given back. Each time, the pool loses one connection for good. With no acquire timeout set, once they are all gone every later query waits on pool.acquire(). In my runs the next request timed out after 5 s.
in_transaction() is also used inside the ORM (for example get_or_create, as in #1152), so code that never opens a transaction itself can hit this. #1152 mentions the same thing happening in __aenter__; the __aexit__ part of that issue is covered now by the try/finally.
To Reproduce
Postgres 16.2, asyncpg 0.32.0, anyio 4.16.0, Python 3.12, tortoise on develop at cdef122 (the class is the same in 1.1.9). This puts a 100 ms anyio deadline around an ordinary 20 ms transaction, with more requests than the pool has connections:
import asyncio
import anyio
from tortoise import Tortoise, connections, fields
from tortoise.models import Model
from tortoise.transactions import in_transaction
DB_URL = "postgres://[email protected]:5532/postgres?minsize=1&maxsize=10"
class Item(Model):
id = fields.IntField(primary_key=True)
async def handler():
async with in_transaction() as conn:
await conn.execute_query("SELECT pg_sleep(0.02)")
async def request():
# a 100 ms deadline around an ordinary 20 ms transaction
with anyio.move_on_after(0.1) as scope:
await handler()
return scope.cancelled_caught
async def main():
await Tortoise.init(db_url=DB_URL, modules={"models": ["__main__"]})
await handler()
timeouts = 0
for _ in range(20): # 20 waves of 150 concurrent requests
results = await asyncio.gather(*[asyncio.create_task(request()) for _ in range(150)])
timeouts += sum(results)
await asyncio.sleep(0.3)
pool = connections.get("default")._pool
print(f"timeouts: {timeouts}, pool size: {pool.get_size()}, idle: {pool.get_idle_size()}")
try:
async with asyncio.timeout(5):
await handler()
print("next request: ok")
except TimeoutError:
print("next request: timed out after 5 s waiting for a connection")
await Tortoise.close_connections()
asyncio.run(main())
One run on develop:
timeouts: 2413, pool size: 10, idle: 0
next request: timed out after 5 s waiting for a connection
The counts vary from run to run. Over 8 runs the leaked connections were 10, 9, 9, 9, 9, 7, 5 and 0. In 3 of those runs I wrapped begin() to count the CancelledErrors coming out of it, and the count matched the leaked connections each time (5, 9 and 9).
A plain asyncio.timeout() leaks the same way when the deadline lands inside BEGIN. With 1000 requests one after another, a pool of 50 and random deadlines of 0-5 ms, 16 connections leaked.
Expected behavior
A request that is cancelled while its transaction is starting gives the connection back, the same as one cancelled inside the block.
With this change to TransactionContextPooled.__aenter__, the same script leaked 0 connections in 6 runs out of 6, and the next request was ok each time:
async def __aenter__(self) -> TransactionalDBClient:
await self.ensure_connection()
self.client._connection = await self.client._parent._pool.acquire()
self.token = get_connections().set(self.connection_name, self.client)
try:
await self.client.begin()
except BaseException:
get_connections().reset(self.token)
await self.client._parent._pool.release(self.client._connection)
raise
return self.client
I have only measured this on asyncpg, where Pool.release() is shielded, so the release finishes while the task is being cancelled. The psycopg backend releases with a plain await self.putconn(connection), and I have not checked mysql, mssql or oracle, so those may also need a shield or some handling of a failed release.
Additional context
With the change, asyncpg logs Resetting connection with an active transaction for some of the cancelled requests, because BEGIN had already reached the server before the cancel. The same warning is the subject of #1094; I don't know whether it has the same cause there.
Possibly related, different trigger: #1438. Open PR #2293 edits these same lines; I can wait for it or rebase on it.
I can open a PR with this change and a test if you want one.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 540
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 31
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tortoise/tortoise-orm
-
URLValidator ignores an explicit empty scheme allowlistPossibly taken @IlyaKhalafi claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
tortoise/tortoise-orm#2393 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
tortoise/tortoise-orm#668 ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 40/100
tortoise/tortoise-orm#2383 · 2 comments ·
Maintainers usually reply within 2 days
-
Prefetch with to_attr leaves the attribute missing when all forward relations are nullPossibly taken @IlyaKhalafi claimed this 10 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
tortoise/tortoise-orm#2374 ·
Maintainers usually reply within 2 days
-
Adding a custom Prefetch to a cloned queryset mutates the original queryPossibly taken @IlyaKhalafi claimed this 10 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
tortoise/tortoise-orm#2372 ·
Maintainers usually reply within 2 days
All issues in tortoise/tortoise-orm
Similar issues
-
HandyModRTransform.deriv3 is wrong for m > 2Possibly taken @SajalDevX claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
read_messages: the first read after subscribe/psubscribe returns no messages even when some are waitingPossibly taken @matheussilvano claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 80/100
The-Swarm-Corporation/swarms-framework-docs#399 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
bump patch
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#1321 ·
Maintainers usually reply within 1 day