Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3845 from matrix-org/erikj/timeout_reads
Browse files Browse the repository at this point in the history
Timeout reading body for outbound HTTP requests
  • Loading branch information
hawkowl authored Sep 12, 2018
2 parents b041115 + 649c647 commit 4073f73
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/3845.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix outbound requests occasionally wedging, which can result in federation breaking between servers.
52 changes: 45 additions & 7 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ def _request(self, destination, method, path,
# :'(
# Update transactions table?
with logcontext.PreserveLoggingContext():
body = yield treq.content(response)
body = yield self._timeout_deferred(
treq.content(response),
timeout,
)
raise HttpResponseException(
response.code, response.phrase, body
)
Expand Down Expand Up @@ -394,7 +397,10 @@ def put_json(self, destination, path, args={}, data={},
check_content_type_is_json(response.headers)

with logcontext.PreserveLoggingContext():
body = yield treq.json_content(response)
body = yield self._timeout_deferred(
treq.json_content(response),
timeout,
)
defer.returnValue(body)

@defer.inlineCallbacks
Expand Down Expand Up @@ -444,7 +450,10 @@ def post_json(self, destination, path, data={}, long_retries=False,
check_content_type_is_json(response.headers)

with logcontext.PreserveLoggingContext():
body = yield treq.json_content(response)
body = yield self._timeout_deferred(
treq.json_content(response),
timeout,
)

defer.returnValue(body)

Expand Down Expand Up @@ -496,7 +505,10 @@ def get_json(self, destination, path, args=None, retry_on_dns_fail=True,
check_content_type_is_json(response.headers)

with logcontext.PreserveLoggingContext():
body = yield treq.json_content(response)
body = yield self._timeout_deferred(
treq.json_content(response),
timeout,
)

defer.returnValue(body)

Expand Down Expand Up @@ -543,7 +555,10 @@ def delete_json(self, destination, path, long_retries=False,
check_content_type_is_json(response.headers)

with logcontext.PreserveLoggingContext():
body = yield treq.json_content(response)
body = yield self._timeout_deferred(
treq.json_content(response),
timeout,
)

defer.returnValue(body)

Expand Down Expand Up @@ -585,15 +600,38 @@ def get_file(self, destination, path, output_stream, args={},

try:
with logcontext.PreserveLoggingContext():
length = yield _readBodyToFile(
response, output_stream, max_size
length = yield self._timeout_deferred(
_readBodyToFile(
response, output_stream, max_size
),
)
except Exception:
logger.exception("Failed to download body")
raise

defer.returnValue((length, headers))

def _timeout_deferred(self, deferred, timeout_ms=None):
"""Times the deferred out after `timeout_ms` ms
Args:
deferred (Deferred)
timeout_ms (int|None): Timeout in milliseconds. If None defaults
to 60 seconds.
Returns:
Deferred
"""

add_timeout_to_deferred(
deferred,
timeout_ms / 1000. if timeout_ms else 60,
self.hs.get_reactor(),
cancelled_to_request_timed_out_error,
)

return deferred


class _ReadBodyToFileProtocol(protocol.Protocol):
def __init__(self, stream, deferred, max_size):
Expand Down

0 comments on commit 4073f73

Please sign in to comment.