Skip to content

Commit dbd0297

Browse files
committed
Merge branch 'develop' into madlittlemods/relocate-snowflake-config-validation
Conflicts: synapse/app/homeserver.py tests/config/test_registration_config.py
2 parents 5df163b + 715cc5e commit dbd0297

4 files changed

Lines changed: 73 additions & 25 deletions

File tree

changelog.d/19015.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Split homeserver creation (`create_homeserver`) and setup (`setup`).

synapse/app/homeserver.py

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def gz_wrap(r: Resource) -> Resource:
8383

8484

8585
class SynapseHomeServer(HomeServer):
86+
"""
87+
Homeserver class for the main Synapse process.
88+
"""
89+
8690
DATASTORE_CLASS = DataStore
8791

8892
def _listener_http(
@@ -345,23 +349,17 @@ def load_or_generate_config(argv_options: List[str]) -> HomeServerConfig:
345349
return config
346350

347351

348-
def setup(
352+
def create_homeserver(
349353
config: HomeServerConfig,
350354
reactor: Optional[ISynapseReactor] = None,
351-
freeze: bool = True,
352355
) -> SynapseHomeServer:
353356
"""
354-
Create and setup the main Synapse homeserver instance given a configuration.
357+
Create a homeserver instance for the Synapse main process.
355358
356359
Args:
357360
config: The configuration for the homeserver.
358361
reactor: Optionally provide a reactor to use. Can be useful in different
359362
scenarios that you want control over the reactor, such as tests.
360-
freeze: whether to freeze the homeserver base objects in the garbage collector.
361-
May improve garbage collection performance by marking objects with an effectively
362-
static lifetime as frozen so they don't need to be considered for cleanup.
363-
If you ever want to `shutdown` the homeserver, this needs to be
364-
False otherwise the homeserver cannot be garbage collected after `shutdown`.
365363
366364
Returns:
367365
A homeserver instance.
@@ -380,24 +378,50 @@ def setup(
380378
synapse.metrics.MIN_TIME_BETWEEN_GCS = config.server.gc_seconds
381379

382380
hs = SynapseHomeServer(
383-
config.server.server_name,
381+
hostname=config.server.server_name,
384382
config=config,
385383
reactor=reactor,
386384
)
387385

388-
setup_logging(hs, config, use_worker_options=False)
386+
return hs
389387

390-
# Start the tracer
391-
init_tracer(hs) # noqa
392388

389+
def setup(
390+
hs: SynapseHomeServer,
391+
*,
392+
freeze: bool = True,
393+
) -> None:
394+
"""
395+
Setup a Synapse homeserver instance given a configuration.
396+
397+
Args:
398+
hs: The homeserver to setup.
399+
freeze: whether to freeze the homeserver base objects in the garbage collector.
400+
May improve garbage collection performance by marking objects with an effectively
401+
static lifetime as frozen so they don't need to be considered for cleanup.
402+
If you ever want to `shutdown` the homeserver, this needs to be
403+
False otherwise the homeserver cannot be garbage collected after `shutdown`.
404+
405+
Returns:
406+
A homeserver instance.
407+
"""
408+
409+
setup_logging(hs, hs.config, use_worker_options=False)
410+
411+
# Log after we've configured logging.
393412
logger.info("Setting up server")
394413

414+
# Start the tracer
415+
init_tracer(hs) # noqa
416+
395417
try:
396418
hs.setup()
397419
except Exception as e:
398420
handle_startup_exception(e)
399421

400-
async def start() -> None:
422+
async def _start_when_reactor_running() -> None:
423+
# TODO: Feels like this should be moved somewhere else.
424+
#
401425
# Load the OIDC provider metadatas, if OIDC is enabled.
402426
if hs.config.oidc.oidc_enabled:
403427
oidc = hs.get_oidc_handler()
@@ -406,21 +430,31 @@ async def start() -> None:
406430

407431
await _base.start(hs, freeze)
408432

433+
# TODO: This should be moved to `SynapseHomeServer.start_background_tasks` (not
434+
# `HomeServer.start_background_tasks`) (this way it matches the behavior of only
435+
# running on `main`)
409436
hs.get_datastores().main.db_pool.updates.start_doing_background_updates()
410437

411-
register_start(hs, start)
438+
# Register a callback to be invoked once the reactor is running
439+
register_start(hs, _start_when_reactor_running)
412440

413-
return hs
414441

442+
def start_reactor(
443+
config: HomeServerConfig,
444+
) -> None:
445+
"""
446+
Start the reactor (Twisted event-loop).
415447
416-
def run(hs: HomeServer) -> None:
448+
Args:
449+
config: The configuration for the homeserver.
450+
"""
417451
_base.start_reactor(
418452
"synapse-homeserver",
419-
soft_file_limit=hs.config.server.soft_file_limit,
420-
gc_thresholds=hs.config.server.gc_thresholds,
421-
pid_file=hs.config.server.pid_file,
422-
daemonize=hs.config.server.daemonize,
423-
print_pidfile=hs.config.server.print_pidfile,
453+
soft_file_limit=config.server.soft_file_limit,
454+
gc_thresholds=config.server.gc_thresholds,
455+
pid_file=config.server.pid_file,
456+
daemonize=config.server.daemonize,
457+
print_pidfile=config.server.print_pidfile,
424458
logger=logger,
425459
)
426460

@@ -431,13 +465,14 @@ def main() -> None:
431465
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
432466
# check base requirements
433467
check_requirements()
434-
hs = setup(homeserver_config)
468+
hs = create_homeserver(homeserver_config)
469+
setup(hs)
435470

436471
# redirect stdio to the logs, if configured.
437472
if not hs.config.logging.no_redirect_stdio:
438473
redirect_stdio_to_logs()
439474

440-
run(hs)
475+
start_reactor(homeserver_config)
441476

442477

443478
if __name__ == "__main__":

tests/app/test_homeserver_start.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ def test_wrong_start_caught(self) -> None:
3737
self.add_lines_to_config([" main:", " host: 127.0.0.1", " port: 1234"])
3838
# Ensure that starting master process with worker config raises an exception
3939
with self.assertRaises(ConfigError):
40+
# Do a normal homeserver creation and setup
4041
homeserver_config = synapse.app.homeserver.load_or_generate_config(
4142
["-c", self.config_file]
4243
)
43-
synapse.app.homeserver.setup(homeserver_config)
44+
# XXX: The error will be raised at this point
45+
hs = synapse.app.homeserver.create_homeserver(homeserver_config)
46+
# Continue with the setup. We don't expect this to run because we raised
47+
# earlier, but in the future, the code could be refactored to raise the
48+
# error in a different place.
49+
synapse.app.homeserver.setup(hs)

tests/config/test_registration_config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,16 @@ def test_refuse_to_start_if_open_registration_and_no_verification(self) -> None:
118118

119119
# Test that allowing open registration without verification raises an error
120120
with self.assertRaises(SystemExit):
121+
# Do a normal homeserver creation and setup
121122
homeserver_config = synapse.app.homeserver.load_or_generate_config(
122123
["-c", self.config_file]
123124
)
124-
synapse.app.homeserver.setup(homeserver_config)
125+
# XXX: The error will be raised at this point
126+
hs = synapse.app.homeserver.create_homeserver(homeserver_config)
127+
# Continue with the setup. We don't expect this to run because we raised
128+
# earlier, but in the future, the code could be refactored to raise the
129+
# error in a different place.
130+
synapse.app.homeserver.setup(hs)
125131

126132
def test_load_config_error_if_open_registration_and_no_verification(self) -> None:
127133
"""

0 commit comments

Comments
 (0)