igorbenav / FastAPI-boilerplate

An extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0, PostgreSQL and Redis.
MIT License
663 stars 80 forks source link

Running via 'uvicorn' from scratch on M2 Macbook fails #127

Closed CHE1RON closed 8 months ago

CHE1RON commented 8 months ago

Describe the bug or question When running the project's "From Scratch" instructions, I get this error:

❯ poetry run uvicorn src.app.main:app --reload
INFO:     Will watch for changes in these directories: ['/path/to/project']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [37118] using StatReload
INFO:     Started server process [37122]
INFO:     Waiting for application startup.
ERROR:    Traceback (most recent call last):
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 734, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/src/app/core/setup.py", line 92, in lifespan
    await create_tables()
  File "/path/to/project/src/app/core/setup.py", line 34, in create_tables
    async with engine.begin() as conn:
  File "/opt/homebrew/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/engine.py", line 1055, in begin
    async with conn:
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/base.py", line 121, in __aenter__
    return await self.start(is_ctxmanager=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/engine.py", line 270, in start
    await greenlet_spawn(self.sync_engine.connect)
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 197, in greenlet_spawn
    result = context.throw(*sys.exc_info())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3279, in connect
    return self._connection_cls(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3303, in raw_connection
    return self.pool.connect()
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 449, in connect
    return _ConnectionFairy._checkout(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1263, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 712, in checkout
    rec = pool._do_get()
          ^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 179, in _do_get
    with util.safe_reraise():
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 177, in _do_get
    return self._create_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 390, in _create_connection
    return _ConnectionRecord(self)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 674, in __init__
    self.__connect()
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 900, in __connect
    with util.safe_reraise():
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 896, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 643, in connect
    return dialect.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 616, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 937, in connect
    await_only(creator_fn(*arg, **kw)),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 127, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 192, in greenlet_spawn
    value = await result
            ^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connection.py", line 2329, in connect
    return await connect_utils._connect(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 1017, in _connect
    raise last_error or exceptions.TargetServerAttributeNotMatched(
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 991, in _connect
    conn = await _connect_addr(
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 828, in _connect_addr
    return await __connect_addr(params, True, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 873, in __connect_addr
    tr, pr = await connector
             ^^^^^^^^^^^^^^^
  File "/Users/username/Library/Caches/pypoetry/virtualenvs/fastapi-boilerplate-lraqQfQg-py3.12/lib/python3.12/site-packages/asyncpg/connect_utils.py", line 744, in _create_ssl_connection
    tr, pr = await loop.create_connection(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1978, in create_connection
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

ERROR:    Application startup failed. Exiting.

Description As you can see, Redis & PostgreSQL containers are running as per the instructions:

image

I'm running this example on an M2 Macbook Air with macOS 14 (Sonoma), and every "solution" I could dig up so far (changing hostname, adding stuff to /etc/hosts) did not work ..

CHE1RON commented 8 months ago

Mhh, after further investigation, this problem has nothing to do with your code, so I'm closing this issue!