Skip to content

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Oct 3, 2025

Split homeserver creation and setup

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 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 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.

Dev notes

Refactor with start_background_tasks: #18886

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

Comment on lines 447 to 448
# TODO: This should be moved to `SynapseHomeServer.start_background_tasks` (this
# way it matches the behavior of only running on `main`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a future TODO


async def start() -> None:
async def _start_when_reactor_running() -> None:
# TODO: Feels like this should be moved somewhere else.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a future TODO

"You have specified `worker_app` in the config but are attempting to start a non-worker "
"instance. Please use `python -m synapse.app.generic_worker` instead (or remove the option if this is the main process)."
)
sys.exit(1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this exit as it's unreachable since we raise just above


def start_reactor(
hs: HomeServer,
config: HomeServerConfig,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To document why I made the change to pass in the HomeServerConfig instead of hs to start_reactor(...) (as I was having trouble remembering myself the day after):

start_reactor(...) doesn't really have anything to do with the homeserver itself. This is just concerned with starting the Twisted event-loop that could be driving any application. And ideally, we wouldn't even pass something called HomeServerConfig as this is more just ReactorConfig. Currently just done out of convenience.

@MadLittleMods MadLittleMods merged commit 715cc5e into develop Oct 9, 2025
44 checks passed
@MadLittleMods MadLittleMods deleted the maddlittlemods/split-hs-creation-and-setup branch October 9, 2025 18:12
@MadLittleMods
Copy link
Contributor Author

Thanks for the review @reivilibre 🐠

MadLittleMods added a commit that referenced this pull request Oct 9, 2025
…HomeServerConfig` to do cross-config class validation (#19027)

This means we
can move the open registration config validation from `setup()` to
`HomeServerConfig.validate_config()` (much more sane).

Spawning from looking at this area of code in
#19015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants