summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--synapse/http/matrixfederationclient.py8
-rw-r--r--synapse/util/__init__.py21
2 files changed, 27 insertions, 2 deletions
diff --git a/synapse/http/matrixfederationclient.py b/synapse/http/matrixfederationclient.py
index 056d446e42..96f4250167 100644
--- a/synapse/http/matrixfederationclient.py
+++ b/synapse/http/matrixfederationclient.py
@@ -79,6 +79,7 @@ class MatrixFederationHttpClient(object):
         self.signing_key = hs.config.signing_key[0]
         self.server_name = hs.hostname
         self.agent = MatrixFederationHttpAgent(reactor)
+        self.clock = hs.get_clock()
 
     @defer.inlineCallbacks
     def _create_request(self, destination, method, path_bytes,
@@ -118,7 +119,7 @@ class MatrixFederationHttpClient(object):
 
             try:
                 with PreserveLoggingContext():
-                    response = yield self.agent.request(
+                    request_deferred = self.agent.request(
                         destination,
                         endpoint,
                         method,
@@ -129,6 +130,11 @@ class MatrixFederationHttpClient(object):
                         producer
                     )
 
+                    response = yield self.clock.time_bound_deferred(
+                        request_deferred,
+                        time_out=60,
+                    )
+
                 logger.debug("Got response to %s", method)
                 break
             except Exception as e:
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index fee76b0a9b..29f1344c5b 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -15,7 +15,7 @@
 
 from synapse.util.logcontext import LoggingContext
 
-from twisted.internet import reactor, task
+from twisted.internet import defer, reactor, task
 
 import time
 
@@ -53,3 +53,22 @@ class Clock(object):
 
     def cancel_call_later(self, timer):
         timer.cancel()
+
+    def time_bound_deferred(self, given_deferred, time_out):
+        ret_deferred = defer.Deferred()
+
+        def timed_out():
+            if not given_deferred.called:
+                given_deferred.cancel()
+                ret_deferred.errback(RuntimeError("Timed out"))
+
+        timer = self.call_later(time_out, timed_out)
+
+        def succeed(result):
+            self.cancel_call_later(timer)
+            ret_deferred.callback(result)
+
+        given_deferred.addCallback(succeed)
+        given_deferred.addErrback(ret_deferred.errback)
+
+        return ret_deferred