Repository: matrix-org/synapse

Issue:
Regression in log context tracking
On both matrix.org and jki.re I'm seeing (on latest develop) a lot of:

```
2021-01-07 18:39:07,534 - synapse.util.metrics - 113 - WARNING - None - Starting metrics collection from sentinel context: metrics will be lost
```

which may mean a bunch of our metrics are inaccurate 


Discussion:
> which may mean a bunch of our metrics are inaccurate

I think the problem has existed for a while: we're just reporting it now.

It will have been introduced by #8935, and I feel like the increased log spam makes this a release-blocker for 1.25.
scratch that, it will be #8939, which didn't make the release.
Agreed. I think the first step is to get it to log *what* block is being ignored, since we have the name there that should be easy.
(it's still a release-blocker, but for 1.26...)
Clearly we need a `next-release blocker` label...

Resolving patch:
diff --git a/synapse/notifier.py b/synapse/notifier.py
--- a/synapse/notifier.py
+++ b/synapse/notifier.py
@@ -396,31 +396,30 @@ def on_new_event(
 
         Will wake up all listeners for the given users and rooms.
         """
-        with PreserveLoggingContext():
-            with Measure(self.clock, "on_new_event"):
-                user_streams = set()
+        with Measure(self.clock, "on_new_event"):
+            user_streams = set()
 
-                for user in users:
-                    user_stream = self.user_to_user_stream.get(str(user))
-                    if user_stream is not None:
-                        user_streams.add(user_stream)
+            for user in users:
+                user_stream = self.user_to_user_stream.get(str(user))
+                if user_stream is not None:
+                    user_streams.add(user_stream)
 
-                for room in rooms:
-                    user_streams |= self.room_to_user_streams.get(room, set())
+            for room in rooms:
+                user_streams |= self.room_to_user_streams.get(room, set())
 
-                time_now_ms = self.clock.time_msec()
-                for user_stream in user_streams:
-                    try:
-                        user_stream.notify(stream_key, new_token, time_now_ms)
-                    except Exception:
-                        logger.exception("Failed to notify listener")
+            time_now_ms = self.clock.time_msec()
+            for user_stream in user_streams:
+                try:
+                    user_stream.notify(stream_key, new_token, time_now_ms)
+                except Exception:
+                    logger.exception("Failed to notify listener")
 
-                self.notify_replication()
+            self.notify_replication()
 
-                # Notify appservices
-                self._notify_app_services_ephemeral(
-                    stream_key, new_token, users,
-                )
+            # Notify appservices
+            self._notify_app_services_ephemeral(
+                stream_key, new_token, users,
+            )
 
     def on_new_replication_data(self) -> None:
         """Used to inform replication listeners that something has happened
diff --git a/synapse/util/metrics.py b/synapse/util/metrics.py
--- a/synapse/util/metrics.py
+++ b/synapse/util/metrics.py
@@ -111,7 +111,8 @@ def __init__(self, clock, name):
         curr_context = current_context()
         if not curr_context:
             logger.warning(
-                "Starting metrics collection from sentinel context: metrics will be lost"
+                "Starting metrics collection %r from sentinel context: metrics will be lost",
+                name,
             )
             parent_context = None
         else:


Test patch: