Skip to content

Commit 3aaa691

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 (cherry picked from commit e70a0b0)
1 parent e5b07fa commit 3aaa691

4 files changed

Lines changed: 37 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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,23 @@ def close(self):
894894
self.await_(self._connection.close())
895895

896896
def terminate(self):
897-
self._connection.terminate()
897+
if util.concurrency.in_greenlet():
898+
# in a greenlet; this is the connection was invalidated
899+
# case.
900+
try:
901+
# try to gracefully close; see #10717
902+
# timeout added in asyncpg 0.14.0 December 2017
903+
self.await_(self._connection.close(timeout=2))
904+
except asyncio.TimeoutError:
905+
# in the case where we are recycling an old connection
906+
# that may have already been disconnected, close() will
907+
# fail with the above timeout. in this case, terminate
908+
# the connection without any further waiting.
909+
# see issue #8419
910+
self._connection.terminate()
911+
else:
912+
# not in a greenlet; this is the gc cleanup case
913+
self._connection.terminate()
898914
self._started = False
899915

900916
@staticmethod

‎lib/sqlalchemy/util/_concurrency_py3k.py‎

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

101101

102+
def in_greenlet() -> bool:
103+
current = getcurrent()
104+
return isinstance(current, _AsyncIoGreenlet)
105+
106+
102107
def await_only(awaitable: Awaitable[_T]) -> _T:
103108
"""Awaits an async function in a sync method.
104109

‎lib/sqlalchemy/util/concurrency.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
have_greenlet = True
2323
from ._concurrency_py3k import await_only as await_only
2424
from ._concurrency_py3k import await_fallback as await_fallback
25+
from ._concurrency_py3k import in_greenlet as in_greenlet
2526
from ._concurrency_py3k import greenlet_spawn as greenlet_spawn
2627
from ._concurrency_py3k import is_exit_exception as is_exit_exception
2728
from ._concurrency_py3k import AsyncAdaptedLock as AsyncAdaptedLock
@@ -56,6 +57,9 @@ def await_only(thing): # type: ignore # noqa: F811
5657
def await_fallback(thing): # type: ignore # noqa: F811
5758
return thing
5859

60+
def in_greenlet(): # type: ignore # noqa: F811
61+
_not_implemented()
62+
5963
def greenlet_spawn(fn, *args, **kw): # type: ignore # noqa: F811
6064
_not_implemented()
6165

0 commit comments

Comments
 (0)