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
close cannot be used while an asynchronous query is underway #364
Comments
Same issue: Exception ignored in: <function ResultProxy.__init__.<locals>.<lambda> at 0x6ca86b973a60>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/aiopg/sa/result.py", line 235, in <lambda>
self._weak = weakref.ref(self, lambda wr: cursor.close())
File "/usr/local/lib/python3.6/site-packages/aiopg/cursor.py", line 50, in close
self._impl.close()
psycopg2.ProgrammingError: close cannot be used while an asynchronous query is underway
Exception ignored in: <function ResultProxy.__init__.<locals>.<lambda> at 0x6ca86b973ea0>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/aiopg/sa/result.py", line 235, in <lambda>
self._weak = weakref.ref(self, lambda wr: cursor.close())
File "/usr/local/lib/python3.6/site-packages/aiopg/cursor.py", line 50, in close
self._impl.close()
psycopg2.ProgrammingError: close cannot be used while an asynchronous query is underway Problem is - stacktrace unexpected short, so impossible understand what part of code made this query, what query, how handle it. |
same here, any hints on what's went wrong? |
Same problem here. the problem is that Strange because that line of code is there from a long time: |
This for me resolves the error if cursor.description is not None:
self._metadata = ResultMetaData(self, cursor.description)
self._weak = None Version of the libraries aiohttp==2.2.5
aiopg==0.13.0
psycopg2==2.7.3.1
SQLAlchemy==1.1.9
SQLAlchemy-Utils==0.32.14 Query async with self.db.acquire() as conn:
last_entry = mLog.alias()
query = select([
mLog.c.name,
mLog.c.user_id,
extract('EPOCH', (last_entry.c.created_at - mLog.c.created_at)).label('usage')
]).select_from(
mLog.join(last_entry, mLog.c.name == last_entry.c.name)
)\
.where(
(mLog.c.user_id == user_id) & \
(last_entry.c.user_id == user_id) & \
(mLog.c.action == 'create') & \
(last_entry.c.action == 'destroy')
)
result = await conn.execute(query) |
Any update? Can core developers comment this issue? This exception bore me all time. Please give some advice why its happen and what to do. |
Any update? really tiring my work |
I will take a look, please share script or test case if possible, so I can reproduce locally |
I am trying to run this script : but postgre got stuck. Please replicate it on your machine Thanks |
@sajjadalee my brain stuck with your script too :D |
I got this exception (close cannot...) with small usual queries like |
😄 bravo. Its an opensource solution and i wanted to contribute in it. However postgre is such an disappointment so far :( |
@sajjadalee you may try asyncpg or it's SQLAlchemy wrapper asyncpgsa, just you need check query timeouts, because your script is giant and may take long time for execution. I have plan move from aiopg to this library, because psycopg async mode disappoint me. |
@sajjadalee That sql script is large, this is not type of workload aiopg is optimized for. Have you tried regula psycopg2 in sync mode? |
Looks like @barrachri is right, basically aiopg/tests/pep492/test_async_await.py Lines 149 to 152 in 7d8f58b
It should explicitly close cursor. Can anyone test this solution? |
I'll try to find time later today to test it. Thanks @jettify |
@jettify This appears to have worked for me. Thanks! |
@asvetlov should we remove code that implicitly closes cursor in order to avoid this issue? |
It works also for me. result = await conn.execute(query) Is there a specific reason to keep track of these weakrefs? |
Hmm. |
Any update on the status of this? |
No, we don't perform hidden conversations and secret commits. |
btw I got this error while using the aiopg cursor context so that doesn't seem to fix the issue: File "/weather/fbn/asyncio/db_utils.py", line 76, in _db_execute
return cur.rowcount, []
File "/weather/fbn/asyncio/db_utils.py", line 33, in __aexit__
return self._ctx.__exit__(exc_type, exc_val, exc_tb)
File "/usr/local/lib/python3.6/site-packages/aiopg/utils.py", line 220, in __exit__
self._cur.close()
File "/usr/local/lib/python3.6/site-packages/aiopg/cursor.py", line 50, in close
self._impl.close()
psycopg2.ProgrammingError: close cannot be used while an asynchronous query is underway along with: File "/usr/local/lib/python3.6/site-packages/aiopg/cursor.py", line 113, in execute
yield from self._conn._poll(waiter, timeout)
File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 241, in _poll
raise exc
File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 238, in _poll
yield from asyncio.wait_for(self._waiter, timeout, loop=self._loop)
File "/usr/local/lib/python3.6/asyncio/tasks.py", line 362, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError And this happened during a timeout, so it looks like it's due to: https://github.com/aio-libs/aiopg/blob/master/aiopg/cursor.py#L116 And this will then cause the connection to not be returned to the pool since https://github.com/aio-libs/aiopg/blob/master/aiopg/utils.py#L241 will not run. band-aid: #415 Fascinating as 2a72b60#diff-3416729d6745fac0ca3dd44b22a69068 is supposed to have cancelled the connection...perhaps the close is timing out / cancel failing? |
Issue is strange, any chance you reuse cursor/connection between different coroutines? |
@jettify not from what I see, here's what I do: class _SyncCtxWrapper:
# wraps an async context around a sync context, need this until https://github.com/aio-libs/aiopg/pull/265 is merged
def __init__(self, sync_ctx):
self._ctx = sync_ctx
async def __aenter__(self):
return self._ctx.__enter__()
async def __aexit__(self, exc_type, exc_val, exc_tb):
return self._ctx.__exit__(exc_type, exc_val, exc_tb)
cursor_ctx = _SyncCtxWrapper(await self._pg_pool.cursor())
async with cursor_ctx as cur:
await cur.execute(query, parameters) |
seems like |
Got same issue in staging environment after 10-15 min in idle, stable reproduce. |
@AbdullaM5 could you try my patch in #415 to see if it helps? In my situation once I hit this error my aiopg pool was stuck. Also if you could provide a script that can reproduce this with a docker postgres server that would be awesome! |
@thehesiod The patch did not help. |
That patch already released in new aiopg version. Can you show code example that reproduces this issue? |
hi, I am getting the same issue on latest git |
Same here - "close cannot be used while an asynchronous query is underway" after "TimeoutError" |
anyone with a reproducible case? |
Make issue on latest git: import asyncio
from aiopg.sa import create_engine
import datetime
import sqlalchemy as sa
import random
test_table = sa.Table(
'test_table', sa.MetaData(),
sa.Column('va1'),
sa.Column('va2'),
sa.Column('va3'),
sa.Column('va4'),
sa.Column('va5'),
sa.Column('va6'),
sa.Column('va7'),
sa.Column('va8'),
)
create = "CREATE TABLE test_table(" \
"va1 varchar(100), " \
"va2 varchar(100), " \
"va3 varchar(100), " \
"va4 varchar(100), " \
"va5 varchar(100), " \
"va6 varchar(100), " \
"va7 varchar(100), " \
"va8 varchar(100))"
select = test_table.select()
insert = test_table.insert().values(
va1="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va2="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va3="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va4="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va5="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va6="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va7="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd",
va8="1edsfsdfasdfasdfasdfadfdsfasdfasdfasdfasdfjhasdlfkhjalskdjhflkajsdhflkasdfasdfasdfd"
)
drop = "DROP TABLE test_table"
async def sql_insert(engine, insert):
await asyncio.sleep(random.random())
async with engine.acquire() as conn:
rp = await conn.execute(select)
# res = await rp.fetchall()
async with engine.acquire() as conn:
rp = await conn.execute(insert)
print(rp)
print(engine.size, engine.freesize)
async def clear(engine):
async with engine.acquire() as conn:
try:
await conn.execute(drop)
except:
pass
await conn.execute(create)
async def aiopg_test(engine):
for i in range(1, 10001):
await sql_insert(engine, insert)
async def test():
engine = await create_engine(
host="192.168.0.2",
port=5432,
database="demo",
user="user",
password="pass",
minsize=1, maxsize=10)
await clear(engine)
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
asyncio.ensure_future(aiopg_test(engine))
await asyncio.sleep(10000)
loop = asyncio.get_event_loop()
loop.run_until_complete(test())
loop.close() If you uncomment the line "res = await rp.fetchall()" the error does not occur. ProxyResult.fetchall() closing cursor, may be). |
@runtel running that script locally on my mac with python 3.6.3, latest released aiopg + psycopg2 and it just hangs. Perhaps want to create a docker container with it? |
@thehesiod Engine connect to database? I was running script on same environment(mac + python 3.6.4, latest released aiopg + psycopg2). |
aha, looks like there's a huge/infinite timeout by default for connect :) ok repro'd! |
@runtel so in this example, the exception is happening due to the weakref in the SQ ResultsProxy trying to close the cursor. (update) second run with maxsize=1 and the issue still happens, interesting...digging |
@runtel ok figured it out, in this function: async with engine.acquire() as conn:
rp = await conn.execute(insert)
print(rp) after print, |
I think maybe each connection needs to keep track of all the cursors, and then release then when the connection is released back to the pool, with the cursor.close() then becoming a no-op in this case |
Tricky problem. To workaround this issue |
I'll work on a PR to fix this in aiopg as well. First updating all my old PRs :) |
I think tracking cursors is good idea, also we should:
|
I have one more question) When i init many engine.acquire(), aiopg should append new connections to pool. This happening, but "slowly". Try to set maxsize=10. |
@runtel sorry not following your last comment, in your example maxsize=10, have another example? |
@jettify should we bump to 0.14 as it can change behavior? |
I can make test on production. Is need? |
This is really important patch also for us. When it will be merged? |
all my aiopg PRs have been stuck :( |
Up) |
You should close cursor by calling |
@asvetlov release new version, please |
Dear all. Please see this pull request #548. |
I'm getting this error if I do multiple fetchs. I'm not sure what I'm doing wrong:
This is the query:
This is the function:
Any help would be appreciated.
The text was updated successfully, but these errors were encountered: