--- /dev/null
+.. change::
+ :tags: bug, asyncio
+ :tickets: 8516
+
+ Improved implementation of ``asyncio.shield()`` used in context managers as
+ added in :ticket:`8145`, such that the "close" operation is enclosed within
+ an ``asyncio.Task`` which is then strongly referenced as the operation
+ proceeds. This is per Python documentation indicating that the task is
+ otherwise not strongly referenced.
+# ext/asyncio/base.py
+# Copyright (C) 2020-2022 the SQLAlchemy authors and contributors
+# <see AUTHORS file>
+#
+# This module is part of SQLAlchemy and is released under
+# the MIT License: https://www.opensource.org/licenses/mit-license.php
import abc
import functools
import weakref
return self.start().__await__()
async def __aexit__(self, type_, value, traceback):
- await asyncio.shield(self.close())
+ task = asyncio.create_task(self.close())
+ await asyncio.shield(task)
@util.create_proxy_methods(
await self.transaction.__aexit__(type_, value, traceback)
await self.conn.close()
- await asyncio.shield(go())
+ task = asyncio.create_task(go())
+ await asyncio.shield(task)
def __init__(self, sync_engine):
if not sync_engine.dialect.is_async:
#
# This module is part of SQLAlchemy and is released under
# the MIT License: https://www.opensource.org/licenses/mit-license.php
-
import asyncio
from . import engine
return self
async def __aexit__(self, type_, value, traceback):
- await asyncio.shield(self.close())
+ task = asyncio.create_task(self.close())
+ await asyncio.shield(task)
def _maker_context_manager(self):
# no @contextlib.asynccontextmanager until python3.7, gr
await self.trans.__aexit__(type_, value, traceback)
await self.async_session.__aexit__(type_, value, traceback)
- await asyncio.shield(go())
+ task = asyncio.create_task(go())
+ await asyncio.shield(task)
class AsyncSessionTransaction(ReversibleProxy, StartableContext):