aio-libs / aio-libs/aiomysql

aiomysql raise InvalidRequestError: Cannot release a connection with not finished transaction

未關閉
#979 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug
主要語言
Python
星號
1.9k
分支
272
PR 合併指標
30 天內沒有已合併 PR

描述

### Describe the bug

When the database connection runs out and a new request is sent, an error occurd and the whole application goes down!
in the aiomysql/utils.py, line 137
```python3
async def __aexit__(self, exc_type, exc, tb):
try:
await self._pool.release(self._conn)
finally:
self._pool = None
self._conn = None
```
why release the whole connection pool?

### To Reproduce

```python3

import asyncio
import base64

import aiohttp.web_routedef
from aiohttp import web
from aiomysql.sa import create_engine

route = aiohttp.web.RouteTableDef()

@route.get('/')
async def test(request):
async with request.app.db.acquire() as conn:
trans = await conn.begin()
await asyncio.sleep(60)
await trans.commit()

return web.Response(text="ok")

@route.get('/test')
async def test(request):
async with request.app.db.acquire() as conn:
trans = await conn.begin()
await trans.close()

return web.Response(text="not ok")

async def mysql_context(app):
engine = await create_engine(
db='fmp_new',
user='root',
password="lyp82nlf",
host="localhost",
maxsize=1,
echo=True,
pool_recycle=1
)
app.db = engine

yield

app.db.close()
await app.db.wait_closed()

async def init_app():

app = web.Application()
app.add_routes(route)
app.cleanup_ctx.append(mysql_context)

return app

def main():

app = init_app()
web.run_app(app, port=9999)

if __name__ == '__main__':
main()
```

### Expected behavior

when a new request arrives , If the connection is exhausted, then wait for it release

### Logs/tracebacks

```python-traceback
Error handling request
Traceback (most recent call last):
File "test_aiomysql.py", line 20, in test
await asyncio.sleep(60)
File "/usr/lib/python3.7/asyncio/tasks.py", line 595, in sleep
return await future
concurrent.futures._base.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/yangshen/Envs/fmp_37/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
resp = await self._request_handler(request)
File "/home/yangshen/Envs/fmp_37/lib/python3.7/site-packages/aiohttp/web_app.py", line 499, in _handle
resp = await handler(request)
File "test_aiomysql.py", line 21, in test
await trans.commit()
File "/home/yangshen/Envs/fmp_37/lib/python3.7/site-packages/aiomysql/utils.py", line 139, in __aexit__
await self._pool.release(self._conn)
File "/home/yangshen/Envs/fmp_37/lib/python3.7/site-packages/aiomysql/sa/engine.py", line 163, in release
raise InvalidRequestError("Cannot release a connection with "
aiomysql.sa.exc.InvalidRequestError: Cannot release a connection with not finished transaction
```

### Python Version

```console
$ python --version
3.7.5
```

### aiomysql Version

```console
$ python -m pip show aiomysql
0.1.0
```

### PyMySQL Version

```console
$ python -m pip show PyMySQL
1.1.0
```

### SQLAlchemy Version

```console
$ python -m pip show sqlalchemy
1.4.49
```

### OS

ubuntu 18.04

### Database type and version

```console
5.7.39-0ubuntu0.18.04.2
```

### Additional context

_No response_

### Code of Conduct

- [X] I agree to follow the aio-libs Code of Conduct

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。