<p dir="auto"><strong>Migrated issue, originally created by Wichert Akkerman (<a href="https://github.com/wichert">@wichert</a>)</strong></p>
<p dir="auto">Except for the first entry all search results are listed twice. As an example here are the current search results for <a href="http://docs.sqlalchemy.org/en/latest/search.html?q=query&amp;check_keywords=yes&amp;area=default" rel="nofollow">query</a>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="sqlalchemy.ext.horizontal_shard.ShardedQuery (Python class, in Horizontal Sharding)
sqlalchemy.ext.horizontal_shard.ShardedQuery.set_shard (Python method, in Horizontal Sharding)
sqlalchemy.ext.horizontal_shard.ShardedQuery.set_shard (Python method, in Horizontal Sharding)
sqlalchemy.orm.query.Query (Python class, in Querying)
sqlalchemy.orm.query.Query (Python class, in Querying)
sqlalchemy.orm.query.Query.add_column (Python method, in Querying)
sqlalchemy.orm.query.Query.add_column (Python method, in Querying)
sqlalchemy.orm.query.Query.add_columns (Python method, in Querying)
sqlalchemy.orm.query.Query.add_columns (Python method, in Querying)
sqlalchemy.orm.query.Query.add_entity (Python method, in Querying)
sqlalchemy.orm.query.Query.add_entity (Python method, in Querying)"><pre class="notranslate"><code class="notranslate">sqlalchemy.ext.horizontal_shard.ShardedQuery (Python class, in Horizontal Sharding)
sqlalchemy.ext.horizontal_shard.ShardedQuery.set_shard (Python method, in Horizontal Sharding)
sqlalchemy.ext.horizontal_shard.ShardedQuery.set_shard (Python method, in Horizontal Sharding)
sqlalchemy.orm.query.Query (Python class, in Querying)
sqlalchemy.orm.query.Query (Python class, in Querying)
sqlalchemy.orm.query.Query.add_column (Python method, in Querying)
sqlalchemy.orm.query.Query.add_column (Python method, in Querying)
sqlalchemy.orm.query.Query.add_columns (Python method, in Querying)
sqlalchemy.orm.query.Query.add_columns (Python method, in Querying)
sqlalchemy.orm.query.Query.add_entity (Python method, in Querying)
sqlalchemy.orm.query.Query.add_entity (Python method, in Querying)
</code></pre></div>
<p dir="auto"><strong>Describe the bug</strong><br>
When using AsyncSession in a session_maker and scoped_session around that, the .close() call on the AsyncSession object is not awaited.</p>
<p dir="auto"><strong>To Reproduce</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from asyncio import current_task, run

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import scoped_session, sessionmaker


engine = create_async_engine('sqlite+aiosqlite:///test.db')
async_session = scoped_session(sessionmaker(engine, expire_on_commit=False, class_=AsyncSession),
                               scopefunc=current_task)


async def main():
    async with async_session() as session:
        pass

    async_session.remove()


run(main())"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">asyncio</span> <span class="pl-k">import</span> <span class="pl-s1">current_task</span>, <span class="pl-s1">run</span>

<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">ext</span>.<span class="pl-s1">asyncio</span> <span class="pl-k">import</span> <span class="pl-s1">create_async_engine</span>, <span class="pl-v">AsyncSession</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">orm</span> <span class="pl-k">import</span> <span class="pl-s1">scoped_session</span>, <span class="pl-s1">sessionmaker</span>


<span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-en">create_async_engine</span>(<span class="pl-s">'sqlite+aiosqlite:///test.db'</span>)
<span class="pl-s1">async_session</span> <span class="pl-c1">=</span> <span class="pl-en">scoped_session</span>(<span class="pl-en">sessionmaker</span>(<span class="pl-s1">engine</span>, <span class="pl-s1">expire_on_commit</span><span class="pl-c1">=</span><span class="pl-c1">False</span>, <span class="pl-s1">class_</span><span class="pl-c1">=</span><span class="pl-v">AsyncSession</span>),
                               <span class="pl-s1">scopefunc</span><span class="pl-c1">=</span><span class="pl-s1">current_task</span>)


<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">main</span>():
    <span class="pl-k">async</span> <span class="pl-k">with</span> <span class="pl-en">async_session</span>() <span class="pl-k">as</span> <span class="pl-s1">session</span>:
        <span class="pl-k">pass</span>

    <span class="pl-s1">async_session</span>.<span class="pl-en">remove</span>()


<span class="pl-en">run</span>(<span class="pl-en">main</span>())</pre></div>
<p dir="auto"><strong>Error</strong><br>
Provide the complete text of any errors received <strong>including the complete<br>
stack trace</strong>.   If the message is a warning, run your program with the<br>
<code class="notranslate">-Werror</code> flag:   <code class="notranslate">python -Werror myprogram.py</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="D:\dev\projects\async-test\site-packages\sqlalchemy\orm\scoping.py:145: RuntimeWarning: coroutine 'AsyncSession.close' was never awaited
  self.registry().close()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback"><pre class="notranslate"><code class="notranslate">D:\dev\projects\async-test\site-packages\sqlalchemy\orm\scoping.py:145: RuntimeWarning: coroutine 'AsyncSession.close' was never awaited
  self.registry().close()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
</code></pre></div>
<p dir="auto"><strong>Versions.</strong></p>
<ul dir="auto">
<li>OS: Windows 10</li>
<li>Python: 3.9.2</li>
<li>SQLAlchemy: 1.4.15</li>
<li>Database: -</li>
<li>DBAPI: aiosqlite 0.17.0</li>
</ul>
<p dir="auto"><strong>Additional context</strong><br>
Test is provided using aiosqlite, because it's simple, but this fails using asyncpg as well.</p>
<p dir="auto"><strong>Have a nice day!</strong></p>