From cd65573b070328851d3965488a170d0605877528 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Mon, 29 Sep 2025 13:24:38 -0500 Subject: [PATCH 1/2] Switch task scheduler from `set_current_context` -> `PreserveLoggingContext` Prefer the utils over raw logcontext manipulation --- synapse/util/task_scheduler.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/synapse/util/task_scheduler.py b/synapse/util/task_scheduler.py index 0539989320f..344695e2a14 100644 --- a/synapse/util/task_scheduler.py +++ b/synapse/util/task_scheduler.py @@ -27,8 +27,8 @@ from synapse.logging.context import ( ContextResourceUsage, LoggingContext, + PreserveLoggingContext, nested_logging_context, - set_current_context, ) from synapse.metrics import SERVER_NAME_LABEL, LaterGauge from synapse.metrics.background_process_metrics import ( @@ -425,14 +425,11 @@ def _occasional_report( """ current_time = self._clock.time() - calling_context = set_current_context(task_log_context) - try: + with PreserveLoggingContext(task_log_context): usage = task_log_context.get_resource_usage() TaskScheduler._log_task_usage( "continuing", task, usage, current_time - start_time ) - finally: - set_current_context(calling_context) async def wrapper() -> None: with nested_logging_context(task.id) as log_context: From c2e15ef26b5eeb36a96190aa351fcfa75f43cfd1 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Mon, 29 Sep 2025 13:58:11 -0500 Subject: [PATCH 2/2] Add changelog --- changelog.d/18990.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/18990.misc diff --git a/changelog.d/18990.misc b/changelog.d/18990.misc new file mode 100644 index 00000000000..f7f8ac5ffdf --- /dev/null +++ b/changelog.d/18990.misc @@ -0,0 +1 @@ +Switch task scheduler from raw logcontext manipulation to using the dedicated logcontext utils.