Skip to content

Commit e70a0b0

Browse files
committed
try to gracefully close even in terminate
Adjusted the asyncpg dialect such that when the ``terminate()`` method is used to discard an invalidated connection, the dialect will first attempt to gracefully close the conneciton using ``.close()`` with a timeout, if the operation is proceeding within an async event loop context only. This allows the asyncpg driver to attend to finalizing a ``TimeoutError`` including being able to close a long-running query server side, which otherwise can keep running after the program has exited. Fixes: #10717 Change-Id: Iaba0aeb67873a7a2b3981d43f4eb663005057309
1 parent 82690b1 commit e70a0b0

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. change::
2+
:tags: bug, postgresql
3+
:tickets: 10717
4+
5+
Adjusted the asyncpg dialect such that when the ``terminate()`` method is
6+
used to discard an invalidated connection, the dialect will first attempt
7+
to gracefully close the conneciton using ``.close()`` with a timeout, if
8+
the operation is proceeding within an async event loop context only. This
9+
allows the asyncpg driver to attend to finalizing a ``TimeoutError``
10+
including being able to close a long-running query server side, which
11+
otherwise can keep running after the program has exited.

‎lib/sqlalchemy/dialects/postgresql/asyncpg.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182

183183
from __future__ import annotations
184184

185+
import asyncio
185186
import collections
186187
import decimal
187188
import json as _py_json
@@ -941,7 +942,23 @@ def close(self):
941942
self.await_(self._connection.close())
942943

943944
def terminate(self):
944-
self._connection.terminate()
945+
if util.concurrency.in_greenlet():
946+
# in a greenlet; this is the connection was invalidated
947+
# case.
948+
try:
949+
# try to gracefully close; see #10717
950+
# timeout added in asyncpg 0.14.0 December 2017
951+
self.await_(self._connection.close(timeout=2))
952+
except asyncio.TimeoutError:
953+
# in the case where we are recycling an old connection
954+
# that may have already been disconnected, close() will
955+
# fail with the above timeout. in this case, terminate
956+
# the connection without any further waiting.
957+
# see issue #8419
958+
self._connection.terminate()
959+
else:
960+
# not in a greenlet; this is the gc cleanup case
961+
self._connection.terminate()
945962
self._started = False
946963

947964
@staticmethod

‎lib/sqlalchemy/util/concurrency.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ def _safe_cancel_awaitable(awaitable: Awaitable[Any]) -> None:
169169
awaitable.close()
170170

171171

172+
def in_greenlet() -> bool:
173+
current = _concurrency_shim.getcurrent()
174+
return isinstance(current, _concurrency_shim._AsyncIoGreenlet)
175+
176+
172177
def await_only(awaitable: Awaitable[_T]) -> _T:
173178
"""Awaits an async function in a sync method.
174179

0 commit comments

Comments
 (0)