From c6a0da63aeb4955f8f14a5a841c3db59d04c83b9 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 22 Jan 2021 22:56:16 +0100 Subject: XEP-0199: cancel ongoing handlers on session end and keep track of them but be careful to not store too many fix for #3442 --- slixmpp/plugins/xep_0199/ping.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/slixmpp/plugins/xep_0199/ping.py b/slixmpp/plugins/xep_0199/ping.py index d1a82026..cef219ea 100644 --- a/slixmpp/plugins/xep_0199/ping.py +++ b/slixmpp/plugins/xep_0199/ping.py @@ -66,6 +66,7 @@ class XEP_0199(BasePlugin): """ register_stanza_plugin(Iq, Ping) + self.__pending_futures = [] self.xmpp.register_handler( Callback('Ping', @@ -90,6 +91,11 @@ class XEP_0199(BasePlugin): def session_bind(self, jid): self.xmpp['xep_0030'].add_feature(Ping.namespace) + def session_end(self, event): + for future in self.__pending_futures: + future.cancel() + self.__pending_futures.clear() + def enable_keepalive(self, interval=None, timeout=None): if interval: self.interval = interval @@ -97,10 +103,20 @@ class XEP_0199(BasePlugin): self.timeout = timeout self.keepalive = True - handler = lambda event=None: asyncio.ensure_future( - self._keepalive(event), - loop=self.xmpp.loop, - ) + def handler(event): + # Cleanup futures + if self.__pending_futures: + tmp_futures = [] + for future in self.__pending_futures[:]: + if not future.done(): + tmp_futures.append(future) + self.__pending_futures = tmp_futures + + future = asyncio.ensure_future( + self._keepalive(event), + loop=self.xmpp.loop, + ) + self.__pending_futures.append(future) self.xmpp.schedule('Ping keepalive', self.interval, handler, -- cgit v1.2.3