aio-libs / aiopg

aiopg is a library for accessing a PostgreSQL database from the asyncio
http://aiopg.readthedocs.io
BSD 2-Clause "Simplified" License
1.4k stars 159 forks source link

Issues with connecting to Google Cloud SQL via sql proxy #370

Open bitrut opened 7 years ago

bitrut commented 7 years ago

I went through official guide from Google on how to connect to Cloud PostgreSQL. Unfortunately I get this traceback in logs:

2017-08-16T13:05:54.966574354Z Traceback (most recent call last):
2017-08-16T13:05:54.966585403Z   File "main.py", line 24, in <module>
2017-08-16T13:05:54.966589301Z     web.run_app(app, host='0.0.0.0', port=8000)
2017-08-16T13:05:54.966592142Z   File "/usr/local/lib/python3.6/site-packages/aiohttp/web.py", line 422, in run_app
2017-08-16T13:05:54.966596332Z     loop.run_until_complete(app.startup())
2017-08-16T13:05:54.966600292Z   File "uvloop/loop.pyx", line 1203, in uvloop.loop.Loop.run_until_complete (uvloop/loop.c:25632)
2017-08-16T13:05:54.966603161Z   File "/usr/local/lib/python3.6/site-packages/aiohttp/web.py", line 259, in startup
2017-08-16T13:05:54.966606143Z     yield from self.on_startup.send(self)
2017-08-16T13:05:54.966608657Z   File "/usr/local/lib/python3.6/site-packages/aiohttp/signals.py", line 51, in send
2017-08-16T13:05:54.966611358Z     yield from self._send(*args, **kwargs)
2017-08-16T13:05:54.966613597Z   File "/usr/local/lib/python3.6/site-packages/aiohttp/signals.py", line 17, in _send
2017-08-16T13:05:54.966616198Z     yield from res
2017-08-16T13:05:54.966618974Z   File "/opt/broadcasts/storage.py", line 18, in init
2017-08-16T13:05:54.966621447Z     password=DB_PASSWORD,
2017-08-16T13:05:54.966623587Z   File "/usr/local/lib/python3.6/site-packages/aiopg/utils.py", line 72, in __await__
2017-08-16T13:05:54.966626117Z     resp = yield from self._coro
2017-08-16T13:05:54.966628477Z   File "/usr/local/lib/python3.6/site-packages/aiopg/sa/engine.py", line 67, in _create_engine
2017-08-16T13:05:54.966630969Z     loop=loop, timeout=timeout, **kwargs)
2017-08-16T13:05:54.966633311Z   File "/usr/local/lib/python3.6/site-packages/aiopg/utils.py", line 67, in __iter__
2017-08-16T13:05:54.966635821Z     resp = yield from self._coro
2017-08-16T13:05:54.966638103Z   File "/usr/local/lib/python3.6/site-packages/aiopg/pool.py", line 46, in _create_pool
2017-08-16T13:05:54.966640578Z     yield from pool._fill_free_pool(False)
2017-08-16T13:05:54.966643043Z   File "/usr/local/lib/python3.6/site-packages/aiopg/pool.py", line 203, in _fill_free_pool
2017-08-16T13:05:54.96664552Z     **self._conn_kwargs)
2017-08-16T13:05:54.966647841Z   File "/usr/local/lib/python3.6/site-packages/aiopg/utils.py", line 67, in __iter__
2017-08-16T13:05:54.966650378Z     resp = yield from self._coro
2017-08-16T13:05:54.966652511Z   File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 74, in _connect
2017-08-16T13:05:54.966656672Z     yield from conn._poll(waiter, timeout)
2017-08-16T13:05:54.966658967Z   File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 239, in _poll
2017-08-16T13:05:54.966661307Z     yield from asyncio.shield(cancel(), loop=self._loop)
2017-08-16T13:05:54.966663591Z   File "/usr/local/lib/python3.6/site-packages/aiopg/connection.py", line 225, in cancel
2017-08-16T13:05:54.966672795Z     self._conn.cancel()
2017-08-16T13:05:54.966675518Z psycopg2.OperationalError: asynchronous connection attempt underway

However, when I run application locally and connect via proxy running under docker with my laptop as I host it works fine. Anyone had problems with Google Cloud like mine?

jettify commented 6 years ago

can you show code example? Make sure you do not reuse connection between coroutines

bitrut commented 6 years ago

Does this usage could have an impact in this issue?

async def get_results():
    async with engine.acquire() as conn:
        return list(await conn.execute(sql))

for result in get_results():
    ...

Instead using async for as docs suggests?

async with engine.acquire() as conn:
    results = await conn.execute(sql)
    async for result in results:
        ...
asvetlov commented 6 years ago

@bitrut both your examples are correct on first glance

robhaswell commented 5 years ago

I also can reproduce this error, would be up for working on a solution with the developers:

async def test_async():
    pool = await aiopg.create_pool(DSN)

asyncio.run(test_async())
Traceback (most recent call last):
  File "dbtest.py", line 25, in <module>
    asyncio.run(test_async())
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
    return future.result()
  File "dbtest.py", line 19, in test_async
    pool = await aiopg.create_pool(DSN)
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/utils.py", line 71, in __await__
    resp = yield from self._coro
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/pool.py", line 47, in _create_pool
    yield from pool._fill_free_pool(False)
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/pool.py", line 208, in _fill_free_pool
    **self._conn_kwargs)
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/utils.py", line 66, in __iter__
    resp = yield from self._coro
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/connection.py", line 74, in _connect
    yield from conn._poll(waiter, timeout)
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/connection.py", line 240, in _poll
    yield from asyncio.shield(cancel(), loop=self._loop)
  File "/Users/cloudfind/Projects/mufasa/venv/lib/python3.7/site-packages/aiopg/connection.py", line 226, in cancel
    self._conn.cancel()
psycopg2.OperationalError: asynchronous connection attempt underway

I have the same problem with using a single connection instead of a pool.

Connecting with the same DSN via psycopg2 or Postico works fine.