summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-02-11 16:48:05 +0000
committerErik Johnston <erik@matrix.org>2015-02-11 16:48:05 +0000
commitef276e8770e19c66d14462aa325b9cb241121bb6 (patch)
tree7ff0f18564a9e06437dc2f6098996e189a08c1c9 /synapse/util
parentMerge branch 'develop' of github.com:matrix-org/synapse into timeout-federati... (diff)
downloadsynapse-ef276e8770e19c66d14462aa325b9cb241121bb6.tar.xz
Fix so timing out connections to actually work.
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/__init__.py52
1 files changed, 43 insertions, 9 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index 29f1344c5b..e77eba90ad 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -18,6 +18,9 @@ from synapse.util.logcontext import LoggingContext
 from twisted.internet import defer, reactor, task
 
 import time
+import logging
+
+logger = logging.getLogger(__name__)
 
 
 class Clock(object):
@@ -55,20 +58,51 @@ class Clock(object):
         timer.cancel()
 
     def time_bound_deferred(self, given_deferred, time_out):
+        if given_deferred.called:
+            return given_deferred
+
         ret_deferred = defer.Deferred()
 
-        def timed_out():
-            if not given_deferred.called:
-                given_deferred.cancel()
+        def timed_out_fn():
+            try:
                 ret_deferred.errback(RuntimeError("Timed out"))
+            except:
+                pass
+
+            try:
+                given_deferred.cancel()
+            except:
+                pass
+
+        timer = None
+
+        def cancel(res):
+            try:
+                self.cancel_call_later(timer)
+            except:
+                pass
+            return res
+
+        ret_deferred.addBoth(cancel)
+
+        def sucess(res):
+            try:
+                ret_deferred.callback(res)
+            except:
+                pass
+
+            return res
+
+        def err(res):
+            try:
+                ret_deferred.errback(res)
+            except:
+                pass
 
-        timer = self.call_later(time_out, timed_out)
+            return res
 
-        def succeed(result):
-            self.cancel_call_later(timer)
-            ret_deferred.callback(result)
+        given_deferred.addCallbacks(callback=sucess, errback=err)
 
-        given_deferred.addCallback(succeed)
-        given_deferred.addErrback(ret_deferred.errback)
+        timer = self.call_later(time_out, timed_out_fn)
 
         return ret_deferred