Skip to content

Commit ea5a841

Browse files
committed
Add log context explanation to run_in_background
1 parent 6a98096 commit ea5a841

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

synapse/logging/context.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,14 @@ def run_in_background(
872872
return from the function, and that the sentinel context is set once the
873873
deferred returned by the function completes.
874874
875+
To explain how the log contexts work here:
876+
- When this function is called, the current context is stored ("original"), we kick
877+
off the background task, and we restore that original context before returning
878+
- When the background task finishes, we don't want to leak our context into the
879+
reactor which would erroneously get attached to the next operation picked up by
880+
the event loop. We add a callback to the deferred which will clear the logging
881+
context after it finishes and yields control back to the reactor.
882+
875883
Useful for wrapping functions that return a deferred or coroutine, which you don't
876884
yield or await on (for instance because you want to pass it to
877885
deferred.gatherResults()).
@@ -963,6 +971,8 @@ def _asdf(result: ResultT, context: LoggingContextOrSentinel) -> ResultT:
963971
)
964972
return _set_context_cb(result, context)
965973

974+
# TODO: Why aren't we using `sentinel` context here. We can't guarantee that `ctx` is
975+
# `sentinel` here?
966976
d.addBoth(_asdf, ctx)
967977
return d
968978

0 commit comments

Comments
 (0)