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

Commit

Permalink
Merge commit 'f43c66d23' into anoa/dinsic_release_1_21_x
Browse files Browse the repository at this point in the history
* commit 'f43c66d23':
  Add support for running Complement against the local checkout (#8317)
  Filter out appservices from mau count (#8404)
  Only assert valid next_link params when provided (#8417)
  Add metrics to track success/otherwise of replication requests (#8406)
  Fix handling of connection timeouts in outgoing http requests (#8400)
  Changelog
  Don't check whether a 3pid is allowed to register during password reset
  Add checks for postgres sequence consistency (#8402)
  Create a mechanism for marking tests "logcontext clean" (#8399)
  Add `ui_auth_sessions_ips` table to `synapse_port_db` ignore list (#8410)
  A pair of tiny cleanups in the federation request code. (#8401)
  typo
  • Loading branch information
anoadragon453 committed Oct 21, 2020
2 parents 5b0b103 + f43c66d commit ab26b58
Show file tree
Hide file tree
Showing 36 changed files with 602 additions and 150 deletions.
1 change: 1 addition & 0 deletions changelog.d/8317.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support testing the local Synapse checkout against the [Complement homeserver test suite](https://github.com/matrix-org/complement/).
1 change: 1 addition & 0 deletions changelog.d/8399.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create a mechanism for marking tests "logcontext clean".
1 change: 1 addition & 0 deletions changelog.d/8400.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect handling of timeouts on outgoing HTTP requests.
1 change: 1 addition & 0 deletions changelog.d/8401.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A pair of tiny cleanups in the federation request code.
1 change: 1 addition & 0 deletions changelog.d/8402.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add checks on startup that PostgreSQL sequences are consistent with their associated tables.
1 change: 1 addition & 0 deletions changelog.d/8404.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not include appservice users when calculating the total MAU for a server.
1 change: 1 addition & 0 deletions changelog.d/8406.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add prometheus metrics for replication requests.
1 change: 1 addition & 0 deletions changelog.d/8410.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a v1.20.0 regression in the `synapse_port_db` script regarding the `ui_auth_sessions_ips` table.
1 change: 1 addition & 0 deletions changelog.d/8414.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unnecessary 3PID registration check when resetting password via an email address. Bug introduced in v0.34.0rc2.
1 change: 1 addition & 0 deletions changelog.d/8417.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a config option to specify a whitelist of domains that a user can be redirected to after validating their email or phone number.
11 changes: 11 additions & 0 deletions docs/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ Note that the above may fail with an error about duplicate rows if corruption
has already occurred, and such duplicate rows will need to be manually removed.


## Fixing inconsistent sequences error

Synapse uses Postgres sequences to generate IDs for various tables. A sequence
and associated table can get out of sync if, for example, Synapse has been
downgraded and then upgraded again.

To fix the issue shut down Synapse (including any and all workers) and run the
SQL command included in the error message. Once done Synapse should start
successfully.


## Tuning Postgres

The default settings should be fine for most deployments. For larger
Expand Down
22 changes: 22 additions & 0 deletions scripts-dev/complement.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#! /bin/bash -eu
# This script is designed for developers who want to test their code
# against Complement.
#
# It makes a Synapse image which represents the current checkout,
# then downloads Complement and runs it with that image.

cd "$(dirname $0)/.."

# Build the base Synapse image from the local checkout
docker build -t matrixdotorg/synapse:latest -f docker/Dockerfile .

# Download Complement
wget -N https://github.com/matrix-org/complement/archive/master.tar.gz
tar -xzf master.tar.gz
cd complement-master

# Build the Synapse image from Complement, based on the above image we just built
docker build -t complement-synapse -f dockerfiles/Synapse.Dockerfile ./dockerfiles

# Run the tests on the resulting image!
COMPLEMENT_BASE_IMAGE=complement-synapse go test -v -count=1 ./tests
1 change: 1 addition & 0 deletions scripts/synapse_port_db
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ IGNORED_TABLES = {
# the sessions are transient anyway, so ignore them.
"ui_auth_sessions",
"ui_auth_sessions_credentials",
"ui_auth_sessions_ips",
}


Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async def on_receive_pdu(self, origin, pdu, sent_to_us_directly=False) -> None:
raise Exception(
"Error fetching missing prev_events for %s: %s"
% (event_id, e)
)
) from e

# Update the set of things we've seen after trying to
# fetch the missing stuff
Expand Down
25 changes: 12 additions & 13 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import urllib.parse
from typing import Awaitable, Callable, Dict, List, Optional, Tuple

from twisted.internet.error import TimeoutError

from synapse.api.errors import (
AuthError,
CodeMessageException,
Expand All @@ -32,6 +30,7 @@
SynapseError,
)
from synapse.config.emailconfig import ThreepidBehaviour
from synapse.http import RequestTimedOutError
from synapse.http.client import SimpleHttpClient
from synapse.types import JsonDict, Requester
from synapse.util import json_decoder
Expand Down Expand Up @@ -107,7 +106,7 @@ async def threepid_from_creds(

try:
data = await self.http_client.get_json(url, query_params)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
logger.info(
Expand Down Expand Up @@ -192,7 +191,7 @@ async def bind_threepid(
if e.code != 404 or not use_v2:
logger.error("3PID bind failed with Matrix error: %r", e)
raise e.to_synapse_error()
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except CodeMessageException as e:
data = json_decoder.decode(e.msg) # XXX WAT?
Expand Down Expand Up @@ -299,7 +298,7 @@ async def try_unbind_threepid_with_id_server(
else:
logger.error("Failed to unbind threepid on identity server: %s", e)
raise SynapseError(500, "Failed to contact identity server")
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")

await self.store.remove_user_bound_threepid(
Expand Down Expand Up @@ -470,7 +469,7 @@ async def requestEmailToken(
except HttpResponseException as e:
logger.info("Proxied requestToken failed: %r", e)
raise e.to_synapse_error()
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")

async def requestMsisdnToken(
Expand Down Expand Up @@ -526,7 +525,7 @@ async def requestMsisdnToken(
except HttpResponseException as e:
logger.info("Proxied requestToken failed: %r", e)
raise e.to_synapse_error()
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")

assert self.hs.config.public_baseurl
Expand Down Expand Up @@ -608,7 +607,7 @@ async def proxy_msisdn_submit_token(
id_server + "/_matrix/identity/api/v1/validate/msisdn/submitToken",
body,
)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
logger.warning("Error contacting msisdn account_threepid_delegate: %s", e)
Expand Down Expand Up @@ -766,7 +765,7 @@ async def _lookup_3pid_v1(
# require or validate it. See the following for context:
# https://github.com/matrix-org/synapse/issues/5253#issuecomment-666246950
return data["mxid"]
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except IOError as e:
logger.warning("Error from v1 identity server lookup: %s" % (e,))
Expand All @@ -793,7 +792,7 @@ async def _lookup_3pid_v2(
"%s/_matrix/identity/v2/hash_details" % (id_server_url,),
{"access_token": id_access_token},
)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")

if not isinstance(hash_details, dict):
Expand Down Expand Up @@ -864,7 +863,7 @@ async def _lookup_3pid_v2(
},
headers=headers,
)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except Exception as e:
logger.warning("Error when performing a v2 3pid lookup: %s", e)
Expand Down Expand Up @@ -962,7 +961,7 @@ async def ask_id_server_for_third_party_invite(
invite_config,
{"Authorization": create_id_access_token_header(id_access_token)},
)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
if e.code != 404:
Expand All @@ -979,7 +978,7 @@ async def ask_id_server_for_third_party_invite(
data = await self.blacklisting_http_client.post_json_get_json(
url, invite_config
)
except TimeoutError:
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")
except HttpResponseException as e:
logger.warning(
Expand Down
17 changes: 2 additions & 15 deletions synapse/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import re

from twisted.internet import task
from twisted.internet.defer import CancelledError
from twisted.python import failure
from twisted.web.client import FileBodyProducer

from synapse.api.errors import SynapseError
Expand All @@ -26,19 +24,8 @@
class RequestTimedOutError(SynapseError):
"""Exception representing timeout of an outbound request"""

def __init__(self):
super().__init__(504, "Timed out")


def cancelled_to_request_timed_out_error(value, timeout):
"""Turns CancelledErrors into RequestTimedOutErrors.
For use with async.add_timeout_to_deferred
"""
if isinstance(value, failure.Failure):
value.trap(CancelledError)
raise RequestTimedOutError()
return value
def __init__(self, msg):
super().__init__(504, msg)


ACCESS_TOKEN_RE = re.compile(r"(\?.*access(_|%5[Ff])token=)[^&]*(.*)$")
Expand Down
54 changes: 33 additions & 21 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import urllib
from io import BytesIO
Expand All @@ -38,25 +37,26 @@

from OpenSSL import SSL
from OpenSSL.SSL import VERIFY_NONE
from twisted.internet import defer, protocol, ssl
from twisted.internet import defer, error as twisted_error, protocol, ssl
from twisted.internet.interfaces import (
IReactorPluggableNameResolver,
IResolutionReceiver,
)
from twisted.internet.task import Cooperator
from twisted.python.failure import Failure
from twisted.web._newclient import ResponseDone
from twisted.web.client import Agent, HTTPConnectionPool, readBody
from twisted.web.client import (
Agent,
HTTPConnectionPool,
ResponseNeverReceived,
readBody,
)
from twisted.web.http import PotentialDataLoss
from twisted.web.http_headers import Headers
from twisted.web.iweb import IResponse

from synapse.api.errors import Codes, HttpResponseException, SynapseError
from synapse.http import (
QuieterFileBodyProducer,
cancelled_to_request_timed_out_error,
redact_uri,
)
from synapse.http import QuieterFileBodyProducer, RequestTimedOutError, redact_uri
from synapse.http.proxyagent import ProxyAgent
from synapse.logging.context import make_deferred_yieldable
from synapse.logging.opentracing import set_tag, start_active_span, tags
Expand Down Expand Up @@ -332,8 +332,6 @@ async def request(
RequestTimedOutError if the request times out before the headers are read
"""
# A small wrapper around self.agent.request() so we can easily attach
# counters to it
outgoing_requests_counter.labels(method).inc()

# log request but strip `access_token` (AS requests for example include this)
Expand Down Expand Up @@ -362,15 +360,17 @@ async def request(
data=body_producer,
headers=headers,
**self._extra_treq_args
)
) # type: defer.Deferred

# we use our own timeout mechanism rather than treq's as a workaround
# for https://twistedmatrix.com/trac/ticket/9534.
request_deferred = timeout_deferred(
request_deferred,
60,
self.hs.get_reactor(),
cancelled_to_request_timed_out_error,
request_deferred, 60, self.hs.get_reactor(),
)

# turn timeouts into RequestTimedOutErrors
request_deferred.addErrback(_timeout_to_request_timed_out_error)

response = await make_deferred_yieldable(request_deferred)

incoming_responses_counter.labels(method, response.code).inc()
Expand Down Expand Up @@ -410,7 +410,7 @@ async def post_urlencoded_get_json(
parsed json
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -461,7 +461,7 @@ async def post_json_get_json(
parsed json
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -506,7 +506,7 @@ async def get_json(
Returns:
Succeeds when we get a 2xx HTTP response, with the HTTP body as JSON.
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -538,7 +538,7 @@ async def put_json(
Returns:
Succeeds when we get a 2xx HTTP response, with the HTTP body as JSON.
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -586,7 +586,7 @@ async def get_raw(
Succeeds when we get a 2xx HTTP response, with the
HTTP body as bytes.
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -631,7 +631,7 @@ async def get_file(
headers, absolute URI of the response and HTTP response code.
Raises:
RequestTimedOutException: if there is a timeout before the response headers
RequestTimedOutError: if there is a timeout before the response headers
are received. Note there is currently no timeout on reading the response
body.
Expand Down Expand Up @@ -684,6 +684,18 @@ async def get_file(
)


def _timeout_to_request_timed_out_error(f: Failure):
if f.check(twisted_error.TimeoutError, twisted_error.ConnectingCancelledError):
# The TCP connection has its own timeout (set by the 'connectTimeout' param
# on the Agent), which raises twisted_error.TimeoutError exception.
raise RequestTimedOutError("Timeout connecting to remote server")
elif f.check(defer.TimeoutError, ResponseNeverReceived):
# this one means that we hit our overall timeout on the request
raise RequestTimedOutError("Timeout waiting for response from remote server")

return f


# XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
# The two should be factored out.

Expand Down
Loading

0 comments on commit ab26b58

Please sign in to comment.