From 95be8dccc25fd74aa402cf4b9257bad492982875 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 23 Oct 2025 20:37:09 -0500 Subject: [PATCH] Add logcontext for SIGHUP callbacks to run in See https://github.com/element-hq/synapse/pull/19095#discussion_r2458308702 --- synapse/app/_base.py | 22 ++++++++++++++-------- synapse/config/logger.py | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/synapse/app/_base.py b/synapse/app/_base.py index d89c52957d..88cb89b0a1 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -112,7 +112,7 @@ P = ParamSpec("P") def register_sighup( - homeserver_instance_id: str, + hs: "HomeServer", func: Callable[P, None], *args: P.args, **kwargs: P.kwargs, @@ -127,19 +127,25 @@ def register_sighup( *args, **kwargs: args and kwargs to be passed to the target function. """ - _instance_id_to_sighup_callbacks_map.setdefault(homeserver_instance_id, []).append( - (func, args, kwargs) + # Wrap the function so we can run it within a logcontext + def _callback_wrapper(*args: P.args, **kwargs: P.kwargs) -> None: + with LoggingContext(name="sighup", server_name=hs.hostname): + func(*args, **kwargs) + + _instance_id_to_sighup_callbacks_map.setdefault(hs.get_instance_id(), []).append( + (_callback_wrapper, args, kwargs) ) -def unregister_sighups(instance_id: str) -> None: +def unregister_sighups(homeserver_instance_id: str) -> None: """ Unregister all sighup functions associated with this Synapse instance. Args: - instance_id: Unique ID for this Synapse process instance. + homeserver_instance_id: The unique ID for this Synapse process instance to + unregister hooks for (`hs.get_instance_id()`). """ - _instance_id_to_sighup_callbacks_map.pop(instance_id, []) + _instance_id_to_sighup_callbacks_map.pop(homeserver_instance_id, []) def start_worker_reactor( @@ -639,8 +645,8 @@ async def start(hs: "HomeServer", freeze: bool = True) -> None: ) setup_sighup_handling() - register_sighup(hs.get_instance_id(), refresh_certificate, hs) - register_sighup(hs.get_instance_id(), reload_cache_config, hs.config) + register_sighup(hs, refresh_certificate, hs) + register_sighup(hs, reload_cache_config, hs.config) # Apply the cache config. hs.config.caches.resize_all_caches() diff --git a/synapse/config/logger.py b/synapse/config/logger.py index e2c4691dcd..e87c6f4220 100644 --- a/synapse/config/logger.py +++ b/synapse/config/logger.py @@ -375,7 +375,7 @@ def setup_logging( if log_config_path: server_name = hs.hostname appbase.register_sighup( - hs.get_instance_id(), _reload_logging_config, server_name, log_config_path + hs, _reload_logging_config, server_name, log_config_path ) # Log immediately so we can grep backwards.