Skip to content

Conversation

@MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Oct 31, 2025

Refactor app entrypoints (avoid exit(1) in our composable functions)

  • Move register_start (calls os._exit(1)) out of setup (our composable function)
  • Align our app entrypoints: homeserver (main), generic_worker (worker), and admin_cmd

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) (c.f Synapse Pro for small hosts), 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/48

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)



def start(config: HomeServerConfig, args: argparse.Namespace) -> None:
def create_homeserver(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored all of our entrypoints to use the create_homeserver(...) -> setup(...) -> start(...) pattern

homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
start(homeserver_config)
worker_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.

Equivalent and simpler 😌

Comment on lines 463 to 464
# Register a callback to be invoked once the reactor is running
register_start(hs, lambda: start(hs))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main fix is moving this register_start(...) from setup(...) (one of our composable functions used in Synapse Pro for small hosts) to here in main(...)

# everything with this homeserver.
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
# check base requirements
check_requirements()
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 check_requirements() here as we call it globally in synapse/app/__init__.py#L29-L35

If I add some debug logging to it, we actually it three times when I start Synapse (even after this change):

$ poetry run synapse_homeserver --config-path homeserver.yaml
check_requirements(None)
check_requirements("oidc")
check_requirements("opentracing)

@MadLittleMods MadLittleMods marked this pull request as ready for review October 31, 2025 22:07
@MadLittleMods MadLittleMods requested a review from a team as a code owner October 31, 2025 22:07
Copy link
Member

@anoadragon453 anoadragon453 left a comment

Choose a reason for hiding this comment

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

LGTM. A minor, non-blocking points below.

setup(hs)

# Register a callback to be invoked once the reactor is running
register_start(hs, lambda: start(hs))
Copy link
Member

Choose a reason for hiding this comment

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

Why lambda: start(hs) instead of:

register_start(hs, start, hs)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷 Hard decisions on what's more clear

I've updated register_start(hs, start, hs) even though it can take a second to recognize what's happening

@MadLittleMods MadLittleMods merged commit a710745 into develop Nov 3, 2025
43 checks passed
@MadLittleMods MadLittleMods deleted the madlittlemods/refactor-app-start branch November 3, 2025 18:04
@MadLittleMods
Copy link
Contributor Author

Thanks for the review @anoadragon453 🐝

(in time for the RC 🚀)

MadLittleMods added a commit that referenced this pull request Nov 3, 2025
This regressed in #19121. I
moved things in #19121 because
I thought that it made sense to redirect anything printed to
`stdout`/`stderr` to the logs as early as possible. But we actually want
to log any immediately apparent problems during initialization to
`stderr` in the terminal so that they are obvious and visible to the
operator.

Now, I've moved `redirect_stdio_to_logs()` back to where it was
previously along with some proper comment context for why we have it
there.
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.

3 participants