Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/18990.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch task scheduler from raw logcontext manipulation to using the dedicated logcontext utils.
7 changes: 2 additions & 5 deletions synapse/util/task_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -425,14 +425,11 @@ def _occasional_report(
"""

current_time = self._clock.time()
calling_context = set_current_context(task_log_context)
Copy link
Contributor Author

@MadLittleMods MadLittleMods Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One could argue that set_current_context(...) usage is perfectly valid and just as much of a utility as PreserveLoggingContext(...). It does work perfectly fine and handles the intricacies as we would expect 👍

But we do have a PreserveLoggingContext(...) that covers the exact pattern we're trying to do here.

And with the debug logs being introduced in #18966, since we're not logging at the set_current_context(...) level (see reasoning there), this removes some usage of set_current_context(...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, nothing was broken before

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:
Expand Down
Loading