Skip to content

Commit 675d94a

Browse files
committed
Fix run_as_background_process not following Synapse log context rules
Resulting in bad logs being seen: ``` PreserveLoggingContext: Expected logging context sentinel but found main ``` ``` LoggingContext: Expected logging context main was lost ```
1 parent af1944b commit 675d94a

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

synapse/metrics/background_process_metrics.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from synapse.logging.context import (
4848
ContextResourceUsage,
4949
LoggingContext,
50-
PreserveLoggingContext,
50+
make_deferred_yieldable,
5151
)
5252
from synapse.logging.opentracing import SynapseTags, start_active_span
5353
from synapse.metrics import SERVER_NAME_LABEL
@@ -223,10 +223,9 @@ def run_as_background_process(
223223
This should be used to wrap processes which are fired off to run in the
224224
background, instead of being associated with a particular request.
225225
226-
It returns a Deferred which completes when the function completes, but it doesn't
227-
follow the synapse logcontext rules, which makes it appropriate for passing to
228-
clock.looping_call and friends (or for firing-and-forgetting in the middle of a
229-
normal synapse async function).
226+
It returns a Deferred which completes when the function completes, which makes it
227+
appropriate for passing to clock.looping_call and friends (or for
228+
firing-and-forgetting in the middle of a normal synapse async function).
230229
231230
Args:
232231
desc: a description for this background process type
@@ -241,8 +240,6 @@ def run_as_background_process(
241240
242241
Returns:
243242
Deferred which returns the result of func, or `None` if func raises.
244-
Note that the returned Deferred does not follow the synapse logcontext
245-
rules.
246243
"""
247244

248245
async def run() -> Optional[R]:
@@ -280,10 +277,9 @@ async def run() -> Optional[R]:
280277
name=desc, **{SERVER_NAME_LABEL: server_name}
281278
).dec()
282279

283-
with PreserveLoggingContext():
284280
# Note that we return a Deferred here so that it can be used in a
285281
# looping_call and other places that expect a Deferred.
286-
return defer.ensureDeferred(run())
282+
return make_deferred_yieldable(defer.ensureDeferred(run()))
287283

288284

289285
P = ParamSpec("P")

0 commit comments

Comments
 (0)