Repository: matrix-org/synapse

Issue:
500s calling /messages on matrix.org
as seen by @schiessle on riot-android at https://github.com/vector-im/riot-android/issues/2802


Discussion:
This is

```
2018-12-26 19:22:53,918 - synapse.http.server - 112 - ERROR - GET-49571769 - Failed handle request via <function JsonResource._async_render at 0x7f889c60b048>: <XForwardedForRequest at 0x7f87a583a550 method='GET' uri='/_matrix/client/r0/rooms/!zMpNuDfQKwlKBGpwEE%3Amatrix.org/messages?from=t50744-785986860_667929170_3319674_214612531_100379625_537039_14986180_10806542_26432&limit=20&dir=b&filter=%7B%22lazy_load_members%22%3Atrue%7D' clientproto='HTTP/1.1' site=8080>: Traceback (most recent call last):
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 1475, in gotResult
    _inlineCallbacks(r, g, status)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
--- <exception caught here> ---
  File "/home/matrix/synapse/synapse/http/server.py", line 81, in wrapped_request_handler
    yield h(self, request)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/home/matrix/synapse/synapse/http/server.py", line 316, in _async_render
    callback_return = yield callback(request, **kwargs)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/python/failure.py", line 491, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "/home/matrix/synapse/synapse/rest/client/v1/room.py", line 479, in on_GET
    event_filter=event_filter,
  File "/home/matrix/.synapse-py3/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/matrix/synapse/synapse/handlers/pagination.py", line 266, in get_messages
    events[0].event_id, state_filter=state_filter,
builtins.IndexError: list index out of range
```

Resolving patch:
diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py
--- a/synapse/handlers/pagination.py
+++ b/synapse/handlers/pagination.py
@@ -235,6 +235,17 @@ def get_messages(self, requester, room_id=None, pagin_config=None,
                 "room_key", next_key
             )
 
+        if events:
+            if event_filter:
+                events = event_filter.filter(events)
+
+            events = yield filter_events_for_client(
+                self.store,
+                user_id,
+                events,
+                is_peeking=(member_event_id is None),
+            )
+
         if not events:
             defer.returnValue({
                 "chunk": [],
@@ -242,16 +253,6 @@ def get_messages(self, requester, room_id=None, pagin_config=None,
                 "end": next_token.to_string(),
             })
 
-        if event_filter:
-            events = event_filter.filter(events)
-
-        events = yield filter_events_for_client(
-            self.store,
-            user_id,
-            events,
-            is_peeking=(member_event_id is None),
-        )
-
         state = None
         if event_filter and event_filter.lazy_load_members():
             # TODO: remove redundant members


Test patch: