From 52fa9e83fe37f3e63e685d08d2939d1ee3995e54 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 10 Sep 2025 15:56:52 -0500 Subject: [PATCH 1/2] Better explain which context the task is run in when using `run_in_background(...)` or `run_as_background_process(...)` Follow-up to https://github.com/element-hq/synapse/pull/18900 --- synapse/logging/context.py | 6 ++++-- synapse/metrics/background_process_metrics.py | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 6eaa19d2f6a..aa4b98e7c76 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -802,8 +802,9 @@ def run_in_background( deferred returned by the function completes. To explain how the log contexts work here: - - When this function is called, the current context is stored ("original"), we kick - off the background task, and we restore that original context before returning + - When `run_in_background` is called, the current context is stored ("original"), + we kick off the background task in the current context, and we restore that + original context before returning - When the background task finishes, we don't want to leak our context into the reactor which would erroneously get attached to the next operation picked up by the event loop. We add a callback to the deferred which will clear the logging @@ -828,6 +829,7 @@ def run_in_background( """ calling_context = current_context() try: + # (kick off the task in the current context) res = f(*args, **kwargs) except Exception: # the assumption here is that the caller doesn't want to be disturbed diff --git a/synapse/metrics/background_process_metrics.py b/synapse/metrics/background_process_metrics.py index c6ee21d42aa..633705b02a3 100644 --- a/synapse/metrics/background_process_metrics.py +++ b/synapse/metrics/background_process_metrics.py @@ -286,9 +286,11 @@ async def run() -> Optional[R]: ).dec() # To explain how the log contexts work here: - # - When this function is called, the current context is stored (using - # `PreserveLoggingContext`), we kick off the background task, and we restore the - # original context before returning (also part of `PreserveLoggingContext`). + # - When `run_as_background_process` is called, the current context is stored + # (using `PreserveLoggingContext`), we kick off the background task, and we + # restore the original context before returning (also part of + # `PreserveLoggingContext`). + # - The background task runs in its own new logcontext named after `desc` # - When the background task finishes, we don't want to leak our background context # into the reactor which would erroneously get attached to the next operation # picked up by the event loop. We use `PreserveLoggingContext` to set the From f865b568ef58da27464617942c0ec2d749b37b57 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 10 Sep 2025 15:58:40 -0500 Subject: [PATCH 2/2] Add changelog --- changelog.d/18906.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/18906.misc diff --git a/changelog.d/18906.misc b/changelog.d/18906.misc new file mode 100644 index 00000000000..d7d8b47eb0c --- /dev/null +++ b/changelog.d/18906.misc @@ -0,0 +1 @@ +Better explain how we manage the logcontext in `run_in_background(...)` and `run_as_background_process(...)`.