Skip to content

Regression in async connection on 3.1.15 when multiple hosts are present #703

@CaselIT

Description

@CaselIT

Hi.

The following script that uses multiple hosts specifying the port only on some of them passes in the sync connection but fails on the async one due to an assertion error

import traceback
import psycopg

urls = [
    {
        "dbname": "test",
        "user": "scott",
        "password": "tiger",
        "host": "localhost,localhost,localhost",
        "port": ",5432,",
    },
    {
        "dbname": "test",
        "user": "scott",
        "password": "tiger",
        "host": "localhost,localhost,localhost",
        "port": "5432,,",
    },
    {
        "dbname": "test",
        "user": "scott",
        "password": "tiger",
        "host": "localhost,localhost,localhost",
        "port": "5432,,5432",
    },
]


def sync():
    for url in urls:
        with psycopg.connect(**url) as conn:
            cur = conn.execute("select 42")
            print(cur.fetchall())


async def async_():
    for url in urls:
        try:
            async with await psycopg.AsyncConnection.connect(**url) as conn:
                cur = await conn.execute("select 42")
                print(await cur.fetchall())
        except Exception:
            traceback.print_exc()


sync()
import asyncio

try:
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
except:
    pass

asyncio.run(async_())

output is

[(42,)]
[(42,)]
[(42,)]
Traceback (most recent call last):
  File "test3.py", line 39, in async_
    async with await psycopg.AsyncConnection.connect(**url) as conn:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\connection_async.py", line 124, in connect
    attempts = await conninfo_attempts_async(params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 330, in conninfo_attempts_async
    attempts.extend(await _resolve_hostnames(attempt))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 426, in _resolve_hostnames
    assert port and "," not in port  # assume a libpq default and no multi
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Traceback (most recent call last):
  File "test3.py", line 39, in async_
    async with await psycopg.AsyncConnection.connect(**url) as conn:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\connection_async.py", line 124, in connect
    attempts = await conninfo_attempts_async(params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 330, in conninfo_attempts_async
    attempts.extend(await _resolve_hostnames(attempt))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 426, in _resolve_hostnames
    assert port and "," not in port  # assume a libpq default and no multi
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Traceback (most recent call last):
  File "test3.py", line 39, in async_
    async with await psycopg.AsyncConnection.connect(**url) as conn:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\connection_async.py", line 124, in connect
    attempts = await conninfo_attempts_async(params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 330, in conninfo_attempts_async
    attempts.extend(await _resolve_hostnames(attempt))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cfede\venvs\sa\Lib\site-packages\psycopg\conninfo.py", line 426, in _resolve_hostnames
    assert port and "," not in port  # assume a libpq default and no multi
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

This test is in sqlalchemy (running with pytest --db psycopg_async .\test\dialect\postgresql\test_dialect.py -k test_multiple_host_real_connect triggers it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions