Fix logging context warnings when losing replication connection (#10984)

Instead of triggering `__exit__` manually on the replication handler's
logging context, use it as a context manager so that there is an
`__enter__` call to balance the `__exit__`.
This commit is contained in:
Sean Quah 2021-10-15 13:10:58 +01:00 committed by GitHub
parent 013e0f9cae
commit 6a67f3786a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 10 deletions

1
changelog.d/10984.misc Normal file
View File

@ -0,0 +1 @@
Fix spurious warnings about losing the logging context on the `ReplicationCommandHandler` when losing the replication connection.

View File

@ -182,9 +182,13 @@ class BaseReplicationStreamProtocol(LineOnlyReceiver):
# a logcontext which we use for processing incoming commands. We declare it as a # a logcontext which we use for processing incoming commands. We declare it as a
# background process so that the CPU stats get reported to prometheus. # background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext( with PreserveLoggingContext():
"replication-conn", self.conn_id # thanks to `PreserveLoggingContext()`, the new logcontext is guaranteed to
) # capture the sentinel context as its containing context and won't prevent
# GC of / unintentionally reactivate what would be the current context.
self._logging_context = BackgroundProcessLoggingContext(
"replication-conn", self.conn_id
)
def connectionMade(self): def connectionMade(self):
logger.info("[%s] Connection established", self.id()) logger.info("[%s] Connection established", self.id())
@ -434,8 +438,12 @@ class BaseReplicationStreamProtocol(LineOnlyReceiver):
if self.transport: if self.transport:
self.transport.unregisterProducer() self.transport.unregisterProducer()
# mark the logging context as finished # mark the logging context as finished by triggering `__exit__()`
self._logging_context.__exit__(None, None, None) with PreserveLoggingContext():
with self._logging_context:
pass
# the sentinel context is now active, which may not be correct.
# PreserveLoggingContext() will restore the correct logging context.
def __str__(self): def __str__(self):
addr = None addr = None

View File

@ -100,9 +100,13 @@ class RedisSubscriber(txredisapi.SubscriberProtocol):
# a logcontext which we use for processing incoming commands. We declare it as a # a logcontext which we use for processing incoming commands. We declare it as a
# background process so that the CPU stats get reported to prometheus. # background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext( with PreserveLoggingContext():
"replication_command_handler" # thanks to `PreserveLoggingContext()`, the new logcontext is guaranteed to
) # capture the sentinel context as its containing context and won't prevent
# GC of / unintentionally reactivate what would be the current context.
self._logging_context = BackgroundProcessLoggingContext(
"replication_command_handler"
)
def connectionMade(self): def connectionMade(self):
logger.info("Connected to redis") logger.info("Connected to redis")
@ -182,8 +186,12 @@ class RedisSubscriber(txredisapi.SubscriberProtocol):
super().connectionLost(reason) super().connectionLost(reason)
self.synapse_handler.lost_connection(self) self.synapse_handler.lost_connection(self)
# mark the logging context as finished # mark the logging context as finished by triggering `__exit__()`
self._logging_context.__exit__(None, None, None) with PreserveLoggingContext():
with self._logging_context:
pass
# the sentinel context is now active, which may not be correct.
# PreserveLoggingContext() will restore the correct logging context.
def send_command(self, cmd: Command): def send_command(self, cmd: Command):
"""Send a command if connection has been established. """Send a command if connection has been established.