Skip to content

Commit 52fa9e8

Browse files
committed
Better explain which context the task is run in when using run_in_background(...) or run_as_background_process(...)
Follow-up to #18900
1 parent 9cc4001 commit 52fa9e8

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

synapse/logging/context.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,9 @@ def run_in_background(
802802
deferred returned by the function completes.
803803
804804
To explain how the log contexts work here:
805-
- When this function is called, the current context is stored ("original"), we kick
806-
off the background task, and we restore that original context before returning
805+
- When `run_in_background` is called, the current context is stored ("original"),
806+
we kick off the background task in the current context, and we restore that
807+
original context before returning
807808
- When the background task finishes, we don't want to leak our context into the
808809
reactor which would erroneously get attached to the next operation picked up by
809810
the event loop. We add a callback to the deferred which will clear the logging
@@ -828,6 +829,7 @@ def run_in_background(
828829
"""
829830
calling_context = current_context()
830831
try:
832+
# (kick off the task in the current context)
831833
res = f(*args, **kwargs)
832834
except Exception:
833835
# the assumption here is that the caller doesn't want to be disturbed

synapse/metrics/background_process_metrics.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,11 @@ async def run() -> Optional[R]:
286286
).dec()
287287

288288
# To explain how the log contexts work here:
289-
# - When this function is called, the current context is stored (using
290-
# `PreserveLoggingContext`), we kick off the background task, and we restore the
291-
# original context before returning (also part of `PreserveLoggingContext`).
289+
# - When `run_as_background_process` is called, the current context is stored
290+
# (using `PreserveLoggingContext`), we kick off the background task, and we
291+
# restore the original context before returning (also part of
292+
# `PreserveLoggingContext`).
293+
# - The background task runs in its own new logcontext named after `desc`
292294
# - When the background task finishes, we don't want to leak our background context
293295
# into the reactor which would erroneously get attached to the next operation
294296
# picked up by the event loop. We use `PreserveLoggingContext` to set the

0 commit comments

Comments
 (0)