From 087ac01fbd8a504599b8bf947c938e8c11355a68 Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Tue, 21 Sep 2021 09:38:56 -0700 Subject: [PATCH] Make sure timeout callbacks always get cleaned up Under certain circumstances the `TimerHandle` used for query timeouts does not get cleaned up in a timely fashion resulting in a temporary (`timeout` seconds) memory leak. Fixes: #830 --- asyncpg/protocol/protocol.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asyncpg/protocol/protocol.pyx b/asyncpg/protocol/protocol.pyx index dbe52e9e..cf463b66 100644 --- a/asyncpg/protocol/protocol.pyx +++ b/asyncpg/protocol/protocol.pyx @@ -647,12 +647,12 @@ cdef class BaseProtocol(CoreProtocol): self.waiter.set_exception(asyncio.TimeoutError()) def _on_waiter_completed(self, fut): + if self.timeout_handle: + self.timeout_handle.cancel() + self.timeout_handle = None if fut is not self.waiter or self.cancel_waiter is not None: return if fut.cancelled(): - if self.timeout_handle: - self.timeout_handle.cancel() - self.timeout_handle = None self._request_cancel() def _create_future_fallback(self):