MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.98k stars 403 forks source link

connection.close is throwing protocol error #490

Open immohsin opened 5 years ago

immohsin commented 5 years ago

asyncpg version: 0.18.1 PostgreSQL version: 9.5.10 Do you use a PostgreSQL SaaS? If so, which? Can you reproduce the issue with a local PostgreSQL install?: using google cloud sql pg instance Python version: 3.6.5 Platform: Ubuntu Do you use pgbouncer?: No Did you install asyncpg with pip?: Yes If you built asyncpg locally, which version of Cython did you use?: Can the issue be reproduced under both asyncio and uvloop?: No

I have been facing this error for quite sometime intermittently asyncpg.exceptions._base.InternalClientError: got result for unknown protocol state 0

The entire stack trace:

 File "/repo/server/schema_db.py", line 345, in read
    await connection.close()

  File "/usr/local/lib/python3.6/dist-packages/asyncpg/connection.py", line 1082, in close
    await self._protocol.close(timeout)

  File "asyncpg/protocol/protocol.pyx", line 560, in close

  File "asyncpg/protocol/protocol.pyx", line 814, in asyncpg.protocol.protocol.BaseProtocol._dispatch_result

asyncpg.exceptions._base.InternalClientError: got result for unknown protocol state 0

Can someone explain when this exception is thrown and for what reason? If i know the reason this exception is thrown, i can figure out the issue myself. I'm not able to reproduce locally or in my stage environment.

elprans commented 5 years ago

Interesting. Can you show the source for your read() function?

immohsin commented 5 years ago

@elprans Here is the read function.

async def read(id: str, env) 
        query = '''SELECT app_value FROM table_name WHERE id=$1;'''.
        connection = await asyncpg.connect(**creds)
        try:
            app = await connection.fetchval(query, id))
        finally:
            await connection.close()
        if not app and env is 'stage':
            app = await self.read(id, 'prod')
       return app
immohsin commented 5 years ago

@elprans any idea why its occurring during closing the connection object?

elprans commented 4 years ago

It looks like a race. The protocol handler is receiving data from the server while not expecting anything. Is finally called normally or is there a preceding exception?

immohsin commented 4 years ago

finally is getting called normally there is no exception. The way I have handled now is to catch for InternalClientError exception while closing the connection.

nikicat commented 1 year ago

A similar error happens in my case, but a connection is closed by a CancelledError exception inside a context manager and I got InternalClientError: got result for unknown protocol state 3

bshakur8 commented 3 months ago

I'm also getting InternalClientError: got result for unknown protocol state 3:

asyncpg version: 0.29.0 PostgreSQL version: 9.5.10 Do you use a PostgreSQL SaaS? If so, which? GCP PostgreSQL Python version: 3.11.9 Platform: linux Do you use pgbouncer?: Yes Did you install asyncpg with pip?: Yes

Got this error after a long time of running app:

    File "/usr/local/lib/python3.11/site-packages/databases/core.py", line 152, in fetch_one
 return await connection.fetch_one(query, values)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/databases/core.py", line 269, in fetch_one
 return await self._connection.fetch_one(built_query)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/databases/backends/postgres.py", line 189, in fetch_one
 row = await self._connection.fetchrow(query_str, *args)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 749, in fetchrow
 data = await self._execute(
 ^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 1794, in _execute
 result, _ = await self.__execute(
 ^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 1892, in __execute
 result, stmt = await self._do_execute(
 ^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 1925, in _do_execute
 stmt = await self._get_statement(
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/asyncpg/connection.py", line 433, in _get_statement
 statement = await self._protocol.prepare(
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "asyncpg/protocol/protocol.pyx", line 166, in prepare
    File "asyncpg/protocol/protocol.pyx", line 902, in asyncpg.protocol.protocol.BaseProtocol._dispatch_result
        asyncpg.exceptions._base.InternalClientError: got result for unknown protocol state 3