From f40dbe860496f544c90182dc1ea3f0f0e641aa97 Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 8 Sep 2021 17:58:48 +0200 Subject: [PATCH] plugin XEP-0384: fix calls outside of main thread: `callFromThread` is used instead of `callLater` with a 0 delay, as the later is not made to be called outside of main thread (which was resulting in huge and random delays during calls). fix `OmemoStorage._callMainThread` which was returning wrong Deferred due a late lambda evaluation. --- sat/plugins/plugin_xep_0384.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sat/plugins/plugin_xep_0384.py b/sat/plugins/plugin_xep_0384.py index 1027062b..36a2f4ba 100644 --- a/sat/plugins/plugin_xep_0384.py +++ b/sat/plugins/plugin_xep_0384.py @@ -115,8 +115,8 @@ def promise2Deferred(promise_): """ d = defer.Deferred() promise_.then( - lambda result: reactor.callLater(0, d.callback, result), - lambda exc: reactor.callLater(0, d.errback, exc) + lambda result: reactor.callFromThread(d.callback, result), + lambda exc: reactor.callFromThread(d.errback, exc) ) return d @@ -145,11 +145,13 @@ class OmemoStorage(omemo.Storage): deferred.addErrback(partial(callback, False)) def _callMainThread(self, callback, method, *args, check_jid=None): - d = method(*args) - if check_jid is not None: + if check_jid is None: + d = method(*args) + else: check_jid_d = self._checkJid(check_jid) - check_jid_d.addCallback(lambda __: d) + check_jid_d.addCallback(lambda __: method(*args)) d = check_jid_d + if callback is not None: d.addCallback(partial(callback, True)) d.addErrback(partial(callback, False)) @@ -160,8 +162,8 @@ class OmemoStorage(omemo.Storage): This method use reactor.callLater to launch Deferred in main thread @param check_jid: run self._checkJid before method """ - reactor.callLater( - 0, self._callMainThread, callback, method, *args, check_jid=check_jid + reactor.callFromThread( + self._callMainThread, callback, method, *args, check_jid=check_jid ) def _checkJid(self, bare_jid): @@ -288,7 +290,7 @@ class OmemoStorage(omemo.Storage): def deleteJID(self, callback, bare_jid): """Retrieve all (in)actives devices of bare_jid, and delete all related keys""" - reactor.callLater(0, self._deleteJID, callback, bare_jid) + reactor.callFromThread(self._deleteJID, callback, bare_jid) class SatOTPKPolicy(omemo.DefaultOTPKPolicy):