From b6fc0c84019f448f56bd536dc25717cbb9282b63 Mon Sep 17 00:00:00 2001 From: Sean Quah Date: Tue, 5 Oct 2021 13:20:06 +0100 Subject: [PATCH] Fix long-standing bug where `ReadWriteLock` could drop logging contexts Use `PreserveLoggingContext()` to ensure that logging contexts are not lost when exiting a read/write lock. When exiting a read/write lock, callbacks on a `Deferred` are triggered as a signal to any waiting coroutines. Any waiting coroutine that becomes runnable is likely to follow the Synapse logging context rules and will restore its own logging context, then either run to completion or await another `Deferred`, resetting the logging context in the process. --- changelog.d/10993.misc | 1 + synapse/util/async_helpers.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelog.d/10993.misc diff --git a/changelog.d/10993.misc b/changelog.d/10993.misc new file mode 100644 index 000000000000..23c73dbac5c1 --- /dev/null +++ b/changelog.d/10993.misc @@ -0,0 +1 @@ +Fix a long-standing bug where `ReadWriteLock`s could drop logging contexts on exit. diff --git a/synapse/util/async_helpers.py b/synapse/util/async_helpers.py index 82d918a05fd0..5df80ea8e7b4 100644 --- a/synapse/util/async_helpers.py +++ b/synapse/util/async_helpers.py @@ -438,7 +438,8 @@ def _ctx_manager(): try: yield finally: - new_defer.callback(None) + with PreserveLoggingContext(): + new_defer.callback(None) self.key_to_current_readers.get(key, set()).discard(new_defer) return _ctx_manager() @@ -466,7 +467,8 @@ def _ctx_manager(): try: yield finally: - new_defer.callback(None) + with PreserveLoggingContext(): + new_defer.callback(None) if self.key_to_current_writer[key] == new_defer: self.key_to_current_writer.pop(key)