From 7dd3bf5e246144806c83b5ed2127733906908331 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 5 May 2017 10:49:19 +0100 Subject: [PATCH 1/3] Rewrite SimpleHttpClient.request to include timeouts Fixes #2191 --- synapse/http/client.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index 9cf797043..68bd06abd 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -75,36 +75,42 @@ class SimpleHttpClient(object): if hs.config.user_agent_suffix: self.user_agent = "%s %s" % (self.user_agent, hs.config.user_agent_suffix,) + @defer.inlineCallbacks def request(self, method, uri, *args, **kwargs): # A small wrapper around self.agent.request() so we can easily attach # counters to it outgoing_requests_counter.inc(method) - d = preserve_context_over_fn( - self.agent.request, - method, uri, *args, **kwargs - ) + + + def send_request(): + request_deferred = self.agent.request( + method, uri, *args, **kwargs + ) + + return self.clock.time_bound_deferred( + request_deferred, + time_out=60, + ) logger.info("Sending request %s %s", method, uri) - def _cb(response): + try: + with logcontext.PreserveLoggingContext(): + response = yield send_request() + incoming_responses_counter.inc(method, response.code) logger.info( "Received response to %s %s: %s", method, uri, response.code ) return response - - def _eb(failure): + except Exception as e: incoming_responses_counter.inc(method, "ERR") logger.info( "Error sending request to %s %s: %s %s", - method, uri, failure.type, failure.getErrorMessage() + method, uri, type(e).__name__, e.message ) - return failure - - d.addCallbacks(_cb, _eb) - - return d + raise e @defer.inlineCallbacks def post_urlencoded_get_json(self, uri, args={}): From c2ddd773bc23dfc9fa2f20d39547bf6a4bd7d0f1 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 5 May 2017 10:52:46 +0100 Subject: [PATCH 2/3] Include the clock --- synapse/http/client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/http/client.py b/synapse/http/client.py index 68bd06abd..58f12cf8e 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -72,6 +72,7 @@ class SimpleHttpClient(object): contextFactory=hs.get_http_client_context_factory() ) self.user_agent = hs.version_string + self.clock = hs.get_clock() if hs.config.user_agent_suffix: self.user_agent = "%s %s" % (self.user_agent, hs.config.user_agent_suffix,) From d0debb21162df5e6975d8b19e0365961d5f75ddb Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 5 May 2017 11:00:21 +0100 Subject: [PATCH 3/3] Remember how twisted works --- synapse/http/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index 58f12cf8e..9eba046bb 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -19,6 +19,7 @@ from synapse.api.errors import ( CodeMessageException, MatrixCodeMessageException, SynapseError, Codes, ) from synapse.util.logcontext import preserve_context_over_fn +from synapse.util import logcontext import synapse.metrics from synapse.http.endpoint import SpiderEndpoint @@ -82,7 +83,6 @@ class SimpleHttpClient(object): # counters to it outgoing_requests_counter.inc(method) - def send_request(): request_deferred = self.agent.request( method, uri, *args, **kwargs @@ -104,7 +104,7 @@ class SimpleHttpClient(object): "Received response to %s %s: %s", method, uri, response.code ) - return response + defer.returnValue(response) except Exception as e: incoming_responses_counter.inc(method, "ERR") logger.info(