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

Apply a timeout to reading the body when fetching a file. #11784

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11784.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug that media streams could cause long-lived connections when generating URL previews.
15 changes: 12 additions & 3 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,15 +731,24 @@ async def get_file(
# straight back in again

try:
length = await make_deferred_yieldable(
read_body_with_max_size(response, output_stream, max_size)
)
d = read_body_with_max_size(response, output_stream, max_size)

# Ensure that the body is not read forever.
d = timeout_deferred(d, 30, self.hs.get_reactor())

length = await make_deferred_yieldable(d)
except BodyExceededMaxSize:
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
"Requested file is too large > %r bytes" % (max_size,),
Codes.TOO_LARGE,
)
except defer.TimeoutError:
raise SynapseError(
HTTPStatus.BAD_GATEWAY,
"Requested file took too long to download",
Codes.TOO_LARGE,
)
except Exception as e:
raise SynapseError(
HTTPStatus.BAD_GATEWAY, ("Failed to download remote body: %s" % e)
Expand Down