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

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.

1 participant