summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/changelog/unreleased_14/8516.rst9
-rw-r--r--lib/sqlalchemy/ext/asyncio/engine.py6
-rw-r--r--lib/sqlalchemy/ext/asyncio/session.py6
3 files changed, 17 insertions, 4 deletions
diff --git a/doc/build/changelog/unreleased_14/8516.rst b/doc/build/changelog/unreleased_14/8516.rst
new file mode 100644
index 000000000..2f83586e2
--- /dev/null
+++ b/doc/build/changelog/unreleased_14/8516.rst
@@ -0,0 +1,9 @@
+.. 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.
diff --git a/lib/sqlalchemy/ext/asyncio/engine.py b/lib/sqlalchemy/ext/asyncio/engine.py
index e8ac10a3d..4d0c872b3 100644
--- a/lib/sqlalchemy/ext/asyncio/engine.py
+++ b/lib/sqlalchemy/ext/asyncio/engine.py
@@ -694,7 +694,8 @@ class AsyncConnection(
return self.start().__await__()
async def __aexit__(self, type_: Any, value: Any, traceback: Any) -> None:
- await asyncio.shield(self.close())
+ task = asyncio.create_task(self.close())
+ await asyncio.shield(task)
# START PROXY METHODS AsyncConnection
@@ -860,7 +861,8 @@ class AsyncEngine(ProxyComparable[Engine], AsyncConnectable):
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: Engine):
if not sync_engine.dialect.is_async:
diff --git a/lib/sqlalchemy/ext/asyncio/session.py b/lib/sqlalchemy/ext/asyncio/session.py
index d4b6b6d50..60b77f3ea 100644
--- a/lib/sqlalchemy/ext/asyncio/session.py
+++ b/lib/sqlalchemy/ext/asyncio/session.py
@@ -851,7 +851,8 @@ class AsyncSession(ReversibleProxy[Session]):
return self
async def __aexit__(self, type_: Any, value: Any, traceback: Any) -> None:
- await asyncio.shield(self.close())
+ task = asyncio.create_task(self.close())
+ await asyncio.shield(task)
def _maker_context_manager(self: _AS) -> _AsyncSessionContextManager[_AS]:
return _AsyncSessionContextManager(self)
@@ -1516,7 +1517,8 @@ class _AsyncSessionContextManager(Generic[_AS]):
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(