From b9dda0ff224e4c3c332e5aefd761834f09c96433 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 13 Nov 2025 09:57:56 -0600 Subject: [PATCH] Restore printing `sentinel` for `log_record.request` (#19172) This was unintentionally changed in https://github.com/element-hq/synapse/pull/19068. There is no real bug here. Without this PR, we just printed an empty string for the `sentinel` logcontext whereas the prior art behavior was to print `sentinel` which this PR restores. Found while staring at the logs in https://github.com/element-hq/synapse/issues/19165 ### Reproduction strategy 1. Configure Synapse with [logging](https://github.com/element-hq/synapse/blob/df802882bb2ec7d52d5c064c20531fe5a5b263b1/docs/sample_log_config.yaml) 1. Start Synapse: `poetry run synapse_homeserver --config-path homeserver.yaml` 1. Notice the `asyncio - 64 - DEBUG - - Using selector: EpollSelector` log line (notice empty string `- -`) 1. With this PR, the log line will be `asyncio - 64 - DEBUG - sentinel - Using selector: EpollSelector` (notice `sentinel`) --- changelog.d/19172.misc | 1 + synapse/logging/context.py | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 changelog.d/19172.misc diff --git a/changelog.d/19172.misc b/changelog.d/19172.misc new file mode 100644 index 000000000..4792f8b56 --- /dev/null +++ b/changelog.d/19172.misc @@ -0,0 +1 @@ +Restore printing `sentinel` for the log record `request` when no logcontext is active. diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 2410d9572..b6535be38 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -619,19 +619,24 @@ class LoggingContextFilter(logging.Filter): True to include the record in the log output. """ context = current_context() - record.request = self._default_request - - # Avoid overwriting an existing `server_name` on the record. This is running in - # the context of a global log record filter so there may be 3rd-party code that - # adds their own `server_name` and we don't want to interfere with that - # (clobber). - if not hasattr(record, "server_name"): - record.server_name = "unknown_server_from_no_logcontext" - - # context should never be None, but if it somehow ends up being, then - # we end up in a death spiral of infinite loops, so let's check, for + # type-ignore: `context` should never be `None`, but if it somehow ends up + # being, then we end up in a death spiral of infinite loops, so let's check, for # robustness' sake. - if context is not None: + # + # Add some default values to avoid log formatting errors. + if context is None: + record.request = self._default_request # type: ignore[unreachable] + + # Avoid overwriting an existing `server_name` on the record. This is running in + # the context of a global log record filter so there may be 3rd-party code that + # adds their own `server_name` and we don't want to interfere with that + # (clobber). + if not hasattr(record, "server_name"): + record.server_name = "unknown_server_from_no_logcontext" + + # Otherwise, in the normal, expected case, fill in the log record attributes + # from the logcontext. + else: def safe_set(attr: str, value: Any) -> None: """