2017-08-15 10:57:46 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2019-2021 The Matrix.org Foundation C.I.C
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2017-08-15 10:57:46 -04:00
|
|
|
#
|
|
|
|
#
|
2021-09-01 06:55:31 -04:00
|
|
|
import atexit
|
2017-08-15 10:57:46 -04:00
|
|
|
import gc
|
|
|
|
import logging
|
2019-07-22 09:19:38 -04:00
|
|
|
import os
|
2019-02-08 12:25:57 -05:00
|
|
|
import signal
|
2019-08-17 04:09:52 -04:00
|
|
|
import socket
|
2017-10-02 13:03:59 -04:00
|
|
|
import sys
|
2019-02-08 12:25:57 -05:00
|
|
|
import traceback
|
2021-03-26 13:33:55 -04:00
|
|
|
import warnings
|
Print full startup/initialization error (#15569)
I found the error in the **Before** really vague and obtuse and didn't realize port `5432` corresponded to the Postgres port until searching the codebase. It says to check the logs but that wasn't my first instinct. It's just more obvious if we just print the full thing which gives context of the error type and the traceback to the relevant area of code.
#### Before
```
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
#### After
```sh
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
Traceback (most recent call last):
File "/home/eric/Documents/github/element/synapse/synapse/app/homeserver.py", line 352, in setup
hs.setup()
File "/home/eric/Documents/github/element/synapse/synapse/server.py", line 337, in setup
self.datastores = Databases(self.DATASTORE_CLASS, self)
File "/home/eric/Documents/github/element/synapse/synapse/storage/databases/__init__.py", line 65, in __init__
with make_conn(database_config, engine, "startup") as db_conn:
File "/home/eric/Documents/github/element/synapse/synapse/storage/database.py", line 161, in make_conn
native_db_conn = engine.module.connect(**db_params)
File "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
2023-05-11 12:50:46 -04:00
|
|
|
from textwrap import indent
|
2021-11-10 15:06:54 -05:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Any,
|
|
|
|
Awaitable,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
List,
|
|
|
|
NoReturn,
|
2021-12-01 07:28:23 -05:00
|
|
|
Optional,
|
2021-11-10 15:06:54 -05:00
|
|
|
Tuple,
|
|
|
|
cast,
|
|
|
|
)
|
2017-10-02 13:03:59 -04:00
|
|
|
|
2021-03-26 13:33:55 -04:00
|
|
|
from cryptography.utils import CryptographyDeprecationWarning
|
2022-05-09 06:27:39 -04:00
|
|
|
from typing_extensions import ParamSpec
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-06-21 06:41:25 -04:00
|
|
|
import twisted
|
2021-11-10 15:06:54 -05:00
|
|
|
from twisted.internet import defer, error, reactor as _reactor
|
2023-04-03 05:27:51 -04:00
|
|
|
from twisted.internet.interfaces import (
|
|
|
|
IOpenSSLContextFactory,
|
|
|
|
IReactorSSL,
|
|
|
|
IReactorTCP,
|
|
|
|
IReactorUNIX,
|
|
|
|
)
|
2021-11-10 15:06:54 -05:00
|
|
|
from twisted.internet.protocol import ServerFactory
|
|
|
|
from twisted.internet.tcp import Port
|
2021-06-21 06:41:25 -04:00
|
|
|
from twisted.logger import LoggingFile, LogLevel
|
2019-02-11 05:36:26 -05:00
|
|
|
from twisted.protocols.tls import TLSMemoryBIOFactory
|
2021-10-26 08:45:38 -04:00
|
|
|
from twisted.python.threadpool import ThreadPool
|
2022-11-15 07:55:00 -05:00
|
|
|
from twisted.web.resource import Resource
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2022-05-11 09:43:22 -04:00
|
|
|
import synapse.util.caches
|
2021-04-23 14:20:44 -04:00
|
|
|
from synapse.api.constants import MAX_PDU_SIZE
|
2019-01-30 09:17:55 -05:00
|
|
|
from synapse.app import check_bind_error
|
2020-10-02 08:23:15 -04:00
|
|
|
from synapse.app.phone_stats_home import start_phone_stats_home
|
2022-05-11 09:43:22 -04:00
|
|
|
from synapse.config import ConfigError
|
|
|
|
from synapse.config._base import format_config_error
|
2021-04-23 14:20:44 -04:00
|
|
|
from synapse.config.homeserver import HomeServerConfig
|
2023-04-03 05:27:51 -04:00
|
|
|
from synapse.config.server import ListenerConfig, ManholeConfig, TCPListenerConfig
|
2019-02-08 12:25:57 -05:00
|
|
|
from synapse.crypto import context_factory
|
2024-05-21 16:09:17 -04:00
|
|
|
from synapse.events.auto_accept_invites import InviteAutoAccepter
|
2021-08-17 09:22:45 -04:00
|
|
|
from synapse.events.presence_router import load_legacy_presence_router
|
2021-10-13 07:21:52 -04:00
|
|
|
from synapse.handlers.auth import load_legacy_password_auth_providers
|
2022-11-15 07:55:00 -05:00
|
|
|
from synapse.http.site import SynapseSite
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import PreserveLoggingContext
|
2022-04-27 09:03:44 -04:00
|
|
|
from synapse.logging.opentracing import init_tracer
|
2022-01-13 09:35:52 -05:00
|
|
|
from synapse.metrics import install_gc_manager, register_threadpool
|
2020-11-26 06:18:10 -05:00
|
|
|
from synapse.metrics.background_process_metrics import wrap_as_background_process
|
2021-05-06 10:54:07 -04:00
|
|
|
from synapse.metrics.jemalloc import setup_jemalloc_stats
|
2023-04-17 20:57:40 -04:00
|
|
|
from synapse.module_api.callbacks.spamchecker_callbacks import load_legacy_spam_checkers
|
2023-05-04 10:18:22 -04:00
|
|
|
from synapse.module_api.callbacks.third_party_event_rules_callbacks import (
|
|
|
|
load_legacy_third_party_event_rules,
|
|
|
|
)
|
2023-09-13 07:57:19 -04:00
|
|
|
from synapse.types import ISynapseReactor, StrCollection
|
2022-06-07 10:24:11 -04:00
|
|
|
from synapse.util import SYNAPSE_VERSION
|
2021-07-05 11:32:12 -04:00
|
|
|
from synapse.util.caches.lrucache import setup_expire_lru_cache_entries
|
2020-08-04 05:03:41 -04:00
|
|
|
from synapse.util.daemonize import daemonize_process
|
2021-10-26 08:45:38 -04:00
|
|
|
from synapse.util.gai_resolver import GAIResolver
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util.rlimit import change_resource_limit
|
|
|
|
|
2021-07-05 11:32:12 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
# Twisted injects the global reactor to make it easier to import, this confuses
|
|
|
|
# mypy which thinks it is a module. Tell it that it a more proper type.
|
|
|
|
reactor = cast(ISynapseReactor, _reactor)
|
|
|
|
|
|
|
|
|
2017-12-17 07:04:05 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-28 07:18:53 -04:00
|
|
|
# list of tuples of function, args list, kwargs dict
|
2021-11-10 15:06:54 -05:00
|
|
|
_sighup_callbacks: List[
|
2022-05-09 06:27:39 -04:00
|
|
|
Tuple[Callable[..., None], Tuple[object, ...], Dict[str, object]]
|
2021-11-10 15:06:54 -05:00
|
|
|
] = []
|
2022-05-09 06:27:39 -04:00
|
|
|
P = ParamSpec("P")
|
2019-02-08 12:25:57 -05:00
|
|
|
|
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
def register_sighup(func: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None:
|
2019-02-08 12:25:57 -05:00
|
|
|
"""
|
|
|
|
Register a function to be called when a SIGHUP occurs.
|
|
|
|
|
|
|
|
Args:
|
2021-11-10 15:06:54 -05:00
|
|
|
func: Function to be called when sent a SIGHUP signal.
|
2019-08-28 07:18:53 -04:00
|
|
|
*args, **kwargs: args and kwargs to be passed to the target function.
|
2019-02-08 12:25:57 -05:00
|
|
|
"""
|
2022-09-30 12:36:28 -04:00
|
|
|
_sighup_callbacks.append((func, args, kwargs))
|
2019-02-08 12:25:57 -05:00
|
|
|
|
2017-08-15 10:57:46 -04:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def start_worker_reactor(
|
|
|
|
appname: str,
|
|
|
|
config: HomeServerConfig,
|
2022-06-30 07:58:12 -04:00
|
|
|
# Use a lambda to avoid binding to a given reactor at import time.
|
|
|
|
# (needed when synapse.app.complement_fork_starter is being used)
|
|
|
|
run_command: Callable[[], None] = lambda: reactor.run(),
|
2021-11-10 15:06:54 -05:00
|
|
|
) -> None:
|
2017-08-15 10:57:46 -04:00
|
|
|
"""Run the reactor in the main process
|
|
|
|
|
|
|
|
Daemonizes if necessary, and then configures some resources, before starting
|
|
|
|
the reactor. Pulls configuration from the 'worker' settings in 'config'.
|
|
|
|
|
|
|
|
Args:
|
2021-11-10 15:06:54 -05:00
|
|
|
appname: application name which will be sent to syslog
|
|
|
|
config: config object
|
|
|
|
run_command: callable that actually runs the reactor
|
2017-08-15 10:57:46 -04:00
|
|
|
"""
|
|
|
|
|
2021-09-13 13:07:12 -04:00
|
|
|
logger = logging.getLogger(config.worker.worker_app)
|
2017-08-15 10:57:46 -04:00
|
|
|
|
|
|
|
start_reactor(
|
|
|
|
appname,
|
2021-09-29 06:44:15 -04:00
|
|
|
soft_file_limit=config.server.soft_file_limit,
|
|
|
|
gc_thresholds=config.server.gc_thresholds,
|
2021-09-24 07:25:21 -04:00
|
|
|
pid_file=config.worker.worker_pid_file,
|
|
|
|
daemonize=config.worker.worker_daemonize,
|
2021-09-29 06:44:15 -04:00
|
|
|
print_pidfile=config.server.print_pidfile,
|
2019-03-14 09:32:14 -04:00
|
|
|
logger=logger,
|
2019-07-01 12:55:26 -04:00
|
|
|
run_command=run_command,
|
2017-08-15 10:57:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def start_reactor(
|
2021-11-10 15:06:54 -05:00
|
|
|
appname: str,
|
|
|
|
soft_file_limit: int,
|
2021-12-01 07:28:23 -05:00
|
|
|
gc_thresholds: Optional[Tuple[int, int, int]],
|
2022-04-11 12:07:23 -04:00
|
|
|
pid_file: Optional[str],
|
2021-11-10 15:06:54 -05:00
|
|
|
daemonize: bool,
|
|
|
|
print_pidfile: bool,
|
|
|
|
logger: logging.Logger,
|
2022-06-30 07:58:12 -04:00
|
|
|
# Use a lambda to avoid binding to a given reactor at import time.
|
|
|
|
# (needed when synapse.app.complement_fork_starter is being used)
|
|
|
|
run_command: Callable[[], None] = lambda: reactor.run(),
|
2021-11-10 15:06:54 -05:00
|
|
|
) -> None:
|
2017-08-15 10:57:46 -04:00
|
|
|
"""Run the reactor in the main process
|
|
|
|
|
|
|
|
Daemonizes if necessary, and then configures some resources, before starting
|
|
|
|
the reactor
|
|
|
|
|
|
|
|
Args:
|
2021-11-10 15:06:54 -05:00
|
|
|
appname: application name which will be sent to syslog
|
|
|
|
soft_file_limit:
|
2017-08-15 10:57:46 -04:00
|
|
|
gc_thresholds:
|
2021-11-10 15:06:54 -05:00
|
|
|
pid_file: name of pid file to write to if daemonize is True
|
|
|
|
daemonize: true to run the reactor in a background process
|
|
|
|
print_pidfile: whether to print the pid file, if daemonize is True
|
|
|
|
logger: logger instance to pass to Daemonize
|
|
|
|
run_command: callable that actually runs the reactor
|
2017-08-15 10:57:46 -04:00
|
|
|
"""
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def run() -> None:
|
2019-07-03 08:40:45 -04:00
|
|
|
logger.info("Running")
|
2021-05-06 10:54:07 -04:00
|
|
|
setup_jemalloc_stats()
|
2019-07-03 08:40:45 -04:00
|
|
|
change_resource_limit(soft_file_limit)
|
|
|
|
if gc_thresholds:
|
|
|
|
gc.set_threshold(*gc_thresholds)
|
2022-01-13 09:35:52 -05:00
|
|
|
install_gc_manager()
|
2019-07-15 09:13:22 -04:00
|
|
|
run_command()
|
2019-07-03 08:40:45 -04:00
|
|
|
|
|
|
|
# make sure that we run the reactor with the sentinel log context,
|
|
|
|
# otherwise other PreserveLoggingContext instances will get confused
|
|
|
|
# and complain when they see the logcontext arbitrarily swapping
|
|
|
|
# between the sentinel and `run` logcontexts.
|
|
|
|
#
|
|
|
|
# We also need to drop the logcontext before forking if we're daemonizing,
|
|
|
|
# otherwise the cputime metrics get confused about the per-thread resource usage
|
|
|
|
# appearing to go backwards.
|
|
|
|
with PreserveLoggingContext():
|
|
|
|
if daemonize:
|
2022-04-11 12:07:23 -04:00
|
|
|
assert pid_file is not None
|
|
|
|
|
2019-07-03 08:40:45 -04:00
|
|
|
if print_pidfile:
|
|
|
|
print(pid_file)
|
|
|
|
|
2020-08-04 05:03:41 -04:00
|
|
|
daemonize_process(pid_file, logger)
|
|
|
|
run()
|
2017-10-02 12:59:34 -04:00
|
|
|
|
|
|
|
|
2020-05-12 06:20:48 -04:00
|
|
|
def quit_with_error(error_string: str) -> NoReturn:
|
2017-10-02 12:59:34 -04:00
|
|
|
message_lines = error_string.split("\n")
|
2021-06-21 06:41:25 -04:00
|
|
|
line_length = min(max(len(line) for line in message_lines), 80) + 2
|
2017-10-02 12:59:34 -04:00
|
|
|
sys.stderr.write("*" * line_length + "\n")
|
|
|
|
for line in message_lines:
|
|
|
|
sys.stderr.write(" %s\n" % (line.rstrip(),))
|
|
|
|
sys.stderr.write("*" * line_length + "\n")
|
|
|
|
sys.exit(1)
|
2017-09-06 11:48:49 -04:00
|
|
|
|
|
|
|
|
2021-06-21 06:41:25 -04:00
|
|
|
def handle_startup_exception(e: Exception) -> NoReturn:
|
|
|
|
# Exceptions that occur between setting up the logging and forking or starting
|
|
|
|
# the reactor are written to the logs, followed by a summary to stderr.
|
|
|
|
logger.exception("Exception during startup")
|
Print full startup/initialization error (#15569)
I found the error in the **Before** really vague and obtuse and didn't realize port `5432` corresponded to the Postgres port until searching the codebase. It says to check the logs but that wasn't my first instinct. It's just more obvious if we just print the full thing which gives context of the error type and the traceback to the relevant area of code.
#### Before
```
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
#### After
```sh
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
Traceback (most recent call last):
File "/home/eric/Documents/github/element/synapse/synapse/app/homeserver.py", line 352, in setup
hs.setup()
File "/home/eric/Documents/github/element/synapse/synapse/server.py", line 337, in setup
self.datastores = Databases(self.DATASTORE_CLASS, self)
File "/home/eric/Documents/github/element/synapse/synapse/storage/databases/__init__.py", line 65, in __init__
with make_conn(database_config, engine, "startup") as db_conn:
File "/home/eric/Documents/github/element/synapse/synapse/storage/database.py", line 161, in make_conn
native_db_conn = engine.module.connect(**db_params)
File "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
2023-05-11 12:50:46 -04:00
|
|
|
|
2023-05-16 09:56:42 -04:00
|
|
|
error_string = "".join(traceback.format_exception(type(e), e, e.__traceback__))
|
Print full startup/initialization error (#15569)
I found the error in the **Before** really vague and obtuse and didn't realize port `5432` corresponded to the Postgres port until searching the codebase. It says to check the logs but that wasn't my first instinct. It's just more obvious if we just print the full thing which gives context of the error type and the traceback to the relevant area of code.
#### Before
```
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
#### After
```sh
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
Traceback (most recent call last):
File "/home/eric/Documents/github/element/synapse/synapse/app/homeserver.py", line 352, in setup
hs.setup()
File "/home/eric/Documents/github/element/synapse/synapse/server.py", line 337, in setup
self.datastores = Databases(self.DATASTORE_CLASS, self)
File "/home/eric/Documents/github/element/synapse/synapse/storage/databases/__init__.py", line 65, in __init__
with make_conn(database_config, engine, "startup") as db_conn:
File "/home/eric/Documents/github/element/synapse/synapse/storage/database.py", line 161, in make_conn
native_db_conn = engine.module.connect(**db_params)
File "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
2023-05-11 12:50:46 -04:00
|
|
|
indented_error_string = indent(error_string, " ")
|
|
|
|
|
2021-06-21 06:41:25 -04:00
|
|
|
quit_with_error(
|
Print full startup/initialization error (#15569)
I found the error in the **Before** really vague and obtuse and didn't realize port `5432` corresponded to the Postgres port until searching the codebase. It says to check the logs but that wasn't my first instinct. It's just more obvious if we just print the full thing which gives context of the error type and the traceback to the relevant area of code.
#### Before
```
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
#### After
```sh
$ poetry run python -m synapse.app.homeserver -c homeserver.yaml
**********************************************************************************
Error during initialisation:
Traceback (most recent call last):
File "/home/eric/Documents/github/element/synapse/synapse/app/homeserver.py", line 352, in setup
hs.setup()
File "/home/eric/Documents/github/element/synapse/synapse/server.py", line 337, in setup
self.datastores = Databases(self.DATASTORE_CLASS, self)
File "/home/eric/Documents/github/element/synapse/synapse/storage/databases/__init__.py", line 65, in __init__
with make_conn(database_config, engine, "startup") as db_conn:
File "/home/eric/Documents/github/element/synapse/synapse/storage/database.py", line 161, in make_conn
native_db_conn = engine.module.connect(**db_params)
File "/home/eric/.cache/pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.10/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
There may be more information in the logs.
**********************************************************************************
```
2023-05-11 12:50:46 -04:00
|
|
|
f"Error during initialisation:\n{indented_error_string}\nThere may be more information in the logs."
|
2021-06-21 06:41:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_stdio_to_logs() -> None:
|
|
|
|
streams = [("stdout", LogLevel.info), ("stderr", LogLevel.error)]
|
|
|
|
|
|
|
|
for stream, level in streams:
|
|
|
|
oldStream = getattr(sys, stream)
|
|
|
|
loggingFile = LoggingFile(
|
|
|
|
logger=twisted.logger.Logger(namespace=stream),
|
|
|
|
level=level,
|
|
|
|
encoding=getattr(oldStream, "encoding", None),
|
|
|
|
)
|
|
|
|
setattr(sys, stream, loggingFile)
|
|
|
|
|
|
|
|
print("Redirected stdout/stderr to logs")
|
|
|
|
|
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
def register_start(
|
|
|
|
cb: Callable[P, Awaitable], *args: P.args, **kwargs: P.kwargs
|
|
|
|
) -> None:
|
2021-01-11 10:55:05 -05:00
|
|
|
"""Register a callback with the reactor, to be called once it is running
|
|
|
|
|
|
|
|
This can be used to initialise parts of the system which require an asynchronous
|
|
|
|
setup.
|
|
|
|
|
|
|
|
Any exception raised by the callback will be printed and logged, and the process
|
|
|
|
will exit.
|
|
|
|
"""
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
async def wrapper() -> None:
|
2021-01-11 10:55:05 -05:00
|
|
|
try:
|
|
|
|
await cb(*args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
# previously, we used Failure().printTraceback() here, in the hope that
|
|
|
|
# would give better tracebacks than traceback.print_exc(). However, that
|
|
|
|
# doesn't handle chained exceptions (with a __cause__ or __context__) well,
|
|
|
|
# and I *think* the need for Failure() is reduced now that we mostly use
|
|
|
|
# async/await.
|
|
|
|
|
|
|
|
# Write the exception to both the logs *and* the unredirected stderr,
|
|
|
|
# because people tend to get confused if it only goes to one or the other.
|
|
|
|
#
|
|
|
|
# One problem with this is that if people are using a logging config that
|
|
|
|
# logs to the console (as is common eg under docker), they will get two
|
|
|
|
# copies of the exception. We could maybe try to detect that, but it's
|
|
|
|
# probably a cost we can bear.
|
|
|
|
logger.fatal("Error during startup", exc_info=True)
|
|
|
|
print("Error during startup:", file=sys.__stderr__)
|
|
|
|
traceback.print_exc(file=sys.__stderr__)
|
|
|
|
|
|
|
|
# it's no use calling sys.exit here, since that just raises a SystemExit
|
|
|
|
# exception which is then caught by the reactor, and everything carries
|
|
|
|
# on as normal.
|
|
|
|
os._exit(1)
|
|
|
|
|
|
|
|
reactor.callWhenRunning(lambda: defer.ensureDeferred(wrapper()))
|
|
|
|
|
|
|
|
|
2023-09-13 07:57:19 -04:00
|
|
|
def listen_metrics(bind_addresses: StrCollection, port: int) -> None:
|
2018-05-31 05:04:50 -04:00
|
|
|
"""
|
|
|
|
Start Prometheus metrics server.
|
|
|
|
"""
|
2022-08-24 07:35:54 -04:00
|
|
|
from prometheus_client import start_http_server as start_http_server_prometheus
|
|
|
|
|
2022-11-24 04:09:17 -05:00
|
|
|
from synapse.metrics import RegistryProxy
|
2018-05-31 05:04:50 -04:00
|
|
|
|
|
|
|
for host in bind_addresses:
|
2019-02-13 06:48:56 -05:00
|
|
|
logger.info("Starting metrics listener on %s:%d", host, port)
|
2022-11-24 04:09:17 -05:00
|
|
|
_set_prometheus_client_use_created_metrics(False)
|
|
|
|
start_http_server_prometheus(port, addr=host, registry=RegistryProxy)
|
2022-08-24 07:35:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _set_prometheus_client_use_created_metrics(new_value: bool) -> None:
|
|
|
|
"""
|
|
|
|
Sets whether prometheus_client should expose `_created`-suffixed metrics for
|
|
|
|
all gauges, histograms and summaries.
|
|
|
|
There is no programmatic way to disable this without poking at internals;
|
|
|
|
the proper way is to use an environment variable which prometheus_client
|
|
|
|
loads at import time.
|
|
|
|
|
|
|
|
The motivation for disabling these `_created` metrics is that they're
|
|
|
|
a waste of space as they're not useful but they take up space in Prometheus.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import prometheus_client.metrics
|
|
|
|
|
|
|
|
if hasattr(prometheus_client.metrics, "_use_created"):
|
|
|
|
prometheus_client.metrics._use_created = new_value
|
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
"Can't disable `_created` metrics in prometheus_client (brittle hack broken?)"
|
|
|
|
)
|
2018-05-31 05:04:50 -04:00
|
|
|
|
|
|
|
|
2021-09-06 11:08:03 -04:00
|
|
|
def listen_manhole(
|
2023-09-13 07:57:19 -04:00
|
|
|
bind_addresses: StrCollection,
|
2021-09-06 11:08:03 -04:00
|
|
|
port: int,
|
|
|
|
manhole_settings: ManholeConfig,
|
|
|
|
manhole_globals: dict,
|
2021-11-10 15:06:54 -05:00
|
|
|
) -> None:
|
2021-03-26 13:33:55 -04:00
|
|
|
# twisted.conch.manhole 21.1.0 uses "int_from_bytes", which produces a confusing
|
|
|
|
# warning. It's fixed by https://github.com/twisted/twisted/pull/1522), so
|
|
|
|
# suppress the warning for now.
|
|
|
|
warnings.filterwarnings(
|
|
|
|
action="ignore",
|
|
|
|
category=CryptographyDeprecationWarning,
|
|
|
|
message="int_from_bytes is deprecated",
|
|
|
|
)
|
|
|
|
|
|
|
|
from synapse.util.manhole import manhole
|
|
|
|
|
|
|
|
listen_tcp(
|
|
|
|
bind_addresses,
|
|
|
|
port,
|
2021-09-06 11:08:03 -04:00
|
|
|
manhole(settings=manhole_settings, globals=manhole_globals),
|
2021-03-26 13:33:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def listen_tcp(
|
2023-09-13 07:57:19 -04:00
|
|
|
bind_addresses: StrCollection,
|
2021-11-10 15:06:54 -05:00
|
|
|
port: int,
|
|
|
|
factory: ServerFactory,
|
|
|
|
reactor: IReactorTCP = reactor,
|
|
|
|
backlog: int = 50,
|
|
|
|
) -> List[Port]:
|
2017-09-06 11:48:49 -04:00
|
|
|
"""
|
|
|
|
Create a TCP socket for a port and several addresses
|
2019-01-30 06:00:02 -05:00
|
|
|
|
|
|
|
Returns:
|
2021-11-10 15:06:54 -05:00
|
|
|
list of twisted.internet.tcp.Port listening for TCP connections
|
2017-09-06 11:48:49 -04:00
|
|
|
"""
|
2019-02-13 06:53:43 -05:00
|
|
|
r = []
|
2017-09-06 11:48:49 -04:00
|
|
|
for address in bind_addresses:
|
|
|
|
try:
|
2019-02-13 06:53:43 -05:00
|
|
|
r.append(reactor.listenTCP(port, factory, backlog, address))
|
2017-09-06 11:48:49 -04:00
|
|
|
except error.CannotListenError as e:
|
2017-12-17 07:04:05 -05:00
|
|
|
check_bind_error(e, address, bind_addresses)
|
2017-09-06 11:48:49 -04:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
# IReactorTCP returns an object implementing IListeningPort from listenTCP,
|
|
|
|
# but we know it will be a Port instance.
|
|
|
|
return r # type: ignore[return-value]
|
2019-01-30 06:00:02 -05:00
|
|
|
|
2017-09-06 11:48:49 -04:00
|
|
|
|
2023-04-03 05:27:51 -04:00
|
|
|
def listen_unix(
|
|
|
|
path: str,
|
|
|
|
mode: int,
|
|
|
|
factory: ServerFactory,
|
|
|
|
reactor: IReactorUNIX = reactor,
|
|
|
|
backlog: int = 50,
|
|
|
|
) -> List[Port]:
|
|
|
|
"""
|
|
|
|
Create a UNIX socket for a given path and 'mode' permission
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list of twisted.internet.tcp.Port listening for TCP connections
|
|
|
|
"""
|
|
|
|
wantPID = True
|
|
|
|
|
|
|
|
return [
|
|
|
|
# IReactorUNIX returns an object implementing IListeningPort from listenUNIX,
|
|
|
|
# but we know it will be a Port instance.
|
|
|
|
cast(Port, reactor.listenUNIX(path, factory, backlog, mode, wantPID))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-11-15 07:55:00 -05:00
|
|
|
def listen_http(
|
2023-07-18 04:49:21 -04:00
|
|
|
hs: "HomeServer",
|
2022-11-15 07:55:00 -05:00
|
|
|
listener_config: ListenerConfig,
|
|
|
|
root_resource: Resource,
|
|
|
|
version_string: str,
|
|
|
|
max_request_body_size: int,
|
2022-11-15 11:36:43 -05:00
|
|
|
context_factory: Optional[IOpenSSLContextFactory],
|
|
|
|
reactor: ISynapseReactor = reactor,
|
2022-11-15 07:55:00 -05:00
|
|
|
) -> List[Port]:
|
|
|
|
assert listener_config.http_options is not None
|
|
|
|
|
2023-04-03 05:27:51 -04:00
|
|
|
site_tag = listener_config.get_site_tag()
|
2022-11-15 07:55:00 -05:00
|
|
|
|
|
|
|
site = SynapseSite(
|
2023-04-03 05:27:51 -04:00
|
|
|
"synapse.access.%s.%s"
|
|
|
|
% ("https" if listener_config.is_tls() else "http", site_tag),
|
2022-11-15 07:55:00 -05:00
|
|
|
site_tag,
|
|
|
|
listener_config,
|
|
|
|
root_resource,
|
|
|
|
version_string,
|
|
|
|
max_request_body_size=max_request_body_size,
|
|
|
|
reactor=reactor,
|
2023-07-18 04:49:21 -04:00
|
|
|
hs=hs,
|
2022-11-15 07:55:00 -05:00
|
|
|
)
|
2023-04-03 05:27:51 -04:00
|
|
|
|
|
|
|
if isinstance(listener_config, TCPListenerConfig):
|
|
|
|
if listener_config.is_tls():
|
|
|
|
# refresh_certificate should have been called before this.
|
|
|
|
assert context_factory is not None
|
|
|
|
ports = listen_ssl(
|
|
|
|
listener_config.bind_addresses,
|
|
|
|
listener_config.port,
|
|
|
|
site,
|
|
|
|
context_factory,
|
|
|
|
reactor=reactor,
|
|
|
|
)
|
|
|
|
logger.info(
|
|
|
|
"Synapse now listening on TCP port %d (TLS)", listener_config.port
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
ports = listen_tcp(
|
|
|
|
listener_config.bind_addresses,
|
|
|
|
listener_config.port,
|
|
|
|
site,
|
|
|
|
reactor=reactor,
|
|
|
|
)
|
|
|
|
logger.info("Synapse now listening on TCP port %d", listener_config.port)
|
|
|
|
|
2022-11-15 07:55:00 -05:00
|
|
|
else:
|
2023-04-03 05:27:51 -04:00
|
|
|
ports = listen_unix(
|
|
|
|
listener_config.path, listener_config.mode, site, reactor=reactor
|
2022-11-15 07:55:00 -05:00
|
|
|
)
|
2023-04-03 05:27:51 -04:00
|
|
|
# getHost() returns a UNIXAddress which contains an instance variable of 'name'
|
|
|
|
# encoded as a byte string. Decode as utf-8 so pretty.
|
|
|
|
logger.info(
|
|
|
|
"Synapse now listening on Unix Socket at: "
|
|
|
|
f"{ports[0].getHost().name.decode('utf-8')}"
|
|
|
|
)
|
|
|
|
|
2022-11-15 07:55:00 -05:00
|
|
|
return ports
|
|
|
|
|
|
|
|
|
2018-08-17 11:08:45 -04:00
|
|
|
def listen_ssl(
|
2023-09-13 07:57:19 -04:00
|
|
|
bind_addresses: StrCollection,
|
2021-11-10 15:06:54 -05:00
|
|
|
port: int,
|
|
|
|
factory: ServerFactory,
|
|
|
|
context_factory: IOpenSSLContextFactory,
|
|
|
|
reactor: IReactorSSL = reactor,
|
|
|
|
backlog: int = 50,
|
|
|
|
) -> List[Port]:
|
2017-09-06 11:48:49 -04:00
|
|
|
"""
|
2019-01-30 06:00:02 -05:00
|
|
|
Create an TLS-over-TCP socket for a port and several addresses
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list of twisted.internet.tcp.Port listening for TLS connections
|
2017-09-06 11:48:49 -04:00
|
|
|
"""
|
2019-01-30 06:00:02 -05:00
|
|
|
r = []
|
2017-09-06 11:48:49 -04:00
|
|
|
for address in bind_addresses:
|
|
|
|
try:
|
2019-01-30 06:00:02 -05:00
|
|
|
r.append(
|
|
|
|
reactor.listenSSL(port, factory, context_factory, backlog, address)
|
2017-09-06 11:48:49 -04:00
|
|
|
)
|
|
|
|
except error.CannotListenError as e:
|
2017-12-17 07:04:05 -05:00
|
|
|
check_bind_error(e, address, bind_addresses)
|
2017-09-06 11:48:49 -04:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
# IReactorSSL incorrectly declares that an int is returned from listenSSL,
|
|
|
|
# it actually returns an object implementing IListeningPort, but we know it
|
|
|
|
# will be a Port instance.
|
|
|
|
return r # type: ignore[return-value]
|
2019-02-08 12:25:57 -05:00
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def refresh_certificate(hs: "HomeServer") -> None:
|
2019-02-08 12:25:57 -05:00
|
|
|
"""
|
|
|
|
Refresh the TLS certificates that Synapse is using by re-reading them from
|
|
|
|
disk and updating the TLS context factories to use them.
|
|
|
|
"""
|
2021-09-29 06:44:15 -04:00
|
|
|
if not hs.config.server.has_tls_listener():
|
2019-02-11 16:30:59 -05:00
|
|
|
return
|
|
|
|
|
2021-10-06 10:47:41 -04:00
|
|
|
hs.config.tls.read_certificate_from_disk()
|
2019-02-08 12:25:57 -05:00
|
|
|
hs.tls_server_context_factory = context_factory.ServerContextFactory(hs.config)
|
|
|
|
|
2019-02-11 05:36:26 -05:00
|
|
|
if hs._listening_services:
|
2019-02-11 16:00:41 -05:00
|
|
|
logger.info("Updating context factories...")
|
2019-02-11 05:36:26 -05:00
|
|
|
for i in hs._listening_services:
|
|
|
|
# When you listenSSL, it doesn't make an SSL port but a TCP one with
|
|
|
|
# a TLS wrapping factory around the factory you actually want to get
|
|
|
|
# requests. This factory attribute is public but missing from
|
|
|
|
# Twisted's documentation.
|
|
|
|
if isinstance(i.factory, TLSMemoryBIOFactory):
|
2019-02-13 06:53:43 -05:00
|
|
|
addr = i.getHost()
|
|
|
|
logger.info(
|
|
|
|
"Replacing TLS context factory on [%s]:%i", addr.host, addr.port
|
|
|
|
)
|
2019-02-11 05:36:26 -05:00
|
|
|
# We want to replace TLS factories with a new one, with the new
|
|
|
|
# TLS configuration. We do this by reaching in and pulling out
|
|
|
|
# the wrappedFactory, and then re-wrapping it.
|
|
|
|
i.factory = TLSMemoryBIOFactory(
|
|
|
|
hs.tls_server_context_factory, False, i.factory.wrappedFactory
|
|
|
|
)
|
2019-02-11 16:00:41 -05:00
|
|
|
logger.info("Context factories updated.")
|
2019-02-11 05:36:26 -05:00
|
|
|
|
2019-02-08 12:25:57 -05:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
async def start(hs: "HomeServer") -> None:
|
2019-02-08 12:25:57 -05:00
|
|
|
"""
|
|
|
|
Start a Synapse server or worker.
|
|
|
|
|
2021-06-17 13:56:48 -04:00
|
|
|
Should be called once the reactor is running.
|
2019-12-19 09:52:52 -05:00
|
|
|
|
|
|
|
Will start the main HTTP listeners and do some other startup tasks, and then
|
|
|
|
notify systemd.
|
|
|
|
|
2019-02-08 12:25:57 -05:00
|
|
|
Args:
|
2020-06-16 07:44:07 -04:00
|
|
|
hs: homeserver instance
|
2019-02-08 12:25:57 -05:00
|
|
|
"""
|
2021-10-26 08:45:38 -04:00
|
|
|
reactor = hs.get_reactor()
|
|
|
|
|
|
|
|
# We want to use a separate thread pool for the resolver so that large
|
|
|
|
# numbers of DNS requests don't starve out other users of the threadpool.
|
|
|
|
resolver_threadpool = ThreadPool(name="gai_resolver")
|
|
|
|
resolver_threadpool.start()
|
2021-10-27 08:04:56 -04:00
|
|
|
reactor.addSystemEventTrigger("during", "shutdown", resolver_threadpool.stop)
|
2021-10-26 08:45:38 -04:00
|
|
|
reactor.installNameResolver(
|
|
|
|
GAIResolver(reactor, getThreadPool=lambda: resolver_threadpool)
|
|
|
|
)
|
|
|
|
|
2021-11-01 07:21:36 -04:00
|
|
|
# Register the threadpools with our metrics.
|
|
|
|
register_threadpool("default", reactor.getThreadPool())
|
|
|
|
register_threadpool("gai_resolver", resolver_threadpool)
|
|
|
|
|
2021-01-11 10:55:05 -05:00
|
|
|
# Set up the SIGHUP machinery.
|
|
|
|
if hasattr(signal, "SIGHUP"):
|
|
|
|
|
|
|
|
@wrap_as_background_process("sighup")
|
2021-11-17 14:07:02 -05:00
|
|
|
async def handle_sighup(*args: Any, **kwargs: Any) -> None:
|
2021-01-11 10:55:05 -05:00
|
|
|
# Tell systemd our state, if we're using it. This will silently fail if
|
|
|
|
# we're not using systemd.
|
|
|
|
sdnotify(b"RELOADING=1")
|
|
|
|
|
|
|
|
for i, args, kwargs in _sighup_callbacks:
|
|
|
|
i(*args, **kwargs)
|
|
|
|
|
|
|
|
sdnotify(b"READY=1")
|
|
|
|
|
|
|
|
# We defer running the sighup handlers until next reactor tick. This
|
|
|
|
# is so that we're in a sane state, e.g. flushing the logs may fail
|
|
|
|
# if the sighup happens in the middle of writing a log entry.
|
2021-11-10 15:06:54 -05:00
|
|
|
def run_sighup(*args: Any, **kwargs: Any) -> None:
|
2021-01-11 10:55:05 -05:00
|
|
|
# `callFromThread` should be "signal safe" as well as thread
|
|
|
|
# safe.
|
|
|
|
reactor.callFromThread(handle_sighup, *args, **kwargs)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGHUP, run_sighup)
|
|
|
|
|
|
|
|
register_sighup(refresh_certificate, hs)
|
2022-05-11 09:43:22 -04:00
|
|
|
register_sighup(reload_cache_config, hs.config)
|
|
|
|
|
|
|
|
# Apply the cache config.
|
|
|
|
hs.config.caches.resize_all_caches()
|
2021-01-11 10:55:05 -05:00
|
|
|
|
|
|
|
# Load the certificate from disk.
|
|
|
|
refresh_certificate(hs)
|
|
|
|
|
|
|
|
# Start the tracer
|
2022-04-27 09:03:44 -04:00
|
|
|
init_tracer(hs) # noqa
|
2021-01-11 10:55:05 -05:00
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
# Instantiate the modules so they can register their web resources to the module API
|
|
|
|
# before we start the listeners.
|
|
|
|
module_api = hs.get_module_api()
|
|
|
|
for module, config in hs.config.modules.loaded_modules:
|
2022-06-15 09:36:16 -04:00
|
|
|
m = module(config, module_api)
|
2022-01-25 09:35:35 -05:00
|
|
|
logger.info("Loaded module %s", m)
|
2021-06-18 07:15:52 -04:00
|
|
|
|
2024-05-21 16:09:17 -04:00
|
|
|
if hs.config.auto_accept_invites.enabled:
|
|
|
|
# Start the local auto_accept_invites module.
|
|
|
|
m = InviteAutoAccepter(hs.config.auto_accept_invites, module_api)
|
|
|
|
logger.info("Loaded local module %s", m)
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
load_legacy_spam_checkers(hs)
|
2021-07-20 06:39:46 -04:00
|
|
|
load_legacy_third_party_event_rules(hs)
|
2021-08-17 09:22:45 -04:00
|
|
|
load_legacy_presence_router(hs)
|
2021-10-13 07:21:52 -04:00
|
|
|
load_legacy_password_auth_providers(hs)
|
2021-06-18 07:15:52 -04:00
|
|
|
|
2021-07-05 11:32:12 -04:00
|
|
|
# If we've configured an expiry time for caches, start the background job now.
|
|
|
|
setup_expire_lru_cache_entries(hs)
|
|
|
|
|
2021-01-11 10:55:05 -05:00
|
|
|
# It is now safe to start your Synapse.
|
2021-04-23 14:20:44 -04:00
|
|
|
hs.start_listening()
|
2022-02-23 06:04:02 -05:00
|
|
|
hs.get_datastores().main.db_pool.start_profiling()
|
2021-01-11 10:55:05 -05:00
|
|
|
hs.get_pusherpool().start()
|
|
|
|
|
|
|
|
# Log when we start the shut down process.
|
|
|
|
hs.get_reactor().addSystemEventTrigger(
|
|
|
|
"before", "shutdown", logger.info, "Shutting down..."
|
|
|
|
)
|
|
|
|
|
|
|
|
setup_sentry(hs)
|
|
|
|
setup_sdnotify(hs)
|
|
|
|
|
2022-09-05 06:26:43 -04:00
|
|
|
# If background tasks are running on the main process or this is the worker in
|
|
|
|
# charge of them, start collecting the phone home stats and shared usage metrics.
|
2021-09-13 13:07:12 -04:00
|
|
|
if hs.config.worker.run_background_tasks:
|
2022-09-05 06:26:43 -04:00
|
|
|
await hs.get_common_usage_metrics_manager().setup()
|
2021-01-11 10:55:05 -05:00
|
|
|
start_phone_stats_home(hs)
|
|
|
|
|
|
|
|
# We now freeze all allocated objects in the hopes that (almost)
|
|
|
|
# everything currently allocated are things that will be used for the
|
|
|
|
# rest of time. Doing so means less work each GC (hopefully).
|
|
|
|
#
|
2022-01-25 13:35:18 -05:00
|
|
|
# PyPy does not (yet?) implement gc.freeze()
|
|
|
|
if hasattr(gc, "freeze"):
|
|
|
|
gc.collect()
|
|
|
|
gc.freeze()
|
|
|
|
|
|
|
|
# Speed up shutdowns by freezing all allocated objects. This moves everything
|
|
|
|
# into the permanent generation and excludes them from the final GC.
|
|
|
|
atexit.register(gc.freeze)
|
2021-09-01 06:55:31 -04:00
|
|
|
|
2019-02-12 08:55:58 -05:00
|
|
|
|
2022-05-11 09:43:22 -04:00
|
|
|
def reload_cache_config(config: HomeServerConfig) -> None:
|
|
|
|
"""Reload cache config from disk and immediately apply it.resize caches accordingly.
|
|
|
|
|
|
|
|
If the config is invalid, a `ConfigError` is logged and no changes are made.
|
|
|
|
|
|
|
|
Otherwise, this:
|
|
|
|
- replaces the `caches` section on the given `config` object,
|
|
|
|
- resizes all caches according to the new cache factors, and
|
|
|
|
|
|
|
|
Note that the following cache config keys are read, but not applied:
|
|
|
|
- event_cache_size: used to set a max_size and _original_max_size on
|
|
|
|
EventsWorkerStore._get_event_cache when it is created. We'd have to update
|
|
|
|
the _original_max_size (and maybe
|
|
|
|
- sync_response_cache_duration: would have to update the timeout_sec attribute on
|
|
|
|
HomeServer -> SyncHandler -> ResponseCache.
|
|
|
|
- track_memory_usage. This affects synapse.util.caches.TRACK_MEMORY_USAGE which
|
|
|
|
influences Synapse's self-reported metrics.
|
|
|
|
|
|
|
|
Also, the HTTPConnectionPool in SimpleHTTPClient sets its maxPersistentPerHost
|
|
|
|
parameter based on the global_factor. This won't be applied on a config reload.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
previous_cache_config = config.reload_config_section("caches")
|
|
|
|
except ConfigError as e:
|
|
|
|
logger.warning("Failed to reload cache config")
|
|
|
|
for f in format_config_error(e):
|
|
|
|
logger.warning(f)
|
|
|
|
else:
|
|
|
|
logger.debug(
|
2022-10-21 11:03:44 -04:00
|
|
|
"New cache config. Was:\n %s\nNow:\n %s",
|
2022-05-11 09:43:22 -04:00
|
|
|
previous_cache_config.__dict__,
|
|
|
|
config.caches.__dict__,
|
|
|
|
)
|
|
|
|
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage
|
|
|
|
config.caches.resize_all_caches()
|
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def setup_sentry(hs: "HomeServer") -> None:
|
|
|
|
"""Enable sentry integration, if enabled in configuration"""
|
2019-02-12 11:03:40 -05:00
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
if not hs.config.metrics.sentry_enabled:
|
2019-02-12 08:55:58 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
import sentry_sdk
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
sentry_sdk.init(
|
2022-02-14 08:12:22 -05:00
|
|
|
dsn=hs.config.metrics.sentry_dsn,
|
2022-06-07 10:24:11 -04:00
|
|
|
release=SYNAPSE_VERSION,
|
2023-12-12 11:04:41 -05:00
|
|
|
environment=hs.config.metrics.sentry_environment,
|
2021-09-23 12:03:01 -04:00
|
|
|
)
|
2019-02-12 11:03:40 -05:00
|
|
|
|
|
|
|
# We set some default tags that give some context to this instance
|
2019-02-12 08:55:58 -05:00
|
|
|
with sentry_sdk.configure_scope() as scope:
|
2021-09-13 13:07:12 -04:00
|
|
|
scope.set_tag("matrix_server_name", hs.config.server.server_name)
|
2019-02-12 08:55:58 -05:00
|
|
|
|
2021-09-13 13:07:12 -04:00
|
|
|
app = (
|
|
|
|
hs.config.worker.worker_app
|
|
|
|
if hs.config.worker.worker_app
|
|
|
|
else "synapse.app.homeserver"
|
|
|
|
)
|
2020-04-29 11:23:08 -04:00
|
|
|
name = hs.get_instance_name()
|
2019-02-12 08:55:58 -05:00
|
|
|
scope.set_tag("worker_app", app)
|
|
|
|
scope.set_tag("worker_name", name)
|
2019-04-09 12:03:35 -04:00
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def setup_sdnotify(hs: "HomeServer") -> None:
|
2019-07-22 09:19:38 -04:00
|
|
|
"""Adds process state hooks to tell systemd what we are up to."""
|
|
|
|
|
|
|
|
# Tell systemd our state, if we're using it. This will silently fail if
|
|
|
|
# we're not using systemd.
|
2019-12-19 09:52:52 -05:00
|
|
|
sdnotify(b"READY=1\nMAINPID=%i" % (os.getpid(),))
|
2019-07-22 09:19:38 -04:00
|
|
|
|
|
|
|
hs.get_reactor().addSystemEventTrigger(
|
2019-08-17 04:09:52 -04:00
|
|
|
"before", "shutdown", sdnotify, b"STOPPING=1"
|
2019-07-22 09:19:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-08-17 04:09:52 -04:00
|
|
|
sdnotify_sockaddr = os.getenv("NOTIFY_SOCKET")
|
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def sdnotify(state: bytes) -> None:
|
2019-08-17 04:09:52 -04:00
|
|
|
"""
|
|
|
|
Send a notification to systemd, if the NOTIFY_SOCKET env var is set.
|
|
|
|
|
|
|
|
This function is based on the sdnotify python package, but since it's only a few
|
|
|
|
lines of code, it's easier to duplicate it here than to add a dependency on a
|
|
|
|
package which many OSes don't include as a matter of principle.
|
|
|
|
|
|
|
|
Args:
|
2021-11-10 15:06:54 -05:00
|
|
|
state: notification to send
|
2019-08-17 04:09:52 -04:00
|
|
|
"""
|
|
|
|
if not isinstance(state, bytes):
|
|
|
|
raise TypeError("sdnotify should be called with a bytes")
|
|
|
|
if not sdnotify_sockaddr:
|
|
|
|
return
|
|
|
|
addr = sdnotify_sockaddr
|
|
|
|
if addr[0] == "@":
|
|
|
|
addr = "\0" + addr[1:]
|
|
|
|
|
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as sock:
|
|
|
|
sock.connect(addr)
|
|
|
|
sock.sendall(state)
|
|
|
|
except Exception as e:
|
|
|
|
# this is a bit surprising, since we don't expect to have a NOTIFY_SOCKET
|
|
|
|
# unless systemd is expecting us to notify it.
|
|
|
|
logger.warning("Unable to send notification to systemd: %s", e)
|
2021-04-23 14:20:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
def max_request_body_size(config: HomeServerConfig) -> int:
|
|
|
|
"""Get a suitable maximum size for incoming HTTP requests"""
|
|
|
|
|
|
|
|
# Other than media uploads, the biggest request we expect to see is a fully-loaded
|
|
|
|
# /federation/v1/send request.
|
|
|
|
#
|
|
|
|
# The main thing in such a request is up to 50 PDUs, and up to 100 EDUs. PDUs are
|
|
|
|
# limited to 65536 bytes (possibly slightly more if the sender didn't use canonical
|
|
|
|
# json encoding); there is no specced limit to EDUs (see
|
|
|
|
# https://github.com/matrix-org/matrix-doc/issues/3121).
|
|
|
|
#
|
|
|
|
# in short, we somewhat arbitrarily limit requests to 200 * 64K (about 12.5M)
|
|
|
|
#
|
|
|
|
max_request_size = 200 * MAX_PDU_SIZE
|
|
|
|
|
|
|
|
# if we have a media repo enabled, we may need to allow larger uploads than that
|
|
|
|
if config.media.can_load_media_repo:
|
|
|
|
max_request_size = max(max_request_size, config.media.max_upload_size)
|
|
|
|
|
|
|
|
return max_request_size
|