From 408a05ebbc70f94cb31e29ebac2d24df8363dfd6 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 13 Nov 2025 15:17:15 -0600 Subject: [PATCH] Fix potential lost logcontext when `PerDestinationQueue.shutdown(...)` (#19178) Spawning from looking at the logs in https://github.com/element-hq/synapse/issues/19165#issuecomment-3527452941 which mention the `federation_transaction_transmission_loop`. I don't think it's the source of the lost logcontext that person in the issue is experiencing because this only applies when you try to `shutdown` the homeserver. Problem code introduced in https://github.com/element-hq/synapse/pull/18828 To explain the fix, see the [*Deferred callbacks*](https://github.com/element-hq/synapse/blob/3b59ac3b69f6a2f73a504699b30313d8dcfe4709/docs/log_contexts.md#deferred-callbacks) section of our logcontext docs for more info (specifically using solution 2). --- changelog.d/19178.misc | 1 + synapse/federation/sender/per_destination_queue.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 changelog.d/19178.misc diff --git a/changelog.d/19178.misc b/changelog.d/19178.misc new file mode 100644 index 0000000000..3f803c3544 --- /dev/null +++ b/changelog.d/19178.misc @@ -0,0 +1 @@ +Fix potential lost logcontext when `PerDestinationQueue.shutdown(...)`. diff --git a/synapse/federation/sender/per_destination_queue.py b/synapse/federation/sender/per_destination_queue.py index 4a1b84aed7..cdacf16d72 100644 --- a/synapse/federation/sender/per_destination_queue.py +++ b/synapse/federation/sender/per_destination_queue.py @@ -41,6 +41,7 @@ from synapse.events import EventBase from synapse.federation.units import Edu from synapse.handlers.presence import format_user_presence_state from synapse.logging import issue9533_logger +from synapse.logging.context import PreserveLoggingContext from synapse.logging.opentracing import SynapseTags, set_tag from synapse.metrics import SERVER_NAME_LABEL, sent_transactions_counter from synapse.types import JsonDict, ReadReceipt @@ -186,7 +187,8 @@ class PerDestinationQueue: self._transaction_manager.shutdown() try: if self.active_transmission_loop is not None: - self.active_transmission_loop.cancel() + with PreserveLoggingContext(): + self.active_transmission_loop.cancel() except Exception: pass