diff options
author | Erik Johnston <erik@matrix.org> | 2019-12-10 14:20:26 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-10 14:20:26 +0000 |
commit | 35f3c366ef70541a8e5d826bb3da530c07723e1c (patch) | |
tree | f0a6b3bddf1a560ccb1b0e635037b43bec880c4c /synapse/logging/context.py | |
parent | Merge pull request #6499 from matrix-org/erikj/fix_sqlite_7 (diff) | |
parent | Update comment (diff) | |
download | synapse-35f3c366ef70541a8e5d826bb3da530c07723e1c.tar.xz |
Merge pull request #6505 from matrix-org/erikj/make_deferred_yiedable
Fix `make_deferred_yieldable` to work with coroutines
Diffstat (limited to 'synapse/logging/context.py')
-rw-r--r-- | synapse/logging/context.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 2c1fb9ddac..6747f29e6a 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -23,6 +23,7 @@ them. See doc/log_contexts.rst for details on how this works. """ +import inspect import logging import threading import types @@ -612,7 +613,8 @@ def run_in_background(f, *args, **kwargs): def make_deferred_yieldable(deferred): - """Given a deferred, make it follow the Synapse logcontext rules: + """Given a deferred (or coroutine), make it follow the Synapse logcontext + rules: If the deferred has completed (or is not actually a Deferred), essentially does nothing (just returns another completed deferred with the @@ -624,6 +626,13 @@ def make_deferred_yieldable(deferred): (This is more-or-less the opposite operation to run_in_background.) """ + if inspect.isawaitable(deferred): + # If we're given a coroutine we convert it to a deferred so that we + # run it and find out if it immediately finishes, it it does then we + # don't need to fiddle with log contexts at all and can return + # immediately. + deferred = defer.ensureDeferred(deferred) + if not isinstance(deferred, defer.Deferred): return deferred |