Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

asyncpg InterfaceError: another operation is in progress with SQLAlchemy

Errorasyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress

Short 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.

  • Applies to SQLAlchemy 2.0, asyncpg 0.30, Python 3.13
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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" .
  2. 2Match what you see, then jump to its fix

    1. 1The error appears when queries run inside asyncio.gather() with the same session objectOne AsyncSession is used by several concurrent tasksFix
    2. 2Works with one request at a time, fails when two requests overlapA module-level or global session is shared by every requestFix
    3. 3You use asyncpg directly with one connect() result passed to several tasksA raw asyncpg connection is shared between tasksFix
    4. 4Error 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

What does the error look like?

  • asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress
  • InterfaceError 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=*.py

2Fix

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.py

Confirmed by docs.sqlalchemy.org

Did this fix it?

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=*.py

2Fix

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 session

3Verify

Send two overlapping requests to the endpoint; both succeed.

curl -s localhost:8000/items & curl -s localhost:8000/items & wait

Confirmed by docs.sqlalchemy.org

Did this fix it?

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=*.py

2Fix

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.py

Confirmed by magicstack.github.io

Did this fix it?

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=*.py

2Fix

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 -q

Confirmed by docs.sqlalchemy.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.