From 3f10dfe138ee0be1c25e3e12546facd3a7b58651 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 22 Jan 2021 22:55:39 +0100 Subject: iq: only update the future if it is not done --- slixmpp/stanza/iq.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/slixmpp/stanza/iq.py b/slixmpp/stanza/iq.py index a3f16e2f..0f53425b 100644 --- a/slixmpp/stanza/iq.py +++ b/slixmpp/stanza/iq.py @@ -194,9 +194,11 @@ class Iq(RootStanza): def callback_success(result): type_ = result['type'] if type_ == 'result': - future.set_result(result) + if not future.done(): + future.set_result(result) elif type_ == 'error': - future.set_exception(IqError(result)) + if not future.done(): + future.set_exception(IqError(result)) else: # Most likely an iq addressed to ourself, rearm the callback. handler = constr(handler_name, @@ -212,7 +214,8 @@ class Iq(RootStanza): callback(result) def callback_timeout(): - future.set_exception(IqTimeout(self)) + if not future.done(): + future.set_exception(IqTimeout(self)) self.stream.remove_handler('IqCallback_%s' % self['id']) if timeout_callback is not None: timeout_callback(self) -- cgit v1.2.3 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 From 9cb5131f1c96d3357398ef64f2989bb98a33acbe Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 23 Jan 2021 15:38:23 +0100 Subject: XEP-0199: Fix handler default parameter, add typing Clear futures when disabling the keepalive, and do it on every disconnect instead of only at session end. --- slixmpp/plugins/xep_0199/ping.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/slixmpp/plugins/xep_0199/ping.py b/slixmpp/plugins/xep_0199/ping.py index cef219ea..3221ba6a 100644 --- a/slixmpp/plugins/xep_0199/ping.py +++ b/slixmpp/plugins/xep_0199/ping.py @@ -9,7 +9,8 @@ import time import logging -from typing import Optional, Callable +from asyncio import Future +from typing import Optional, Callable, List from slixmpp.jid import JID from slixmpp.stanza import Iq @@ -64,9 +65,9 @@ class XEP_0199(BasePlugin): """ Start the XEP-0199 plugin. """ - register_stanza_plugin(Iq, Ping) - self.__pending_futures = [] + + self.__pending_futures: List[Future] = [] self.xmpp.register_handler( Callback('Ping', @@ -76,7 +77,9 @@ class XEP_0199(BasePlugin): if self.keepalive: self.xmpp.add_event_handler('session_start', self.enable_keepalive) - self.xmpp.add_event_handler('session_end', + self.xmpp.add_event_handler('session_resumed', + self.enable_keepalive) + self.xmpp.add_event_handler('disconnected', self.disable_keepalive) def plugin_end(self): @@ -85,16 +88,22 @@ class XEP_0199(BasePlugin): if self.keepalive: self.xmpp.del_event_handler('session_start', self.enable_keepalive) - self.xmpp.del_event_handler('session_end', + self.xmpp.del_event_handler('session_resumed', + self.enable_keepalive) + self.xmpp.del_event_handler('disconnected', self.disable_keepalive) 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 _clear_pending_futures(self): + """Cancel all pending ping futures""" + if self.__pending_futures: + log.debug('Clearing %s pdnding pings', len(self.__pending_futures)) + for future in self.__pending_futures: + future.cancel() + self.__pending_futures.clear() def enable_keepalive(self, interval=None, timeout=None): if interval: @@ -103,7 +112,7 @@ class XEP_0199(BasePlugin): self.timeout = timeout self.keepalive = True - def handler(event): + def handler(event=None): # Cleanup futures if self.__pending_futures: tmp_futures = [] @@ -123,8 +132,11 @@ class XEP_0199(BasePlugin): repeat=True) def disable_keepalive(self, event=None): + self._clear_pending_futures() self.xmpp.cancel_schedule('Ping keepalive') + session_end = disable_keepalive + async def _keepalive(self, event=None): log.debug("Keepalive ping...") try: -- cgit v1.2.3