Skip to content

Commit 1c09350

Browse files
Switch task scheduler from raw logcontext manipulation (set_current_context) to utils (PreserveLoggingContext) (#18990)
Prefer the utils over raw logcontext manipulation. Spawning from adding some logcontext debug logs in #18966 and since we're not logging at the `set_current_context(...)` level (see reasoning there), this removes some usage of `set_current_context(...)`.
1 parent 0615b64 commit 1c09350

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

changelog.d/18990.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Switch task scheduler from raw logcontext manipulation to using the dedicated logcontext utils.

synapse/util/task_scheduler.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
from synapse.logging.context import (
2828
ContextResourceUsage,
2929
LoggingContext,
30+
PreserveLoggingContext,
3031
nested_logging_context,
31-
set_current_context,
3232
)
3333
from synapse.metrics import SERVER_NAME_LABEL, LaterGauge
3434
from synapse.metrics.background_process_metrics import (
@@ -422,14 +422,11 @@ def _occasional_report(
422422
"""
423423

424424
current_time = self._clock.time()
425-
calling_context = set_current_context(task_log_context)
426-
try:
425+
with PreserveLoggingContext(task_log_context):
427426
usage = task_log_context.get_resource_usage()
428427
TaskScheduler._log_task_usage(
429428
"continuing", task, usage, current_time - start_time
430429
)
431-
finally:
432-
set_current_context(calling_context)
433430

434431
async def wrapper() -> None:
435432
with nested_logging_context(task.id) as log_context:

0 commit comments

Comments
 (0)