From db00925ae77f134872b5e7cb26a1cacb31281334 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Mon, 3 Nov 2025 16:16:23 -0600 Subject: [PATCH] Redirect `stdout`/`stderr` to logs after initialization (#19131) This regressed in https://github.com/element-hq/synapse/pull/19121. I moved things in https://github.com/element-hq/synapse/pull/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. --- changelog.d/19131.misc | 1 + synapse/app/admin_cmd.py | 1 + synapse/app/generic_worker.py | 13 +++++++++---- synapse/app/homeserver.py | 13 +++++++++---- 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 changelog.d/19131.misc diff --git a/changelog.d/19131.misc b/changelog.d/19131.misc new file mode 100644 index 000000000..cb1fb8f02 --- /dev/null +++ b/changelog.d/19131.misc @@ -0,0 +1 @@ +Refactor and align app entrypoints (avoid `exit(1)` in our composable functions). diff --git a/synapse/app/admin_cmd.py b/synapse/app/admin_cmd.py index dac603de8..193482b7f 100644 --- a/synapse/app/admin_cmd.py +++ b/synapse/app/admin_cmd.py @@ -369,6 +369,7 @@ async def start(admin_command_server: AdminCmdServer, args: argparse.Namespace) def main() -> None: homeserver_config, args = load_config(sys.argv[1:]) with LoggingContext(name="main", server_name=homeserver_config.server.server_name): + # Initialize and setup the homeserver admin_command_server = create_homeserver(homeserver_config) setup(admin_command_server) diff --git a/synapse/app/generic_worker.py b/synapse/app/generic_worker.py index a1dde368d..0a4abd183 100644 --- a/synapse/app/generic_worker.py +++ b/synapse/app/generic_worker.py @@ -450,16 +450,21 @@ def main() -> None: # Create a logging context as soon as possible so we can start associating # everything with this homeserver. with LoggingContext(name="main", server_name=homeserver_config.server.server_name): - # redirect stdio to the logs, if configured. - if not homeserver_config.logging.no_redirect_stdio: - redirect_stdio_to_logs() - + # Initialize and setup the homeserver hs = create_homeserver(homeserver_config) try: setup(hs) except Exception as e: handle_startup_exception(e) + # For problems immediately apparent during initialization, we want to log to + # stderr in the terminal so that they are obvious and visible to the operator. + # + # Now that we're past the initialization stage, we can redirect anything printed + # to stdio to the logs, if configured. + if not homeserver_config.logging.no_redirect_stdio: + redirect_stdio_to_logs() + # Register a callback to be invoked once the reactor is running register_start(hs, start, hs) diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index fb937c63c..bd51aad9a 100644 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -479,16 +479,21 @@ def main() -> None: # Create a logging context as soon as possible so we can start associating # everything with this homeserver. with LoggingContext(name="main", server_name=homeserver_config.server.server_name): - # redirect stdio to the logs, if configured. - if not homeserver_config.logging.no_redirect_stdio: - redirect_stdio_to_logs() - + # Initialize and setup the homeserver hs = create_homeserver(homeserver_config) try: setup(hs) except Exception as e: handle_startup_exception(e) + # For problems immediately apparent during initialization, we want to log to + # stderr in the terminal so that they are obvious and visible to the operator. + # + # Now that we're past the initialization stage, we can redirect anything printed + # to stdio to the logs, if configured. + if not homeserver_config.logging.no_redirect_stdio: + redirect_stdio_to_logs() + # Register a callback to be invoked once the reactor is running register_start(hs, start, hs)