Split homeserver creation and setup (#19015)
### Background As part of Element's plan to support a light form of vhosting (virtual host) (multiple instances of Synapse in the same Python process), we're currently diving into the details and implications of running multiple instances of Synapse in the same Python process. "Clean tenant provisioning" tracked internally by https://github.com/element-hq/synapse-small-hosts/issues/221 ### Partial startup problem In the context of Synapse Pro for Small Hosts, since the Twisted reactor is already running (from the `multi_synapse` shard process itself), when provisioning a homeserver tenant, the `reactor.callWhenRunning(...)` callbacks will be invoked immediately. This includes the Synapse's [`start`](0615b64bb4/synapse/app/homeserver.py (L418-L429)) callback which sets up everything (including listeners, background tasks, etc). If we encounter an error at this point, we are partially setup but the exception will [bubble back to us](8be122186b/multi_synapse/app/shard.py (L114-L121)) without us having a handle to the homeserver yet so we can't call `hs.shutdown()` and clean everything up. ### What does this PR do? Structures Synapse so we split creating the homeserver instance from setting everything up. This way we have access to `hs` if anything goes wrong during setup and can subsequently `hs.shutdown()` to clean everything up.
This commit is contained in:
@@ -37,7 +37,13 @@ class HomeserverAppStartTestCase(ConfigFileTestCase):
|
||||
self.add_lines_to_config([" main:", " host: 127.0.0.1", " port: 1234"])
|
||||
# Ensure that starting master process with worker config raises an exception
|
||||
with self.assertRaises(ConfigError):
|
||||
# Do a normal homeserver creation and setup
|
||||
homeserver_config = synapse.app.homeserver.load_or_generate_config(
|
||||
["-c", self.config_file]
|
||||
)
|
||||
synapse.app.homeserver.setup(homeserver_config)
|
||||
# XXX: The error will be raised at this point
|
||||
hs = synapse.app.homeserver.create_homeserver(homeserver_config)
|
||||
# Continue with the setup. We don't expect this to run because we raised
|
||||
# earlier, but in the future, the code could be refactored to raise the
|
||||
# error in a different place.
|
||||
synapse.app.homeserver.setup(hs)
|
||||
|
||||
@@ -112,7 +112,13 @@ class RegistrationConfigTestCase(ConfigFileTestCase):
|
||||
|
||||
# Test that allowing open registration without verification raises an error
|
||||
with self.assertRaises(ConfigError):
|
||||
# Do a normal homeserver creation and setup
|
||||
homeserver_config = synapse.app.homeserver.load_or_generate_config(
|
||||
["-c", self.config_file]
|
||||
)
|
||||
synapse.app.homeserver.setup(homeserver_config)
|
||||
# XXX: The error will be raised at this point
|
||||
hs = synapse.app.homeserver.create_homeserver(homeserver_config)
|
||||
# Continue with the setup. We don't expect this to run because we raised
|
||||
# earlier, but in the future, the code could be refactored to raise the
|
||||
# error in a different place.
|
||||
synapse.app.homeserver.setup(hs)
|
||||
|
||||
Reference in New Issue
Block a user