changeset 3672:e4054b648111

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.
author Goffi <goffi@goffi.org>
date Wed, 08 Sep 2021 17:58:48 +0200 (2021-09-08)
parents 9c50d2f812c1
children bd13391ee29e
files sat/plugins/plugin_xep_0384.py
diffstat 1 files changed, 10 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0384.py	Wed Sep 08 17:58:48 2021 +0200
+++ b/sat/plugins/plugin_xep_0384.py	Wed Sep 08 17:58:48 2021 +0200
@@ -115,8 +115,8 @@
     """
     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 @@
         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 @@
         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 @@
 
     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):