asyncpg InterfaceError: another operation is in progress with SQLAlchemy
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progressShort answer
A single database connection can run one operation at a time. The error means two tasks used the same connection or the same AsyncSession at once. Give each concurrent task its own session or its own pool connection, and do not share one engine across event loops.
Fix it fast
1Find out which cause you have
Find where several coroutines share one session or connection: asyncio.gather, create_task, or a module-level session.
grep -rn "gather\|create_task\|AsyncSession(\|async_sessionmaker" .2Match what you see, then jump to its fix
- 1
The error appears when queries run inside asyncio.gather() with the same session objectOne AsyncSession is used by several concurrent tasksFix - 2
Works with one request at a time, fails when two requests overlapA module-level or global session is shared by every requestFix - 3
You use asyncpg directly with one connect() result passed to several tasksA raw asyncpg connection is shared between tasksFix - 4
Error shows in tests or scripts that call asyncio.run() several times, often next to 'attached to a different loop'One engine is used across different event loopsFix
- 1
What does the error look like?
asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progressInterfaceError raised from a FastAPI endpoint that runs several queries at once
Cause 1 of 4
One AsyncSession is used by several concurrent tasks
1Check
The SQLAlchemy docs warn that a single AsyncSession is not safe for use in multiple concurrent tasks. It represents one stateful transaction in progress.
grep -rn -B3 "gather(" . --include=*.py2Fix
Open a separate session for each task, from a session factory.
import asyncio
from sqlalchemy import text
async def load(factory, user_id):
async with factory() as session:
return await session.scalar(text("SELECT name FROM users WHERE id = :i"), {"i": user_id})
async def main(factory):
return await asyncio.gather(load(factory, 1), load(factory, 2))3Verify
Run the same gather call; both queries return and no InterfaceError is raised.
python main.pyConfirmed by docs.sqlalchemy.org
Cause 2 of 4
A module-level or global session is shared by every request
1Check
In FastAPI a session created once at import time is shared by every concurrent request.
grep -rn "AsyncSession(" . --include=*.py2Fix
Create the engine and the sessionmaker once, but open a session per request in a dependency.
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
engine = create_async_engine("postgresql+asyncpg://app:secret@db/appdb")
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def get_session():
async with SessionLocal() as session:
yield session3Verify
Send two overlapping requests to the endpoint; both succeed.
curl -s localhost:8000/items & curl -s localhost:8000/items & waitConfirmed by docs.sqlalchemy.org
Cause 3 of 4
A raw asyncpg connection is shared between tasks
1Check
asyncpg's pool hands out one connection per acquire() call, and returns it with release(). One connection handed to many tasks has no such separation.
grep -rn "asyncpg.connect\|create_pool" . --include=*.py2Fix
Create a pool and acquire a connection inside each task.
import asyncpg
pool = await asyncpg.create_pool(dsn="postgresql://app:secret@db/appdb", min_size=2, max_size=10)
async def fetch_user(user_id):
async with pool.acquire() as con:
return await con.fetchrow("SELECT * FROM users WHERE id = $1", user_id)3Verify
Run the tasks concurrently with gather; no operation overlaps on one connection.
python main.pyConfirmed by magicstack.github.io
Cause 4 of 4
One engine is used across different event loops
1Check
The docs say not to share one AsyncEngine between event loops with the default pool. Connections made on one loop are not valid on another.
grep -rn "asyncio.run\|new_event_loop" . --include=*.py2Fix
Call await engine.dispose() before the engine moves to another loop, or create the engine with NullPool when it must be shared.
from sqlalchemy.pool import NullPool
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine("postgresql+asyncpg://app:secret@db/appdb", poolclass=NullPool)3Verify
Run the test suite twice in one process; no loop errors appear.
pytest -qConfirmed by docs.sqlalchemy.org
How do I stop it coming back?
- One session per request or per task, never a global one.
- Treat AsyncSession like a transaction: short-lived and not shared.
- Use a pool, not a single connection, in a server that handles requests concurrently.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Django cannot load MySQLdb and asks whether mysqlclient is installed
Error loading MySQLdb module: Did you install mysqlclient or MySQL-python?
Django's MySQL backend imports MySQLdb, which is provided by the mysqlclient package.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fixDatabases
GeoDjango on Windows says it could not find the GDAL library (WinError 126)
OSError: [WinError 126] The specified module could not be found
GeoDjango needs the GDAL, GEOS and PROJ C libraries, and Windows does not ship them.
3 causes, each with a fix