<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Neeraj Kansal on Medium]]></title>
        <description><![CDATA[Stories by Neeraj Kansal on Medium]]></description>
        <link>https://medium.com/@neeraj9194?source=rss-3052777c026f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2w4dMqhH9hbp3hMd.</url>
            <title>Stories by Neeraj Kansal on Medium</title>
            <link>https://medium.com/@neeraj9194?source=rss-3052777c026f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:18:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@neeraj9194/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[FastAPI & async pytest: Event Loop Trap]]></title>
            <link>https://medium.com/@neeraj9194/fastapi-async-pytest-event-loop-trap-03f481a334bf?source=rss-3052777c026f------2</link>
            <guid isPermaLink="false">https://medium.com/p/03f481a334bf</guid>
            <category><![CDATA[asyncio]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[pytest]]></category>
            <category><![CDATA[sqlalchemy]]></category>
            <dc:creator><![CDATA[Neeraj Kansal]]></dc:creator>
            <pubDate>Tue, 14 Apr 2026 13:30:06 GMT</pubDate>
            <atom:updated>2026-04-14T13:30:06.214Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve ever written async tests with FastAPI and SQLAlchemy, chances are you’ve seen this error:</p><blockquote><em>RuntimeError: Task attached to a different loop</em></blockquote><p>I have spent many hours figuring this out here are my findings hoping someone else will not.</p><h4>Setup</h4><p>Ok, here is the setup.</p><p>I have a FastAPI application with SQLAlchemy for ORM and Alembic for migrations. For tests I wanted to use pytest. Since DB and other operations are async, I had to write async tests.</p><p>Here is the example repo I set up for this blog: <a href="https://github.com/neeraj9194/fastapi-async-sqlalchemy-testing">FastAPI + Async Pytest</a></p><p>I have written tests in the past using pytest fixtures, so I know about fixture scopes and what should go in session scope and what should go in <a href="https://docs.pytest.org/en/6.2.x/fixture.html#fixture-scopes">function scope</a>.</p><p>In a typical sync setup you have:</p><ul><li><strong>DB engine</strong>: session scoped, created once and reused to take advantage of connection pooling</li><li><strong>DB Session</strong>: function scoped, created per test, takes a connection from the pool</li><li><strong>Client</strong>: This is a imoprtant fixture in API testing this is the object that will be used to call and even initilize the app that will respond to that call.</li></ul><h4>What breaks in async</h4><p>When you try to run with same config you end up with couple of diffrent errors like</p><ul><li>“<em>got Future &lt;Future pending cb=[…]&gt; attached to a different loop</em>” or</li><li>“<em>Error while inserting record: … cannot perform operation another operation is in progress</em>”</li></ul><p>Both happen when you are sharing or mixing up fixture and test loop scope.</p><blockquote><strong>What are loop scope?<br></strong>Each async program runs on an event loop. The loop schedules and executes async tasks.</blockquote><blockquote>In pytest (pytest-asyncio), loop scope decides: <br>- when the loop is created &amp; <br>- when it is destroyed</blockquote><p>By default,</p><p>By default, everything runs with function loop scope. That means each async def runs in a separate event loop.</p><p>Which is great but with one problem, you cannot share objects between loop so heavy operations that you generally put in “session” scope you will need to initilize everytime.</p><h3>First attempt</h3><p>So you will think, “I will keep DB engine in session scope, and let tests run in function loops” atleast that is what i thought. Something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8VKlc_rWiUHm4W5xx4k5FQ.png" /></figure><p>That is great thinking but then you will end up with a error like this“<em>got Future &lt;Future pending cb=[…]&gt; attached to a different loop</em>”.</p><p>Example:</p><pre>@pytest_asyncio.fixture(scope=&quot;session&quot;, loop_scope=&quot;session&quot;)<br>async def db_engine():<br>    print(id(asyncio.get_running_loop()))    # 1111<br>    yield ...<br><br>@pytest.fixture<br>async def db_session(db_engine):<br>    print(id(asyncio.get_running_loop()))    # 2222<br>    db_engine.connect...<br>    yield ...<br><br><br>async def test_example(db_session):<br>    print(id(asyncio.get_running_loop()))    # 2222</pre><h3>Second attempt</h3><p>You will realise loop objects cannt be shared , but what about global objects that shoud work right?</p><pre>engine: AsyncEngine = create_async_engine(str(DATABASE_URL))<br><br>@pytest.fixture<br>async def db_session():<br>    print(id(asyncio.get_running_loop()))    # 2222<br>    engine.connect...<br>    yield ...<br><br><br>async def test_example(db_session):<br>    print(id(asyncio.get_running_loop()))    # 2222</pre><p>No, same error.</p><p>Here is the important part:</p><blockquote><em>The engine itself is not loop bound, but the connection pool gets bound to the first loop where it is used.</em></blockquote><p>So:</p><ul><li>Test 1 initializes pool in loop A</li><li>Test 2 runs in loop B</li><li>Same pool is reused and it fails</li></ul><h3>Third attempt</h3><p>Now you might try full isolation, making db_engine fixture a function loop scope so it looks somethign like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*n8AJCM4x82M2O6nPBQvIFg.png" /></figure><p>This is a great solution and might even work but</p><ul><li>Engine creation is expensive</li><li>Connection pools are not reused</li><li>Tests become slower</li><li>High DB load if you have many tests</li></ul><h3>Final approach</h3><p>So, you go to the extreme opposite side. Why not use One loop for everything?</p><p>That’s your best option, given the limitations. Even a FastAPI server runs a single loop to handle various async coroutines.</p><p>Example:</p><pre>@pytest_asyncio.fixture(scope=&quot;session&quot;, loop_scope=&quot;session&quot;)<br>async def db_engine():<br>    print(id(asyncio.get_running_loop()))    # 1111<br>    yield ...<br><br>@pytest_asyncio.fixture(scope=&quot;function&quot;, loop_scope=&quot;session&quot;)<br>async def db_session(db_engine):<br>    print(id(asyncio.get_running_loop()))    # 1111<br>    db_engine.connect...<br>    yield ...<br><br>@pytest_asyncio.fixture(scope=&quot;function&quot;, loop_scope=&quot;session&quot;)<br>async def test_example(db_session):<br>    print(id(asyncio.get_running_loop()))    # 1111</pre><p>Now everything runs in the same loop. This works and is more efficient than function loop scope.</p><h3>New problem</h3><p>Now there is another issue.</p><p>Data created in one test leaks into another test.</p><p>When you have multiple test functions and they create/update/delete resources on DB. The <strong>data created in one test will leak into another test</strong> because we are not cleaning up DB tables.</p><h4>Common fix</h4><p>You will write a fixture that will run before every test function something like this.</p><pre>@pytest_asyncio.fixture(scope=&quot;function&quot;, loop_scope=&quot;session&quot;, autouse=True)<br>def migration(db_engine):<br>    async with db_engine.begin() as conn:<br>        await conn.run_sync(Base.metadata.create_all)<br>    <br>    yield &quot;on head&quot;<br>    <br>    async with db_engine.begin() as conn:<br>        await conn.run_sync(Base.metadata.drop_all)</pre><p>This will work fine, but,</p><p>It is slow. Creating and dropping tables for every test is expensive.</p><h4>Better fix: transactions</h4><p>Instead, use transactions, rollback and savepoint.</p><p>Here’s the idea:</p><ul><li>open a DB connection &amp; start a transaction</li><li>run the test</li><li>rollback everything at the end</li><li>if commit is called inside test don’t commit the outer transaction use <strong>savepoint</strong>.</li></ul><p>Example:</p><pre>@pytest_asyncio.fixture(scope=&quot;function&quot;, loop_scope=&quot;session&quot;)<br>async def db_session():<br>    &quot;&quot;&quot;<br>    Create a transactional test database session.<br>    Ref:<br>    https://docs.sqlalchemy.org/en/latest/orm/session_transaction.html<br>    &quot;&quot;&quot;<br>    connection = await engine.connect()<br>    transaction = await connection.begin()<br><br>    async_session = AsyncSession(<br>        bind=connection,<br>        expire_on_commit=False,<br>        join_transaction_mode=&quot;create_savepoint&quot;,<br>    )<br><br>    try:<br>        yield async_session<br>    finally:<br>        await async_session.rollback()<br>        await async_session.close()<br>        await transaction.rollback()<br>        await connection.close()</pre><p>When you do:</p><pre>connection = await engine.connect()<br>transaction = await connection.begin()</pre><p>You are starting a real database transaction. Now anything that happens using this connection is part of that transaction. If you rollback this transaction:</p><pre>await transaction.rollback()</pre><p>Everything done inside it is <strong>undone</strong>.</p><p>Where <strong>savepoints</strong> come in In SQLAlchemy async sessions, we usually do this:</p><p>In SQLAlchemy async sessions, we usually do this:</p><pre>async_session = AsyncSession(<br> bind=connection,<br> expire_on_commit=False,<br> join_transaction_mode=”create_savepoint”,<br>)</pre><p>That <em>join_transaction_mode=”create_savepoint”</em> is important.</p><blockquote>It tells SQLAlchemy: “If something inside the session tries to start or commit a transaction, don’t commit the outer transaction. Just create a savepoint.”</blockquote><p>So DB stays clean without recreating tables.</p><h3>Cleaner config</h3><p>Instead of adding loop_scope=&quot;session&quot; everywhere better way to setup this is using “pytest.ini”</p><pre>[pytest]<br>asyncio_mode = auto<br>asyncio_default_test_loop_scope = session<br>asyncio_default_fixture_loop_scope = session</pre><p>This makes all tests and fixtures run in the same loop.</p><h3>Final takeaway</h3><p>Once you understand this:</p><blockquote>The connection pool is tied to the event loop where it is first used</blockquote><p>Everything becomes clear.</p><p>You have two choices:</p><ul><li>Use one loop and share the pool</li><li>Use multiple loops but do not share the pool</li></ul><p>Trying to mix both will fail.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=03f481a334bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>