MagicStack / MagicStack/asyncpg

TimeoutError on multiple requests

Open
#1,076 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

* **asyncpg version**: 0.27.0
* **PostgreSQL version**: 14.7
* **Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?**: We can reproduce it, not locally
* **Python version**: 3.10.2
* **Platform**: Backend: Linux, t3large AWS instance, Database: AWS RDS
* **Do you use pgbouncer?**: No
* **Did you install asyncpg with pip?**: Yes

We're using FastAPI ,and our current code for setting up the db connection is:

```
connection_arguments: Dict[str, Any] = {"server_settings": {"jit": "off"}}

ENUM_TYPES = (
("public", "sometype"), ...
)

async def register_enum_types(conn) -> None:
for enum_class, typename in ENUM_TYPES:
await conn.set_builtin_type_codec(
typename, schema=enum_class, codec_name="text"
)

engine = create_async_engine(
settings.uri,
echo=False,
connect_args=connection_arguments,
pool_size=200,
max_overflow=10,
)

async_sessionmaker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)

@event.listens_for(engine.sync_engine, "connect")
def on_connect(dbapi_connection, connection_record):
dbapi_connection.run_async(register_enum_types)
```

We added the `register_enum_types` as we were thinking that the query:
`SELECT t.oid, t.typelem AS elemtype, t.typtype AS kind FROM pg_catalog.pg_type AS t WHERE t.oid = $1`

which we saw a lot in AWS RDS monitoring is causing the issue, but now we're not sure what is it anymore.

In logs when it happens we see:

`File "/path/to/file/python3.10/site-packages/asyncpg/connect_utils.py", line 780, in _connect_addr
raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError`

and after some time, it gets back to normal.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at asyncpg/connect_utils.py:780, where the reported asyncio.TimeoutError is raised, and review the connection setup using FastAPI, SQLAlchemy pooling, and AWS RDS. Compare the timeout logs with the concurrent pool settings and the reported pg_catalog query. Done means establishing a reproducible cause and a verified fix or clear diagnosis.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, postgresql, python
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.