diff options
author | Erik Johnston <erik@matrix.org> | 2019-12-10 11:22:12 +0000 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2019-12-10 11:22:12 +0000 |
commit | 9a2223d4c8c2a46f7ab072597bdba2614bc3e6ea (patch) | |
tree | ce1b5d9f6cdf8593e1ae55be6fa9fb3f7fbaadf8 /synapse/logging | |
parent | Merge tag 'v1.7.0rc1' into develop (diff) | |
download | synapse-9a2223d4c8c2a46f7ab072597bdba2614bc3e6ea.tar.xz |
Fix make_deferred_yieldable to work with coroutines
Diffstat (limited to 'synapse/logging')
-rw-r--r-- | synapse/logging/context.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 2c1fb9ddac..9f484ce59e 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,11 @@ 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 need to convert it to a deferred so that + # we can attach callbacks (and not immediately return). + deferred = defer.ensureDeferred(deferred) + if not isinstance(deferred, defer.Deferred): return deferred |