Skip to content

Sometimes got error  #899

@redradist

Description

@redradist

Describe the bug

Sometimes I see the following issue:

async with repo:
  File "<server>/src/database/repository.py", line 54, in __aenter__
    self._engine = await self._engine_context.__aenter__()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/utils.py", line 82, in __aenter__
    self._obj = await self._coro
                ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/sa/engine.py", line 94, in _create_engine
    pool = await aiopg.create_pool(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/pool.py", line 300, in from_pool_fill
    await self._fill_free_pool(False)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/pool.py", line 336, in _fill_free_pool
    conn = await connect(
                 ^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/connection.py", line 65, in connect
    connection = Connection(
                 ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/connection.py", line 760, in __init__
    self._conn = psycopg2.connect(dsn, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "127.0.0.1", port 5432 failed: Can't assign requested address
        Is the server running on that host and accepting TCP/IP connections?

To Reproduce

  1. Use the following class:
class Repository:
    class Settings:
        def __init__(self, database_config):
            self.user = database_config['user'] if 'user' in database_config else None
            self.database = database_config['database'] if 'database' in database_config else None
            self.host = database_config['host'] if 'host' in database_config else None
            self.port = database_config['port'] if 'port' in database_config else None
            self.password = database_config['password'] if 'password' in database_config else None
            self.connection_pool_size = database_config[
                'connection_pool_size'] if 'connection_pool_size' in database_config else None

    def __init__(self, settings: Settings):
        self.settings = settings
        self._engine_context = None
        self._engine: Engine = None

    async def __aenter__(self):
        if self._engine is None:
            if self.settings.connection_pool_size:
                self._engine_context = create_engine(user=self.settings.user,
                                                     database=self.settings.database,
                                                     host=self.settings.host,
                                                     port=self.settings.port,
                                                     password=self.settings.password,
                                                     maxsize=self.settings.connection_pool_size)
            else:
                self._engine_context = create_engine(user=self.settings.user,
                                                     database=self.settings.database,
                                                     host=self.settings.host,
                                                     port=self.settings.port,
                                                     password=self.settings.password)
            self._engine = await self._engine_context.__aenter__()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self._engine is not None:
            await self._engine_context.__aexit__(exc_type, exc_val, exc_tb)
            self._engine.close()
            self._engine_context.close()
            self._engine = None
            self._engine_context = None
...
  1. Try multiples connections from different sources (web page, console and etc.)

Expected behavior

PostgresSQL connections should be established

Logs/tracebacks

async with repo:
  File "<server>/src/database/repository.py", line 54, in __aenter__
    self._engine = await self._engine_context.__aenter__()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/utils.py", line 82, in __aenter__
    self._obj = await self._coro
                ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/sa/engine.py", line 94, in _create_engine
    pool = await aiopg.create_pool(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/pool.py", line 300, in from_pool_fill
    await self._fill_free_pool(False)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/pool.py", line 336, in _fill_free_pool
    conn = await connect(
                 ^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/connection.py", line 65, in connect
    connection = Connection(
                 ^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiopg/connection.py", line 760, in __init__
    self._conn = psycopg2.connect(dsn, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server at "127.0.0.1", port 5432 failed: Can't assign requested address
        Is the server running on that host and accepting TCP/IP connections?

Python Version

$ python --version
Python 3.10.6

aiopg Version

$ python -m pip show aiopg

OS

Linux, MacOS

Additional context

Here is server config used for Repository settings:

database {
  database = <DATABASE_NAME>
  host = "localhost"
  host = ${?DATABASE_HOST}
  port = 5432
  port = ${?DATABASE_PORT}
  user = "postgres"
  user = ${?DATABASE_USER}
  password = "postgres"
  password = ${?DATABASE_PASSWORD}
  connection_pool_size = 50
  url = "postgresql://"${database.user}":"${database.password}"@"${database.host}":"${database.port}"/"${database.database}
}

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions