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

Handle bad JSON data being returned from the federation API. #9070

Merged
merged 3 commits into from
Jan 12, 2021
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/9070.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `JSONDecodeError` spamming the logs when sending transactions to remote servers.
10 changes: 10 additions & 0 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ async def _handle_json_response(
d = timeout_deferred(d, timeout=timeout_sec, reactor=reactor)

body = await make_deferred_yieldable(d)
except ValueError as e:
# The JSON content was invalid.
Comment on lines +177 to +178
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not catch JSONDecodeError here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arg, I forget half the time there's a specific error for this. Thank you!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so we need to catch ValueError since we manually throw that when doing JSON validation:

def _reject_invalid_json(val):
"""Do not allow Infinity, -Infinity, or NaN values in JSON."""
raise ValueError("Invalid JSON value: '%s'" % val)

I had looked at raising a JSONDecodeError there, but you don't really have the necessary information, since we don't have the position of what is being parsed.

A couple of options:

  • Leave this as a ValueError
  • Change the error raised by _reject_invalid_json to a custom exception InvalidJsonError (which inherits from ValueError) and catch both here.
  • Raise JSONDecodeError in _reject_invalid_json by giving a dummy position.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I don't feel very strongly. May as well leave it as-is.

logger.warning(
"{%s} [%s] Failed to parse JSON response - %s %s",
request.txn_id,
request.destination,
request.method,
request.uri.decode("ascii"),
)
raise RequestSendFailed(e, can_retry=False) from e
except defer.TimeoutError as e:
logger.warning(
"{%s} [%s] Timed out reading response - %s %s",
Expand Down
2 changes: 1 addition & 1 deletion tests/http/test_fedclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,4 @@ def test_json_error(self, return_value):
self.pump()

f = self.failureResultOf(test_d)
self.assertIsInstance(f.value, ValueError)
self.assertIsInstance(f.value, RequestSendFailed)